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

An Interesting discussion about malloc( ) and calloc( )
What are malloc( ) and calloc( )? Simply putting they are the predefined functions in C language. Malloc( ) and calloc( ) are two such functions which are used for more or less identical purpose and...

Pointers Concept
Every variable has a memory location and every memory location has its address defined which can be accessed using ampersand (&) operator, which denotes an address in memory. Consider the following...

Advantages of C++ Language
Advantages of C++ - C++ is a profoundly convenient dialect and is frequently the dialect of decision for multi-gadget, multi-stage application advancement. - C++ is a protest situated programming dialect...

Set 0 To Kth Bit In A Variable
The following code snippet Set 0 to Kth Bit in an variable #include int UnsetBitValue(int n, int k);int main(){ printf("%d\n\n",UnsetBitValue(255,6)); return 0;}// Set kth bit to zeroint UnsetBitValue(int...

Some interview questions and answers for fresher level on Pointers
What is a void pointer? Void pointer is a special type of pointer which can reference or point to any data type. This is why it is also called as Generic Pointer. As a void pointer can point to...
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