Examples of printf() in C
Example 1: Print a Variable using Specifier in printf()
In this example, we are printing an integer using a format specifier “%d” which is used for an integer. In the printf() function we are printing integers along with string using %d and in the arguments, we have passed variable names in a sequence corresponding to their format specifiers.
C
// C program to print a variable #include <stdio.h> int main() { int num1 = 99; int num2 = 1; printf ( "The sum of %d and %d is %d\n" , num1, num2, num1 + num2); return 0; } |
The sum of 99 and 1 is 100
Example 2: printf with Specified Width
In this example, we will specify the width of the output which will be printed by the printf() function.
C
// C program to illustrate the use of printf with width // specifier #include <stdio.h> int main() { // number to be printed int num = 123456; // printing the num with 10 width and getting the // printed characters in char_printed printf ( "Printing num with width 10: " ); int chars_printed = printf ( "%10d" , num); printf ( "\nNumber of characters printed: %d" , chars_printed); // specifying with using other method printf ( "\nPrinting num with width 3: " ); chars_printed = printf ( "%*d" , 3, num); printf ( "\nNumber of characters printed: %d" , chars_printed); return 0; } |
Printing num with width 10: 123456 Number of characters printed: 10 Printing num with width 3: 123456 Number of characters printed: 6
As we can see, even if we define the width that is less than the present characters, all the characters are still printed. Also, we have seen the two ways in which we can define the width.
Example 3: printf with Precision Sub-Specifier
In this example, we will demonstrate the precision sub-specifier in the printf() function
C
// C program to illustrate the use of precision // sub-specifier #include <stdio.h> int main() { int num = 2451; float dec = 12.45126; char * str = "w3wiki" ; // precision for integral data printf ( "For integers: %.10d\n" , num); // precision for numbers with decimal points printf ( "For floats: %.2f\n" , dec); // for strings printf ( "For strings: %.5s" , str); return 0; } |
For integers: 0000002451 For floats: 12.45 For strings: Geeks
Example 4: printf with Length Sub-Specifier
C
// C program to illustrate the length modifier #include <stdio.h> int main() { long var = 3000000000; // printing var using %d printf ( "Using %%d: %d" , var); // printing var using %ld printf ( "\nUsing %%ld: %ld" , var); return 0; } |
Using %d: -1294967296 Using %ld: 3000000000
Related Articles:
printf in C
In C language, printf() function is used to print formatted output to the standard output stdout (which is generally the console screen). The printf function is a part of the C standard library <stdio.h> and it can allow formatting the output in numerous ways.