Updated April 1, 2023
Introduction to Format Specifiers in C
In C programming language, format specifiers are a kind of special operators that are used for input and output processing i.e. these specifiers are a type of data that is used to print the data on standard output. These specifiers are usually associated with printf and scanf functions for printing the output data that is referred to by any variable. So whenever we want to print the value of the variable on the standard output then we use scanf() function through which we use format specifier for particular data types to print accordingly and these are implemented in printf() function. These format specifiers usually start with the “%” symbol followed by characters for particular data types.
Working of Format Specifier with Examples
In the C programming language, there are different varieties of format specifiers for different data types. The format specifier starts with the “%” symbol followed by the characters of specified data types. We use these format specifiers to print on the output using printf() function and to take the values we use the same format specifier using scanf() function. Let us see the list of format specifiers used in C programming language for different data types.
Syntax:
Printf("%format_specifier", variable_name);
Scanf(" %format_specifier", &variable_name);
Some of the basic format specifiers used in C programming language are as follows:
- For integer data types we use the “%d” format specifier to print the integer values.
Example:
#include <stdio.h>
int main()
{
int a = 45;
printf("%d\n", a);
return 0;
}
Output:
- For float data types we use the “%f ” format specifier to print the real numbers means numbers with floating decimal points.
Example:
#include <stdio.h>
int main()
{
float a = 45.76;
printf("%f\n", a);
return 0;
}
Output:
- For printing any characters as output then we use the “%c” format specifier.
Example:
#include <stdio.h>
int main()
{
char a = 'E';
printf("%c\n", a);
return 0;
}
Output:
- For printing the entire string or group of characters then we use the “%s” as format specifier to print string as output.
Example:
#include <stdio.h>
int main()
{
char a[] = "Educba Training";
printf("%s\n", a);
return 0;
}
Output:
- To print hexadecimal values we use the “%x” or “%X” specifier in C.
Example:
#include <stdio.h>
int main()
{
int data = 14;
printf("%x\n", data);
return 0;
}
Output:
- To print the value stored in long int data types we use the “%ld” format specifier.
Example:
#include <stdio.h>
int main()
{
long int a = 10;
printf("%ld\n", a);
return 0;
}
Output:
- To print the values stored in unsigned integer we use the “ %u” specifier.
Example:
#include <stdio.h>
int main()
{
int a = 10;
printf("%u\n", a);
return 0;
}
Output:
- For printing octal integer without leading zero we use the %o.
Example:
#include <stdio.h>
int main()
{
int a = 65;
printf("%o\n", a);
return 0;
}
Output:
- To print % we can use %% specifier for printing the “%” on standard output.
Example:
#include <stdio.h>
int main()
{
printf("%%");
return 0;
}
Output:
There are many other format specifiers in C programming language. Let us see few of the other specifiers used in programming. They are listed as below:
Specifier | Uses |
%hi | Short signed |
%hu | Short unsigned |
%lf | Long double |
%p | An address or pointer |
%n | Prints nothing |
%e | Prints floating point number in scientific notation |
%E | Prints floating point number in scientific notaition |
There are different other basic format specifiers where we add symbols before the format specifiers. Such symbols are as follows:
- – (minus symbol) this is used for left alignment
- A number after % defines that minimum field width and if the string is less than the specified width then by default it will be filled with spaces.
- Then we use (.) period this symbol is used to separate field width and its precision.
Example:
#include <stdio.h>
main() {
char str[] = "Educba Training";
printf("%20s\n", str);
printf("%-20s\n", str);
printf("%20.5s\n", str);
printf("%-20.5s\n", str);
}
Output:
In the above program, the first print statement which has “20s” prints 20 characters including the string it shifts to the right, the second print statement which has “-20s” prints the string at the left as it aligns to the left, the third print statement “20.5s” prints the characters up to 5 characters of the string and also shifts 20 characters to the right side, the fourth print statement has“-20.5s” that prints the 5 characters of the string and shifts the string to the left side.
In the C programming language, the scanf() function also uses a format specifier. This function is used to take input from the user through the keyboard and store it in the variable declared. So to it can return the items or variables that are read. This function can also take different format specifier for different data types.
Syntax:
scanf(data_type *format_specifier, arg1, arg2…)
- To read integer values from the user then we use the %d.
Example:
#include <stdio.h>
int main()
{
int a = 0;
printf("Enter the integer value:");
scanf("%d", &a);
printf("%d\n", a);
return 0;
}
Output:
- To take input as integer octal or hexadecimal values:
Example:
#include <stdio.h>
int main()
{
int a = 0;
printf("Enter the octal number");
scanf("%i", &a);
printf("%d\n", a);
printf("Enter the hexadecimal number");
scanf("%i", &a);
printf("%d\n", a);
return 0;
}
Output:
- To take character as input from the keyboard we have the following example.
Example:
#include <stdio.h>
int main()
{
char a;
printf("Enter any character: ");
scanf("%c", &a);
printf("%c\n", a);
return 0;
}
Output:
- To take string as input then let us see the following example.
Example:
#include <stdio.h>
int main()
{
char a[50];
printf("Enter any string: ");
scanf("%s", a);
printf("%s\n", a);
return 0;
}
Output:
Conclusion – Format Specifiers in C
This article gives a brief description of the format specifier used in the C programming language. In C, there are different format specifier for different data types and these are used to print the values stored in variables using printf() function and these variable values can be taken as input and to do this also format specifiers are used using scanf() function. Few of the specifiers discussed are integer (%d), float (%f), char (%c), string (%s), etc.
Recommended Articles
This is a guide to Format Specifiers in C. Here we discuss the Working of Format Specifier in the C programming language along with the Examples. You may also have a look at the following articles to learn more –