Format specifier defines the type of data to be printed on standard output. Whether to print formatted output or to take formatted input. Format specifier is also known as format string. The format string (format specifier) always starts with a '%' character.
Here is the list of all format specifiers in C programming language.
Format Specifier |
Description |
Supported Data
Type |
%c |
used to print character |
char |
%d or %i |
It is used to
print the signed integer value where signed integer means that the variable
can hold both positive and negative values. |
short |
%e/%E |
It is used for
scientific notation. It is also known as Mantissa or Exponent. |
float |
%f |
It is used to print decimal
floating point values. By default, it
prints the 6 values after '.'. |
float |
%g or %G |
It is used to
print the decimal floating-point values, and it uses the fixed precision,
i.e., the value after the decimal in input would be exactly the same as the
value in the output. |
float |
%hi |
signed integer (short) |
short |
%hu |
unsigned integer (short) |
unsigned integer |
Let's understand the format specifiers in detail through an example.
Example 1: Format Specifier %d is used for Integer value
#include <stdio.h>
#include <conio.h>
void main ()
{
int a = 10, b= 20;
clrscr(); //used to clear the screen
/* Format specifier %d, will be replace with a value of variable a i.e. 10 */
printf("Value of a = %d\n", a);
/* Format specifier %d, will be replace with a value of b i.e. 20 */
printf("Value of b = %d\n", b);
/* Same way, %d and %d will be replace with values of
* variables- a and b i.e. 10 and 20 */
printf("Value of a = %d and b = %d\n", a, b);
/* Remember string written inside the double quotes like-
* "whatever string is written" will print as it is.
* And In place of format specifier like %d and %d = %d
* will be replace with value of variables a, b and expression a+b
* in the same order in which they have written. */
printf("Sum of %d and %d = %d\n", a, b, a + b);
getch(); //used to take a character form keyboard
}
Output
Value of a = 10
Value of b = 20
Value of a = 10 and b = 20
Sum of 10 and 20 = 30
Example 2: %f for float and %lf for double
#include <stdio.h>
#include <conio.h>
void main ()
{
float num1 = 12.5;
double num2 = 10.4;
clrscr(); //used to clear screen
/* Format specifier %f, is used for float type value. */
printf("num1 = %f\n ", num1);
/* Format specifier %lf, is used for double type value. */
printf("num2 = %lf\n ", num2);
getch(); //used to take a character form keyboard
}
Note: To print float, we use %f format specifier. Similarly, we use %lf to print double values.
Output
num1 = 12.500000
num2 = 10.400000
Example 3: %c for Characters
#include <stdio.h>
#include <conio.h>
void main ()
{
char chr = 'p';
clrscr(); //used to clear screen
/* Format specifier %c, is used for character. */
printf("Character = %c ",chr);
getch(); //used to take a character form keyboard
}
Output
Character = p
Example 4: %u for unsigned Integer
int main()
{
int b=10;
int c= -10;
printf("Value of b is:%u", b);
printf("\nValue of c is:%u",c);
getch();
}
In above program, we are displaying the value of b and c by using an unsigned format specifier, i.e., %u. The value of b is positive, so %u specifier prints the exact value of b, but it does not print the value of c as c contains the negative value.
Output
b = 10
c = 4294967286
0 Comments:
Post a Comment
Please don't enter any spam link in the comment box.