Format Specifier in C
Hello Everyone, In this article, we will cover the concept of Format Specifier in C. Format specifier in c is a special sequence of characters that is used to indicate the type and format of data to be printed or read from standard input/output. Whether you’re new to C or have some experience, this guide will help you to understand Format Specifier in C effectively. Let’s get started.
Format specifiers begin with a percent sign (%) followed by a character that indicates the type of data to be formatted. For example, %d is a format specifier for an integer data type, %f is a format specifier for a floating-point data type, and %s is a format specifier for a string data type.
Format specifiers are used with functions such as printf() and scanf() in C language. When used with printf(), format specifiers are used to format and print data on the console or other output device. When used with scanf(), format specifiers are used to read and store data from standard input.
Here is a table that lists all the format specifiers in C programming language along with their corresponding data types:
Format Specifier | Data Type |
---|---|
%c | char |
%d | int |
%e or %E | scientific notation of float/double |
%f | float |
%g or %G | %e or %f (whichever is shorter) |
%hi | short int |
%hu | unsigned short int |
%i | int |
%ld | long int |
%li | long int |
%lld | long long int |
%llu | unsigned long long int |
%lu | unsigned long int |
%o | octal number |
%p | pointer address |
%s | string |
%u | unsigned int |
%x or %X | hexadecimal number |
It’s important to use the correct format specifier with the appropriate data type to avoid errors in your program.
C Program that demonstrates the use of all format specifiers:
#include <stdio.h>
int main() {
char c = 'A';
int i = 42;
long int li = 123456789L;
long long int lli = 123456789012345LL;
unsigned int ui = 42;
unsigned long int uli = 1234567890UL;
unsigned long long int ulli = 1234567890123456789ULL;
short int si = 32767;
unsigned short int usi = 65535;
float f = 3.14159f;
double d = 3.14159265358979323846;
char s[] = "Hello, World!";
void* ptr = &i;
printf("char: %c\\n", c);
printf("int: %d\\n", i);
printf("long int: %ld\\n", li);
printf("long long int: %lld\\n", lli);
printf("unsigned int: %u\\n", ui);
printf("unsigned long int: %lu\\n", uli);
printf("unsigned long long int: %llu\\n", ulli);
printf("short int: %hi\\n", si);
printf("unsigned short int: %hu\\n", usi);
printf("float: %f\\n", f);
printf("double: %lf\\n", d);
printf("string: %s\\n", s);
printf("pointer address: %p\\n", ptr);
return 0;
}
In this program, we have declared variables of all data types that correspond to each format specifier. We then use the printf() function to display the values of these variables on the console, using the appropriate format specifier for each data type.
When we run this program, we should see the following output:
char: A
int: 42
long int: 123456789
long long int: 123456789012345
unsigned int: 42
unsigned long int: 1234567890
unsigned long long int: 1234567890123456789
short int: 32767
unsigned short int: 65535
float: 3.141590
double: 3.141593
string: Hello, World!
pointer address: 0x7ffeefbff8fc
As we can see, the format specifiers have been used to format the output of the program for each data type. By using the correct format specifier for each variable, we can ensure that the output is displayed in a readable and understandable format.