Format Modifiers (Format Specifiers)
Format specifiers are the secret codes in C that control how data is displayed and input. They're like instructions for the computer, telling it how to format numbers, characters, and other data types the way we want when using functions like printf() and scanf(). Mastering these codes is crucial for any C programmer who wants their programs to work flawlessly.
List of Format Specifiers in C
Format Specifier Description
%c For character type.
%d For signed integer type.
%e or %E For scientific notation of floats.
%f For float type.
%g or %G For float type with the current precision.
%i Unsigned integer
%ld or %li Long
%lf Double
%Lf Long double
%lu Unsigned int or unsigned long
%lli or %lld Long long
%llu Unsigned long long
%o Octal representation
%p Pointer
%s String
%u Unsigned int
%x or %X Hexadecimal representation
%n Prints nothing
%% Prints % character
%d, %i Print as decimal integer
%6d Print as decimal integer, at least six characters wide.
%f Print as floating point
%6f Print as floating point, at least six characters wide.
%.2f Print as floating point, 2 characters after decimal point.
%6.2f Print as floating point, at least 6 wide and 2 characters after decimal point.
%-4s Print as four character string with left justified
%4s Print as four character string with right justified.
A program that subtracts the value 15 from 87 and displays the result, together with an appropriate message, at the terminal.
#include <stdio.h>
int main(){
printf("Subtracts the value 15 from 87 : %i\n", 87 - 15);
return 0;
}
Comments