Learn C – Day 3 printf

Posted by

This is in continuation of earlier blog on C language for beginners.  In this particular blog,  We will go through  various printf options. Printing a message , on console , is one of the important aspect in programming. It helps  in debugging boot loader, Kernel  and user applications. You may find it with various names such as printf, printk, println etc. You might have already tried first “Hello World ! ” example for C programming.

Are you confused with printf options ?

Many students learn C++ during higher secondary which has cout() for printing messages on console. They find  difficulty with printf options as compared to cout .  I would like to go through a quick discussion followed by demo of important features.

Lets try to understand some basics behind the printf().

What one byte character can represent ?

  • Non-Printable  –  ASCII code  < 0x20
  • Printable            –  ASCII code >=0x20 to <-0x7F
        • 0x20 = Blank, 0x31=1 , 0x41=A , 0x61=a
  • Extended            –  ASCII code >=0x80  

Control Characters – All character 0x00 to 0x1F and 0x7F(DEL)  are called control characters

Escape Sequence 

The backslash  \  is the special character used along with other characters to make a special command called Escape Sequence . For ex \n

CRLF(Carriage Return Line Feed)  \r\n 

Carriage Return  is the terminology taken from old days  typewriter where  a moving carriage used to hold the paper. After completion of one line there was a mechanism to move back carriage to original place and that was called carriage return and the mechanism to move to next line called line feed.

In terms of today’s console, Carriage return (\r) in printf is used for moving cursor or print position to leftmost side and a new line character (\n) has been used to move cursor or print position to the beginning of next line.   The term CRLF(Carriage Return Line Feed) is used for \r\n.

Other useful  escape sequence options  is horizontal tab(\t) . Support of these escape sequence commands depend on the library.

Why I don’t see any print message sometime on console ?

stdout is buffered stream . This buffer will be flushed with new line (\n) or fflush() or buffer stream close or normal exit of the program.

The  stdin and stdout uses buffering to reduce system I/O call overhead :

  • stdin  and stdout(Terminal or TTY) – Line bufferred
  • stdout (non terminal or non TTY)  – Fully buffered
  • stderr – unbuffered 

Which printf options are frequently used ?

Lets go through demo at printf video to understand it better.

Here are few other interesting topics @Embedkari

How to Learn C  practically ? 

How to do web scraping with Python ? 

How to choose sensor, processing device and network for IoT ?

How Computer Vision is  impacting Processor Road MAP ? 

How device driver is related to Operating System ?

How to Learn Linux Device driver coding ? 

How I can practice AVR, ARM, Linux and other Embedded Questions for Interview ? 

How can I start learning RISC-V without purchasing development board ?

How RISC architecture differs in simple and advanced embedded processors ? 

How to use Home Router to understand Embedded Linux Commands ?

Thanks for reading till end. Please provide your feedback regarding this article. Also subscribe  Embedkari for other interesting topics. .

Embedkari has introduced its first low cost technical training .

Thanks for reading till end. I am trying to improve usability of my  site. Did you find this discussion helpful ? If so,  Please subscribe to YouTube channel Embedkarias well for additional embedded related stuff.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.