Format specifiers in C Language
Format specifiers are used to specify the type of data to be printed on the output screen. In C language, a format specifier starts with % and ends with a conversion character.
Basic Format Specifiers
Format Specifier | Output Format |
%d | Integer (decimal) |
%i | Integer (decimal, octal, or hexadecimal) |
%f | Floating-point number (decimal notation) |
%e or %E | Floating-point number (scientific notation) |
%c | Single character |
%s | String of characters |
%p | Pointer address |
Example:
int a=10;
float b=20.5;
printf(“Integer value is %d and Float value is %f”,a,b);
Output:
Integer value is 10 and Float value is 20.5
Format specifiers example
// Create variables
int val = 15; // Integer (whole number)
float val2 = 5.99; // Floating point number
char name = ‘D’; // Character
// Print variables
printf(“%d\n”, val); // %d is a integer format specifier & \n is for new line on the out put console
printf(“%f\n”, val1); // %f is a float format specifier
printf(“%c\n”, name); // %c is a character format specifier
Declare Multiple Variables in C Language
int num1, num2, num3;
float num4, num5, num6;
char ch1, ch2, ch3;
Declare Multiple Variables in C Language
int num1, num2, num3;
float num4, num5, num6;
char ch1, ch2, ch3;
int x = 5, y = 6, z = 50;
printf(“%d”, x + y + z);
int x, y, z;
x = y = z = 50;
printf(“%d”, x + y + z);