-->

Printf function in c

3 minute read

         Printf function in c

 
Explain printf() function
Printing a text message on-screen is simple. Call the printf() function and pass the desired message enclosed in double quotation marks. For example, to display “welcome to c” on-screen, we write
printf("welcome to c");
In addition to text messages, however, we want to display the value of program variables. For that we want to display the value of the numeric variable x on-screen, along with some identifying text. We could use the printf() function as follows:
printf("The value of x is %d", x);
The resulting screen display ( the value of x is 10)
The value of x is 10
In the above example, two arguments are used in printf(). The first argument is in the double quotation marks and is known as format string. The second argument contain the name of the variable (x) which print the value of variable.
The most commonly needed conversion specifiers.
Specifier                             Meaning                                        Types 
%c                                  Single character                             char
%d                                 Signed decimal                              int, short
%ld                                Signed long decimal                      long
%f                                  Decimal floating-point                  float, double
%s                                  Character string                             char arrays
%u                                 Unsigned decimal integer              unsigned int,
%lu                                Unsigned long decimal integer      unsigned long