#6. Format specifiers in C
The format specifier is used during input and output. It is a way to tell the compiler what type of data is in a variable during taking input using scanf() or printing using printf(). Some examples are %c, %d, %f, etc. The format specifier in printf() and scanf() are mostly the same but there is some difference which we will see.
printf(char *format, arg1, arg2, …)
This function prints the character on standard output and returns the number of characters printed, the format is a string starting with % and ending with a conversion character (like c, i, f, d, etc.). Between both, there can be elements governing the printing format.
Below is its description
- A minus(-) sign tells left alignment.
- A number after % specifies the minimum field width to be printed if the characters are less than the size of the width of the remaining space is filled with space and if it is greater then it is printed as it is without truncation.
- A period( . ) symbol separates field width with precision.
Precision tells the minimum number of digits in an integer, the maximum number of characters in a string, and the number of digits after the decimal part in a floating value.
Character format specifier: %c
#include <stdio.h>
// Let's use %c to print a single assigned character
int main()
{
char ch = 'A';
printf("%c\n", ch);
return 0;
}
Output:
A
For Signed Integer format specifier: %d, %i
#include <stdio.h>
// Let's use %d for a single decimal integer
// And use %i to print decimal, hexadecimal or octal integers
int main()
{
int x = 45, y = 90;
printf("%d\n", x);
printf("%i\n", x);
return 0;
}
Output:
45
45
Unsigned Integer Format Specifier: %u
#include <stdio.h>
// Let's use %u to print unsigned decimal integer
int main()
{
// The -10 value is converted into it's positive
// equivalent by %u
printf("%u\n", -10);
printf("%u\n", 10);
return 0;
}
Output:
4294967286
10
Floating-point format specifier : %f, %e or %E
#include <stdio.h>
// Let's use %f to print floating point numbers
// And %e to print scientific notation of floats
int main()
{
float a = 12.67;
printf("%f\n", a);
printf("%e\n", a);
return 0;
}
Output:
12.670000
1.267000e+01
Unsigned Octal number for integer: %o
#include <stdio.h>
int main()
{
int a = 67;
printf("%o\n", a);
return 0;
}
Output:
103
Unsigned Hexadecimal for integer: %x, %X
#include <stdio.h>
// Let's use %X and %x to print and unsigned hexadecimal
// %X in the form of A, B, C, D...
// %x in the form of a, b, c, d...
int main()
{
int a = 15;
printf("%x\n", a);
return 0;
}
Output:
f
String printing: %s
#include <stdio.h>
// Let's use %s to print a string
int main()
{
char a[] = "Docotor Eyob";
printf("%s\n", a);
return 0;
}
Output:
Doctor Eyob
Address Printing: %p
When our intention is to print the memory address of a variable/pointer ‘%d’ will not work because ‘%d’ will try to format an address into a number and values like 0xbfdd812 are clearly not a number, i.e. we MUST use %p.
#include <stdio.h>
int main()
{
int a = 10;
printf("The Memory Address of a: %p\n",(void*)&a);
return 0;
}
Output:
The Memory Address of a: 0x7ffc85861624
More formatting
#include <stdio.h>
int main()
{
char str[] = "DoctorDoHardThings";
printf("%20s\n", str);
printf("%-20s\n", str);
printf("%20.5s\n", str);
printf("%-20.5s\n", str);
return 0;
}
Output:
DoctorDoHardThings
DoctorDoHardThings
Docotor
Doctor
Explanation:
%20s\n
% start formatting operator
20 specify fieldwidth (min number of printed characters)
s specify format type as string.
\n print newline character/s
scanf(char *format, arg1, arg2, …)
This function takes input using standard input (keyboard) and stores it in a variable accordingly. It returns the number of items successfully read. Formal parameter arg1, agr2, .. must be a pointer
decimal integer: %d
#include <stdio.h>
int main()
{
int a = 0;
scanf("%d", &a); // input is 45
printf("%d\n", a);
return 0;
}
An integer may be octal or hexadecimal: %i
#include <stdio.h>
int main()
{
int a = 0;
scanf("%i", &a); // input is 017 (octal of 15 )
printf("%d\n", a);
scanf("%i", &a); // input is 0xf (hexadecimal of 15 )
printf("%d\n", a);
return 0;
}
Double floating-point number: %lf
#include <stdio.h>
int main()
{
double a = 0.0;
scanf("%lf", &a); // input is 45.65
printf("%lf\n", a);
return 0;
}
Output:
45.650000
String input: %s
#include <stdio.h>
int main()
{
char str[20];
scanf("%s", str); // input is geeksforgeeks
printf("%s\n", str);
return 0;
}
Output:
P@
Character input: %c
#include <stdio.h>
int main()
{
char ch;
scanf("%c", &ch); // input is A
printf("%c\n", ch);
return 0;
}
Many other format specifiers are also there
1.%u for an unsigned integer.
2.%lld for long long int.
3.%o octal integer without leading zero
4.%x hexadecimal integer without 0x before the number.
Let’s recap: