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


Necessity of Theory and Practical in Computer Science.
Upon studying a subject both theory and practical are important. Usually many schools concentrate more on theory and the marks not on the practical. Other and opposite kind of people prefer practical...


What is a Programming Language
What is a Language? Language is a communication system of human. What is a programming Language? A programming Language is a formal constructed language design to communicate...

Variables
Variables in C Language:A variable is a name that may be used to store a data value. Unlike constant, variables are changeable, we can change value of a variable during execution of a program. A programmer...

Looking for C Language Classes?

Learn from Best Tutors on UrbanPro.

Are you a Tutor or Training Institute?

Join UrbanPro Today to find students near you
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