Format specifiers in C language

Kalana Eranda Jayasuriya
Level Up Coding
Published in
6 min readJan 22, 2020

--

Many peoples have lot of problems with C format specifiers. Most of them don’t know how to use it, when can use it, ranges of data types and etc. I have studied these format specifiers and it takes few hours to complete this article.

I would like to show these using table and give an example to each format specifier by explain them.

char

Format  Description  Type              range         Uses
%c Character char -128 to 128 Use to output
single character
%c Character unsigned char 0 to 255 Use to output
single character
%s String char [] — Use to output any
range of strings

Examples


#include <stdio.h>
int main()
{
char letter1 = ‘a’;
unsigned char letter2 = ‘a’;

char *words1 = “Hello World!”;
char words2[] = “Hello Guys!”;

printf(“letter 1 = %c”, letter1); //Output only one character
printf(“\nletter 2 = %c\n”, letter2);
printf(“\n%s”, words1); //Output any range of strings
printf(“\n%s”, words2);

letter1 = 255;
letter2 = 255;

printf(“\n\nchar = %d\n”, letter1); //Diferrence between char and unsigned char
printf(“Unsigned char = %d\n”, letter2);

return 0;
}

Output


letter 1 = a
letter 2 = a
Hello World!
Hello Guys!
char = -1
Unsigned char = 255

— — — — — — — — — — — — — — — — — — — — — — — — — —

Integer(short)


Format Type range(Uses)
%hd short int -32,768 to 32,767
%hu unsigned short int 0 to 65,535

Examples


#include <stdio.h>
int main()
{
short int min1 = -32768;
short int max1 = 32767;

unsigned short int min2 = 0;
unsigned short int max2 = 65535;

printf(“Minimum value of short int = %hd”, min1);
printf(“\nMaximum value of short int = %hd”, max1);

printf(“\n\nMinimum value of unsigned short int = %hu”, min2);
printf(“\nMaximum value of unsigned short int = %hu”, max2);

return 0;
}

Output


Minimum value of short int = -32768
Maximum value of short int = 32767
Minimum value of unsigned short int = 0
Maximum value of unsigned short int = 65535

— — — — — — — — — — — — — — — — — — — — — — — — — —

Integer

Format      Type            range                 Uses
%d int -2,147,483,648 to Can take integers
2,147,483,647 as decimals
%u unsigned int 0 to 4,294,967,295 Output only positive
numbers
%i unsigned int -2,147,483,648 to Can take integers as
2,147,483,647 decimals, hexadecimals
and octal s type.

Examples


#include <stdio.h>
int main()
{
int min1 = -2147483648;
int max1 = 2147483647;

unsigned int min2 = 0;
unsigned int max2 = 4294967295;

unsigned int min3 = -2147483648;
unsigned int max3 = 2147483647;

unsigned int decimal = 12;
unsigned int octal = 012;
unsigned int hexadecimal = 0X12;


printf(“Minimum value of int = %d”, min1);
printf(“\nMaximum value of int = %d”, max1);

printf(“\n\nMinimum value of unsigned int(u) = %u”, min2);
printf(“\nMaximum value of unsigned int(u) = %u”, max2);

printf(“\n\nMinimum value of unsigned int(i) = %i”, min3);
printf(“\nMaximum value of unsigned int(i) = %i”, max3);

printf(“\n\n12 in decimal format = %i”, decimal);
printf(“\n012 in octal format = %i”, octal);
printf(“\n0X12 in hexadecimal format = %i”, hexadecimal);

return 0;
}

Output


Minimum value of int = -2147483648
Maximum value of int = 2147483647
Minimum value of unsigned int(u) = 0
Maximum value of unsigned int(u) = 4294967295
Minimum value of unsigned int(i) = -2147483648
Maximum value of unsigned int(i) = 2147483647
12 in decimal format = 12
012 in octal format = 10
0X12 in hexadecimal format = 18

— — — — — — — — — — — — — — — — — — — — — — — — — —

Integer(long and long long)


Format Type range(Uses)
%ld or %li long int -2,147,483,648 to
2,147,483,647
%lu unsigned long int 0 to 4,294,967,295%lld or %lli long long int -9,223,372,036,854,775,808 to
9,223,372,036,854,775,807
(-(2⁶³) to (2⁶³)-1)
%llu unsigned long long int 0 to 18,446,744,073,709,551,615

Examples


#include <stdio.h>
int main()
{
long int min1 = -2147483648;
long int max1 = 2147483647;

unsigned long int min2 = 0;
unsigned long int max2 = 4294967295;

long long int min3 = -9223372036854775808; // 2⁶³
long long int max3 = 9223372036854775807; // 2⁶³ — 1

unsigned long long int min4 = 0;
unsigned long long int max4 = 18446744073709551615;
printf(“Minimum value of long int(ld) = %ld”, min1);
printf(“\nMaximum value of long int(ld) = %ld”, max1);

printf(“\n\nMinimum value of long int(li) = %li”, min1);
printf(“\nMaximum value of long int(li) = %li”, max1);

printf(“\n\nMinimum value of unsigned long int(lu) = %lu”, min2);
printf(“\nMaximum value of unsigned long int(lu) = %lu”, max2);

printf(“\n\nMinimum value of long long int(lld) = %lld”, min3);
printf(“\nMaximum value of long long int(lld) = %lld”, max3);

printf(“\n\nMinimum value of long long int(lli) = %lli”, min3);
printf(“\nMaximum value of long long int(lli) = %lli”, max3);

printf(“\n\nMinimum value of unsigned long long int(llu) = %llu”, min4);
printf(“\nMaximum value of unsigned long long int(llu) = %llu”, max4);

return 0;
}

Output


Minimum value of long int(ld) = -2147483648
Maximum value of long int(ld) = 2147483647
Minimum value of long int(li) = -2147483648
Maximum value of long int(li) = 2147483647
Minimum value of unsigned long int(lu) = 0
Maximum value of unsigned long int(lu) = 4294967295
Minimum value of long long int(lld) = -9223372036854775808
Maximum value of long long int(lld) = 9223372036854775807
Minimum value of long long int(lli) = -9223372036854775808
Maximum value of long long int(lli) = 9223372036854775807
Minimum value of unsigned long long int(llu) = 0
Maximum value of unsigned long long int(llu) = 18446744073709551615

— — — — — — — — — — — — — — — — — — — — — — — — — —

Float, Double and Long Double


Format Type Range Uses
%f float 1.2E-38 to 3.4E+38 Use when number has
6 decimal places
%lf double 2.3E-308 to 1.7E+308 Use when number has
15 decimal places
%Lf long Double 3.4E-4932 to 1.1E+4932 Use when number has
19 decimal places
%e or %E float, double Scientific notation
of float values
%g or %G float, double Scientific notation
of float values
Accept integers and
float double also

Examples


#include <stdio.h>
int main()
{
float number1 = 5.12;
double number2 = 5;
long double number3 = 5;

printf(“Float number is(f) = %f”, number1);
printf(“\nDouble number is(lf) = %lf”, number2);
printf(“\nLong Double number is(Lf) = %Lf”, number3);

printf(“\n\nFloat number is(e) = %e”, number1);
printf(“\nDouble number is(e) = %e”, number2);

printf(“\n\nFloat number is(E) = %E”, number1);
printf(“\nDouble number is(E) = %E”, number2);

printf(“\n\nFloat number is(g) = %g”, number1); //Support for both integers and floats, doubles
printf(“\nDouble number is(g) = %g”, number2);

printf(“\n\nFloat number is(G) = %G”, number1); //Support for both integers and floats, doubles
printf(“\nDouble number is(G) = %G”, number2);

return 0;
}

Output


Float number is(f) = 5.120000
Double number is(lf) = 5.000000
Long Double number is(Lf) = 5.000000
Float number is(e) = 5.120000e+00
Double number is(e) = 5.000000e+00
Float number is(E) = 5.120000E+00
Double number is(E) = 5.000000E+00
Float number is(g) = 5.12
Double number is(g) = 5
Float number is(G) = 5.12
Double number is(G) = 5

— — — — — — — — — — — — — — — — — — — — — — — — — —

Other Specifiers

Format     Description       Type                      Uses
%o Integer short int, Octal representation
unsigned short int of Integer.
int, unsigned int,
long int

%x or %X Integer short int, Hexadecimal
unsigned short int representation of an
int, unsigned int, Integer.
long int

%p Void * void * Use to get address of
pointer or any other
variable.
Address of pointer to
void void *

%n — — Prints nothing.It
cause printf() to
load the variable
pointed by
corresponding
argument. The loading
is done with a value
which is equal to the
number of characters
printed by printf()
before the occurrence
of %n.

%% — — Prints % character.

Examples


#include <stdio.h>
int main()
{
int num1 = 65;
int num2 = 67;
int num3 = 15;

int* ptr =&num3;

int num4;
int num5 = 20;

printf(“Octal representation of 65 = %o”, num1);
printf(“\nOctal representation of 67 = %o”, num2);

printf(“\n\nHexadecimal representation of 15(x) = %x”, num3);
printf(“\nHexadecimal representation of 15(X) = %X”, num3);

printf(“\n\nAddress of pointer = %p”,ptr);

printf(“\n\nThe value of %nnum4 and %nnum5 is = “, &num4, &num5); //Uses of %n
printf(“%d %d”, num4, num5);

printf(“\n\nI got 50%% discount when I was shopping”); //Uses of %%

return 0;
}

Output


Octal representation of 65 = 101
Octal representation of 67 = 103
Hexadecimal representation of 15(x) = f
Hexadecimal representation of 15(X) = F
Address of pointer = 0x7ffe402adda4The value of num4 and num5 is = 15 24I got 50% discount when I was shopping

I hope this will help who are stuck with format specifiers in C. If I have done any mistakes please mentioned that.

--

--

Software Engineer at Kodez | Former Research Analyst at CoinGuru | Specialize in Data Science