Format Specifiers in C programming
Introduction:
In this article, We are going to discuss the format specifiers in C programming language. and we will also learn when to use which format specifier and how to use the format specifiers.
Format Specifiers in C:
Format Specifiers in the C language are used for formatting input and output values. Which helps us to specify the datatype of each value.
As C-Language is the Strictly typed language. We need to specify the datatype of each variable.
Similarly, We need to specify the datatype while taking the input from the user, and also we need to specify the datatype while printing the value of any variable. We need to specify variables type explicitly. The most common use of format specifiers are in printf() and scanf() functions. Format specifiers are one way to let the compiler know about the data we are using.
C Programming language support few format specifiers we are going to discuss them
📢 We also discussed all available data types and The size and ranges of all datatypes in following article, Please check for more info.
Different type of Format Specifiers in C Programming:
Here is the list of format specifiers in C programming Language
Datatype Name | Format Specifier |
---|---|
Character ( Singed and Unsigned ) | %c |
Short Integer (Signed) | %hd or %hi |
Unsigned Short Integer | %hu |
Integer (Signed) | %d or %i |
Unsigned Integer | %u |
Long Integer (Signed) | %ld |
Unsigned Long Integer | %lu |
Long Long Integer (Signed) | %lld |
Unsigned Long Long Integer | %llu |
Float | %f |
Double | %lf |
Long Double | %Lf |
Pointer (Memory Address) | %p |
Scientific Notation (Floating point data) | %e |
Octal (Base 8 representation) | %o |
String | %s |
Hexadecimal Representation | %x or %X |
% Character | %% |
Nothing (Empty) | %n |
Here are few examples to showcase the usage of format specifiers.
The Character (%c) Format Specifier:
Character format specifier %c is used to represent the character and unsigned character datatype.
Program:
1 2 3 4 5 6 |
#include <stdio.h> int main() { char ch = 'v'; printf("Value of ch is : %c \n", ch); return 0; } |
Output:
1 |
Value of ch is : v |
The Integer (%d or %i ) and Unsigned Integer (%u) Format Specifier:
We can use %dand %i format specifiers for the Signed Integer datatype. We use %ufor the unsigned Integer datatype.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#include <stdio.h> int main() { // Take Integer value int i = 100; // Integer Format specifier '%i' or '%d' printf("Value of i is : %d \n", i); printf("Value of i is : %i \n", i); // Unsigned Integer format specifier is '%u' // We are giving -1 to get the INT_MAX ( Maximum integer value) unsigned int u = -1; printf("Unsigned int u is : %u \n", u); return 0; } |
Here We used both datatypes of integer datatype. Also for the Unsigned integer datatype, we used the value as -1, So that we will get the Maximum allowable Integer value ( INT_MAX).
Output:
1 2 3 |
Value of i is : 100 Value of i is : 100 Unsigned int u is : 4294967295 |
The Float (%f) format Specifier:
The %f format specifier is used to represent the float datatype values.
Program:
1 2 3 4 5 6 7 8 9 |
#include <stdio.h> int main() { // Take Integer value float f = 3.143; // Float Format specifier '%f' printf("Value of f is : %f \n", f); return 0; } |
Output:
1 |
Value of f is : 3.143000 |
📢 Float datatype takes the precision upto 6 digits.
The Address or Pointer (%p) Format Specifier:
We use %p format specifier for representing the Address or Pointer type values.
Let’s use above float datatype example and convert it to get Address of variable and display it using the %p format specifier.
Program:
1 2 3 4 5 6 7 8 9 |
#include <stdio.h> int main() { // Take Integer value float f = 3.143; // Float Format specifier '%f' printf("Address of f is : %p \n", &f); return 0; } |
Output:
1 |
Address of f is : 0x7ffeee4b3958 |
The Octal (%o), Hexa-decimal(%x and %X) Format Specifiers:
The %oformat specifier is used to represent the data in Octal or Base-8 format.
The %xor %X format specifier is used to represent the data in Hexadecimal or base-16 format.
Here is an example program.
Program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#include <stdio.h> int main() { // Take Integer value int a = 64; // Octal Format Specifier ('%o') printf("Octal Representation : %o \n", a); // Hexa-Decimal Format Specifier ('%x') printf("Hexa-Decimal Representation : %x \n", a); // Hexa-Decimal Format Specifier ('%X') printf("Hexa-decimal̵ Representation : %X \n", a); return 0; } |
Output:
1 2 3 |
Octal Representation : 100 Hexa-Decimal Representation : 40 Hexa-decimal̵ Representation : 40 |
Similarly, We can have all other format specifiers as well. Please play around with the format specifier to make yourself comfortable.
Conclusion:
In this article, We discussed what are format specifiers and the List of all format specifiers in the C language. We also looked into few examples of format specifiers.
5 Responses
[…] Format Specifiers […]
[…] 📢 Learn More about Format specifier here – Format Specifiers in C programming – SillyCodes […]
[…] 📢 Here is the complete list of the format specifier in the C language https://sillycodes.com/format-specifiers-in-c-format/ […]
[…] C Program to Basic Input and Output of datatypes with Format specifiers […]
[…] have a look at the following example, Where we used scanf() function with %s format specifier to read the input string and store it in variable […]