Format specifiers in C
Learn via video course

Overview
Format Specifiers in C are just a type of string or operator which are mainly used while taking the input from the user and while outputting something on the console. Their motive is to specify the Data type of the input or output to the compiler. By data type, we mean integer, string, float etc. For every data type, their format specifiers are different. Format specifiers always start with a % symbol followed by some characters. In C language, they are used with scanf for input and printf for output.
Scope of the Article
In this article, we are going to learn the following about the Format Specifiers in C language.
- Uses of Format Specifiers.
- Examples of Format Specifiers along with their different types.
- Implementation of Format Specifiers and all about that stuff.
Types of Format Specifiers
Now let's talk about the different types of Format Specifiers that are commonly used in C language. Below is the list of all Format Specifiers in C.
Symbol | Name | Description |
---|---|---|
%d or %i | Signed Integer Format Specifier | It is used when data type is of type signed int which stores an integer value like 1,-1,2. |
%u | Unsigned Integer Format Specifier | It is used when data type is of type unsigned int which stores unsigned integer value |
%f | Floating Point Format Specifier | It is used when data type is of type float which stores decimal floating point values like 2.5, 3.4 |
%s | String Format Specifier | It is used when data type is of type String which stores a string like "HelloWorld". |
%c | Character Format Specifier | It is used when data type is of type Char which stores a single character like 'a','b'. |
%p | Address Printing Format Specifier | It is used only while printing the address of some variable or pointer |
%ld | Long Integer Format Specifier | It is used when data type is of long int which stores a long integer value from range [−2,147,483,647, +2,147,483,647]. |
%lld | Long Long Integer Format Specifier | It is used when data type is of type long long int which stores a long long integer value of up to 64 bits. |
%lf | Double floating Format Specifier | It is used when data type is of type double which stores high-precision floating-point data. |
Now, as we discussed the various types of Format Specifiers in C, it's time to learn how to implement them in C language. Let's look at the code of both input and output format.
Code
Output
Explanation
At first, we declared 3 integer variables var1,var2,var3, and then we took input of these three variables from the user using scanf with the help of %d format specifier as these all are int variables, after that, we are printing the values input by the user to the console using printf with also use %d to tell the compiler the data type of the variable which it is going to print.
Implementation Through Pseudo Code With Examples
Now let's discuss and see the implementation of some Commonly used Format Specifiers in C in detail.
- Format Specifiers of different integer types - In C language, different integer Data types are int, unsigned int, long int, long long int and their Format Specifiers are different from each other.Now we will see the implementation of input as well as output of some integer data types like int,unsigned int, long long int, long int etc. Here is the code of the same.
Code
Output
In the above code, for every variable we use their appropriate format specifier according to their data type.
- Format Specifiers of different Floating data types - They are mainly of two types - float and double and are used if we want to take input or give output in some decimal value.The difference between them is just the precision, Double data type is more precise than float data type.Both of them have their different Format Specifiers. Let's look at the code and implementation of the same.
Code
Output
In the above code, for every variable we use their appropriate format specifier according to their data type.
- Format Specifiers of char and string data type - If we want to input our output a single character or a bunch of characters (which is a string or char array) we use different Format Specifiers. As we know, in C Language strings are the character array, so to take input of this in one line syntax is a little bit different from others. Let's see the implementation of that also.
Code
In the above code, for every variable we use their appropriate format specifier according to their data type.
- Address Printing Format Specifier - This is a something different format specifier, and it is used only for printing the address of some variable or some pointer. Below is the implementation of the same.
Output
It is not compulsory that the address is always same. It will always be different during every compilation.
Now, as we have discussed all the important and commonly used Format Specifiers. Let's Have a look at some more Format Specifiers in C Language which are not commonly used, but its good to have a knowledge of them also.
-
Octal Integer Format Specifier (%o) - This Format specifier is basically used for printing or taking input for the octal integer, which is a Base 8 integer. It is represented by %o.
-
HexaDecimal Integer Format Specifier (%x) - This Format Specifier is mainly used to print or take input for the Hexadecimal unsigned integer value. This is represented by %x symbol. In this type of Format Specifier all the alphabets are always printed in small cases only.
-
Signed Short and Unsigned Short Format Specifier - For scanning and printing the Signed short we use %hi symbol, and for unsigned short we use %hu symbol and their implementation is same as other specifiers.
So Now We are done with all the Format Specifiers in C language, We have learned all the various types of it along with their examples and also learned how to implement them along with the code. That's all with this topic, now let's recap one time everything that we have learned so far.
Conclusion
- The Format Specifiers in C are type of operator or string that tells the data type of the input thing and the output thing to the compiler.
- For all data types , their Format Specifiers are different.
- Format Specifiers are used with scanf as well as printf in C Language.