printf() function to display the values.
#include <stdio.h>
int main()
{
int number = 100;
float percentage = 95.75;
char grade = 'A';
char course[] = "C Programming";
printf("Integer Value = %d\n", number);
printf("Float Value = %.2f\n", percentage);
printf("Character Value = %c\n", grade);
printf("String Value = %s\n", course);
return 0;
}
Integer Value = 100
Float Value = 95.75
Character Value = A
String Value = C Programming
A format specifier is a special code used with input and output functions such as printf() and scanf(). It tells the compiler the type of data that should be displayed or accepted.
The following program demonstrates some commonly used format specifiers in C.
Different data types require different format specifiers. Using the correct format specifier ensures that data is displayed correctly.
| Format Specifier | Data Type | Example Value |
|---|---|---|
| %d | int | 100 |
| %f | float | 95.75 |
| %c | char | A |
| %s | string (character array) | C Programming |
In the statement printf("Integer Value = %d", number);, the %d format specifier is used to display an integer value. Similarly, %f is used for floating-point numbers, %c is used for characters, and %s is used for strings.
Some other commonly used format specifiers in C are:
%ld - Long Integer%lf - Double%u - Unsigned Integer%x - Hexadecimal Number%o - Octal NumberFormat specifiers are essential because they help the compiler correctly interpret and display data stored in variables.
First understand the algorithm carefully. Then study the program line-by-line and compare it with the output. Finally, review the explanation section to strengthen your logic and programming understanding.
Rewrite the program without looking at the code. Modify values, conditions or logic and run it again. This helps improve confidence and strengthens coding skills much faster.