Less Used Format Specifiers in C
Some format specifiers are not used in every beginner program, but they are important for exams, number systems, memory concepts and larger integer values.
First study the common page: Format Specifiers in C.
Less Used Format Specifiers Table
| Format Specifier | Used For | Data Type / Use | Example |
|---|---|---|---|
| %u | Unsigned integer | unsigned int |
unsigned int x = 50; |
| %ld | Long integer | long int |
long int n = 100000; |
| %lld | Long long integer | long long int |
long long int n = 9000000000; |
| %hd | Short integer | short int |
short int x = 10; |
| %x | Hexadecimal in lowercase | int |
255 prints ff |
| %X | Hexadecimal in uppercase | int |
255 prints FF |
| %o | Octal number | int |
8 prints 10 |
| %e or %E | Scientific notation | float, double |
1234.5 prints like 1.234500e+003 |
| %p | Address or pointer value | Pointer | printf("%p", &x); |
| %zu | Result of sizeof |
size_t |
printf("%zu", sizeof(int)); |
1. %u - Unsigned Integer
%u is used for unsigned int. Unsigned integers store
only zero and positive values.
#include <stdio.h>
int main() {
unsigned int count = 50;
printf("Count = %u", count);
return 0;
}
Output:
Count = 50
Explanation:
countis an unsigned integer.%uprints unsigned integer values.
2. %ld, %lld and %hd - Size Based Integer Specifiers
These specifiers are used when integer values are stored using short,
long or long long.
#include <stdio.h>
int main() {
short int small = 10;
long int population = 1000000;
long long int distance = 9000000000;
printf("Small = %hd\n", small);
printf("Population = %ld\n", population);
printf("Distance = %lld", distance);
return 0;
}
Output:
Small = 10
Population = 1000000
Distance = 9000000000
Explanation:
%hdprints short integer.%ldprints long integer.%lldprints long long integer.
3. %x, %X and %o - Number System Specifiers
These specifiers print integer values in hexadecimal and octal number systems.
#include <stdio.h>
int main() {
int number = 255;
printf("Hex lowercase = %x\n", number);
printf("Hex uppercase = %X\n", number);
printf("Octal = %o", number);
return 0;
}
Output:
Hex lowercase = ff
Hex uppercase = FF
Octal = 377
Explanation:
%xprints hexadecimal value in lowercase.%Xprints hexadecimal value in uppercase.%oprints octal value.
4. %e and %E - Scientific Notation
%e and %E print decimal values in scientific notation.
This is useful for very large or very small numbers.
#include <stdio.h>
int main() {
double value = 12345.678;
printf("Scientific = %e\n", value);
printf("Scientific = %E", value);
return 0;
}
Output:
Scientific = 1.234568e+004
Scientific = 1.234568E+004
Explanation:
%eprints scientific notation with lowercase e.%Eprints scientific notation with uppercase E.
6. %zu - sizeof Result Specifier
%zu is used to print values of type size_t. The
sizeof operator returns a size_t value.
#include <stdio.h>
int main() {
printf("Size of int = %zu bytes", sizeof(int));
return 0;
}
Possible Output:
Size of int = 4 bytes
Explanation:
sizeof(int)returns the memory size of int in bytes.%zuis the correct specifier for printingsizeofresult.
6. %zu - sizeof Result Specifier
%zu is used to print values of type size_t. The
sizeof operator returns a size_t value.
#include <stdio.h>
int main() {
printf("Size of int = %zu bytes", sizeof(int));
return 0;
}
Possible Output:
Size of int = 4 bytes
Explanation:
sizeof(int)returns the memory size of int in bytes.%zuis the correct specifier for printingsizeofresult.
Combined Example of Less Used Format Specifiers
#include <stdio.h>
int main() {
unsigned int rank = 5;
long int population = 1000000;
int number = 255;
printf("Rank = %u\n", rank);
printf("Population = %ld\n", population);
printf("Hex = %X\n", number);
printf("Octal = %o\n", number);
printf("Size of int = %zu bytes", sizeof(int));
return 0;
}
Possible Output:
Rank = 5
Population = 1000000
Hex = FF
Octal = 377
Size of int = 4 bytes
Explanation:
%uprints unsigned integer.%ldprints long integer.%Xprints hexadecimal in uppercase.%oprints octal value.%zuprints sizeof result.
Summary:
- Less used format specifiers are important for special cases.
%uis used for unsigned integer.%ld,%lldand%hdare used for different integer sizes.%x,%Xand%oare used for number systems.%pis used for addresses and%zuis used for sizeof result.