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 character printed the format is a string starting with % and ends with 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 width the remaining space is filled with space and if it is greater than it printed as it is without truncation.
- A period( . ) symbol separate field width with the precision.
Precision tells the maximum number of digits in integer, characters in string and number of digits after decimal part in floating value.
Lets see these..
Character format specifier : %c
Integer format specifier : %d, %i
Floating-point format specifier : %f, %e or %E
Unsigned Octal number for integer : %o
Unsigned Hexadecimal for integer : %x, %X
String printing : %s
geeksforgeeks geeksforgeeks geeks geeks
scanf(char *format, arg1, arg2, …)
This function take input using standard input (keyboard) and store it in variable accordingly. It returns the number of items successfully read. Formal parameter arg1, agr2, .. must be a pointer
decimal integer : %d
Integer may be octal or in hexadecimal : %i
Double floating-point number : %lf
String input : %s
Character input : %c
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.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- C program to print characters without using format specifiers
- Format specifiers in different Programming Languages
- Using a variable as format specifier in C
- Format String Vulnerability and Prevention with Example
- Difference between %d and %i format specifier in C language
- C program to invert (making negative) an image content in PGM format
- Encode an ASCII string into Base-64 Format
- C program to write an image in PGM format
- Difference between Static and Dynamic Memory Allocation in C
- How does Volatile qualifier of C works in Computing System
- Header files in C/C++ with Examples
- C Programming For Beginners - A 20 Day Curriculum!
- Tr0ll 3 Walkthrough of Check the Flag or CTF Problem
- What are the C programming concepts used as Data Structures
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.