Format Specifier in C
Format Specifier in C
Format specifiers can be described as an operator that is used to print the data referred by any object or variable in combination with the printf () function. Without using the format specifiers, even if a value is stored in a specific variable, you cannot print the value stored in the variable.
By implementing these format specifiers in a printf () function, you can retrieve the data stored in the variables and print them onto the console screen. Format specifiers start with a percentage operator and then have a special character to identify the type of data.
List of format Specifiers in C
Format Specifier | Description |
%f | Float format specifier |
%c | Character format specifier |
%s | String format specifier |
%d | Integer format specifier |
%o | Unsigned octal number for integer |
%u | Unsigned integer format specifier |
Float Format Specifier
The %f format specifier is implemented to reflect the fractional values. This is implemented within the function printf () to print the fractional or floating value stored in the variable. Whenever you need to print any data that is fractional or floating, use the %f format specifier.
Syntax:
printf (“%f”, <variable name>);
Code:
#include <stdio.h> int main() { float a = 15.8; printf("%f\n", a); printf("%e\n", a); return 0; }
Output:
Character Format Specifier
To represent characters, the per cent c format specifier is implemented. This is used for printing the character stored in a variable with the function printf (). You should include the %c format specifier when you want to print character information.
Syntax:
printf (“%c”, <variable name>);
Code:
#include <stdio.h> int main() { char lp = 'W'; printf("%c\n", lp); return 0; }
Output:
String Format Specifier
For the representation of strings, the percent s format specifier is implemented. This is used for printing a string stored in the character array variable in printf function. The %s format specifier should be implemented when you need to print a string.
Syntax:
printf (“%c”, <variable name>);
Code:
#include <stdio.h> int main() { char b[] = "Tutorialandexample"; printf("%s\n", b); return 0; }
Output:
Integer Format Specifier
To represent integer values, the %d format specifier is implemented. It is used for printing the integer value stored in the variable with the function printf ().
Syntax:
printf (“%d”, <variable name>);
Code:
#include <stdio.h> int main() { int a = 25, b = 50; printf("%d\n", a); printf("%i\n", a); return 0; }
Output:
Unsigned Octal Number for Integer
The specifier for the %o format is implemented to display integer values. This is implemented with function printf () to print the octal number integer value stored in the variable.
Syntax:
printf (“%ld”, <variable name>);
Code:
#include <stdio.h> int main() { int b = 92; printf("%o\n", b); return 0; }
Output:
Unsigned Integer Format Specifier
For fetching values from the address of a variable having unsigned decimal integer stored in the memory, the %u format specifier is implemented. This is used for printing the unsigned integer variable inside the printf () function.
Syntax:
printf (“%u”, <variable name>);
Code:
int main() { int a=40; int c= -40; printf("Value of b is:%u",a); printf("\nValue of c is:%u",c); return 0; }
Output: