UrbanPro
true

Learn C Language from the Best Tutors

  • Affordable fees
  • 1-1 or Group class
  • Flexible Timings
  • Verified Tutors

Search in

Everything about printf in C language

Ganesh Shinde
10/01/2020 0 0

What is printf and from where it comes from in C code.? 

You must have used printf so many times in your Car programs but do you really know everything about printf? Let's see. 

What is the full form of printf? 

Printf function name is made of two words 'Print format'. Here format means format specifiers like%d %f etc. 

Printf and scanf functions are actually included in your code by line #include header file. This file contents only header of standard IO functions and respective stdio.c file contains function definitions. 

How to print Hex, Oct and Decimal number formats? 

printf("%x", 10) ; this will print small 'a' on screen which is hex value of number 10.

printf("%X", 10) ; this will print capital A' on screen. 

printf("%#x", 10) ; this will print small '0xa' with prefix 0x on screen. 

printf("%o", 10) ; this will print '12' on screen which is octal value of number 10 

printf("%5d", 10) ; this will print 3 spaces and then number 10 on screen because right alignment given in format i.e. "%5d".

For left alignment use "%-5d" Negative number it will print 10 and then 3 spaces. 

What if you want a variable number of spaces instead of a constant number in format specifiers then use the star as below. 

int s = 5;

printf("%*d",  s, 10); this will give s number of spaces before printing number 10.

%g - is used to print float number without trialling zero. For example. 

printf("%g", 10.2); this will print only 10.2 and not like%f prints 10.200000 always 6 trialling zeros. 

 %p - is used to print pointer values. That means addresses which are hex values. 

%u - is used to print unsigned numbers that means positive numbers. 

%i and %d are equivalent. 

%c - is for character and%s is for word. 

There are also many escape sequence characters which are commonly used but not known to all are:

To print% sign use double%% to avoid conflict with format specifiers and the actual per cent sign. 

Example

printf("i got %d%% in practicals\n", 100) ; this will print I got 100% in practicals

Similarly, to print double quotes use \" . 

Example 

printf("I love \"C\" Language\n", 100) ; this will print I love "C" Language. 

To print backslash use double backslash \\

To bring back cursor to start of the line use \r and to backspace or delete previously printed char on-screen use \b.

Using above \r and \b we can really create loading or progress bar effect on screen because we can display on same line different text with time delay. 

for(I = 0; I <= 100; I ++) 

{

     printf("loading.... %d\r") ;

     // some delay... 

}

This will print loading..... 0 till 100 on same line. 

There are so many such things you must explore to know what printf can do, isn't it? 

 

0 Dislike
Follow 2

Please Enter a comment

Submit

Other Lessons for You

Storage classes in c
Storage classes determine the scope and life time of a variable. Scope is defined as the region over which the defined variable is accessible. Lifetime is the time during which the value of a variable...

C++ Program-Working with constant using #define preprocessor
//Header Files #include#include // using #define preprocessor for defining a constant#define len 10#define br 5#define rad 3#define NEWLINE '\n' //Main function void main(){ int area_r; float area_c; //Function...
S

Lets know about far, near and huge pointers for fresher interview purpose
All of these three candidates relate to 16 bit architecture where segmented memory architecture was there. They are not at all relevant for present days 32 or 64 bit architecture where the way of memory...

Creating First Program Using C Language
Step 1: Install and setup Turbo C compiler on your computer. Step 2: Open Turbo C from your Desktop or Programs menu. Select “File” from menu bar and select option “New”. Step...

Do You Know How Is Size Of Structure Defined?
Size of the structure is defined based on multiplies of bigger data type member in the structure. Example: If a structure contains integer, char, short data type, then size of the Structure will be multiples...
X

Looking for C Language Classes?

The best tutors for C Language Classes are on UrbanPro

  • Select the best Tutor
  • Book & Attend a Free Demo
  • Pay and start Learning

Learn C Language with the Best Tutors

The best Tutors for C Language Classes are on UrbanPro

This website uses cookies

We use cookies to improve user experience. Choose what cookies you allow us to use. You can read more about our Cookie Policy in our Privacy Policy

Accept All
Decline All

UrbanPro.com is India's largest network of most trusted tutors and institutes. Over 55 lakh students rely on UrbanPro.com, to fulfill their learning requirements across 1,000+ categories. Using UrbanPro.com, parents, and students can compare multiple Tutors and Institutes and choose the one that best suits their requirements. More than 7.5 lakh verified Tutors and Institutes are helping millions of students every day and growing their tutoring business on UrbanPro.com. Whether you are looking for a tutor to learn mathematics, a German language trainer to brush up your German language skills or an institute to upgrade your IT skills, we have got the best selection of Tutors and Training Institutes for you. Read more