Understanding of Format Specifiers in C Language
Format specifiers in C language play a crucial role in informing the compiler about the data type to be printed or
scanned during input and output operations. These specifiers always start with a ‘%’ symbol and are utilized within formatted strings in functions like printf(), scanf(), sprintf(), and more.C offers a range of format specifiers, each associated with different data types. For instance, ‘%d’ corresponds to int, ‘%c’ to char, and so on. This article will delve into some commonly used format specifiers and how to effectively use them. In programming, understanding and correctly using C format specifiers is crucial for precise data formatting and manipulation.
Commonly Used Format Specifiers in C
Here’s a list of the most frequently employed format specifiers in C:
Format Specifier | Description |
---|---|
%c | Char data type. |
%d | Signed integer data type. |
%e or %E | Scientific notation for floats. |
%f | Float data type. |
%g or %G | Float data type with current precision. |
%i | Unsigned integer. |
%ld or %li | Long data type. |
%lf | Double data type. |
%Lf | Long double data type. |
%lu | Unsigned int or unsigned long. |
%lli or %lld | Long long data type. |
%llu | Unsigned long long. |
%o | Octal representation. |
%p | Pointer. |
%s | String. |
%u | Unsigned int. |
%x or %X | Hexadecimal representation. |
%n | Prints nothing. |
%% | Prints the ‘%’ character. |
Examples of Format Specifiers in C
- Character Format Specifier – %c in C
‘%c’ is the format specifier for the char data type in C. It can be used for both formatted input and formatted output.
#include <stdio.h>
int main() {
char c;
// Using %c for character input
scanf("Enter some character: %c", &c);
// Using %c for character output
printf("The entered character: %c", c);
return 0;
}
Input:
Enter some character: A
Output:
The entered character: A
- Integer Format Specifier (signed) – %d in C
‘%d’ is used for signed integer input and output in C.
#include <stdio.h>
int main() {
int x;
// Taking integer input
scanf("Enter the two integers: %d", &x);
// Printing integer output
printf("Printed using %%d: %d\n", x);
printf("Printed using %%i: %3i\n", x);
return 0;
}
Input:
Enter the integer: 45
Output:
Printed using %d: 45
Printed using %i: 45
- Unsigned Integer Format Specifier – %u in C
‘%u’ is used for the unsigned integer data type. It also handles negative integer values by converting them to their first complement.
#include <stdio.h>
int main() {
unsigned int var;
scanf("Enter an integer: %u", &var);
printf("Entered Unsigned Integer: %u", var);
printf("Printing -10 using %%u: %u\n", -10);
return 0;
}
Input:
Enter an integer: 25
Output:
Entered unsigned integer: 25
Printing -10 using %u: 4294967286
- Floating-point Format Specifier – %f in C
‘%f’ is the floating-point format specifier for float data type. It’s used for input and output of floating-point numbers. ‘%e’ and ‘%E’ are also available for scientific notation.
#include <stdio.h>
int main() {
float a = 12.67;
printf("Using %%f: %f\n", a);
printf("Using %%e: %e\n", a);
printf("Using %%E, %E", a);
return 0;
}
Output:
Using %f: 12.670000
Using %e: 1.267000e+01
Using %E, 1.267000E+01
- Unsigned Octal Number for Integer – %o in C
‘%o’ is used to print or take input for unsigned octal integers.
#include <stdio.h>
int main() {
int a = 67;
printf("%o\n", a);
return 0;
}
Output:
103
- Unsigned Hexadecimal for Integer – %x in C
‘%x’ is used for hexadecimal integers, with lowercase alphabets in the representation. ‘%X’ is used for uppercase.
#include <stdio.h>
int main() {
int a = 15454;
printf("%x\n", a);
printf("%X", a);
return 0;
}
Output:
3c5e
3C5E
- String Format Specifier – %s in C
‘%s’ is used for printing strings or taking string inputs.
#include <stdio.h>
int main() {
char a[] = "Hi Geeks";
printf("%s\n", a);
return 0;
}
Output:
Hi Geeks
Note: Using ‘%s’ with scanf()
differs slightly, as it stops reading at whitespace. Scansets can be used to avoid this.
- Address Format Specifier – %p in C
‘%p’ is used to print addresses and pointers in C.
#include <stdio.h>
int main() {
int a = 10;
printf("The Memory Address of a: %p\n", (void*)&a);
return 0;
}
Output:
The Memory Address of a: 0x7ffe9645b3fc
Input and Output Formatting
C provides tools for formatting input and output. These tools are inserted between the ‘%’ sign and the format specifier symbol. Some common formatting options include:
- A minus (-) sign for left alignment.
- A number after ‘%’ specifies the minimum field width; if the characters are fewer, the remaining space is filled with spaces.
- A period (.) separates field width and precision.
- Precision sets the minimum number of digits in an integer, maximum characters in a string, or digits after the decimal point in a floating-point value.
Example of I/O Formatting:
“`c
include
int main() {
char str[] = “geeksforgeeks”;
printf(“%20s\n”, str);
printf(“%-20s\n”, str);
printf(“%20.5s\n”, str);
Discover more from PiEmbSysTech
Subscribe to get the latest posts sent to your email.