C Programming Tutorial

Format Specifiers

What are Format Specifiers?

Format Specifiers define the data type of the variable to be taken as input using scanf and printed as output using print in C programming.

Note:- The letters in format specifiers are shown in uppercase only for uppercase format specifiers. Otherwise, they can be used in either uppercase or lowercase.

Examples of Format Specifiers –

1) “%c” single-character specifier (char)

				
					#include <stdio.h> 
int main()
{ 
   char letter = 'A'; 
   printf("%c\n", letter); 
   return 0; 
} 
				
			

Output:-
A

2) “%s” string format specifier

				
					#include <stdio.h> 
int main()
{
   char str[] = "Coding is fun."; 
   printf("%s\n", str[]);
   return 0; 
}
				
			
Output:-
Coding is fun.

3) “%o” octal format specifier

				
					#include <stdio.h>
int main()
{
   int num = 31;
   printf("%o\n", num);
   return 0;
}
				
			

Output:-
37

4) “%d” and “%i” decimal integer format specifiers

				
					#include <stdio.h>
int main()
{
   int year = 2015, investment = 10000;
   printf("%d\n", year);
   printf("%i\n", investment); 
   return 0;
}
				
			

Output:-
2015
10000

5) “%f” and “%e” for float value and exponential notation

				
					#include <stdio.h> 
int main()
{
   float num = 19.99;
   printf("%f\n", num);
   printf("%e\n", num);
   return 0;
}
				
			

Output:-
19.990000
1.999000e+001

6) “%x” hexadecimal integer format specifier

				
					#include <stdio.h>
int main()
{
   int c = 28;
   printf("%x\n", c);
   return 0;
}
				
			

Output:-
1c

7) “%u” unsigned integer format specifier

				
					#include <stdio.h>
int main()
{
    printf("%u\n", -10);
    printf("%u\n", 10);
    return 0;
}
				
			

Output:-
4294967286
10

8) “%p” address printing

				
					#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: 000000000062FE1C

9) Additional Formatting

				
					#include <stdio.h>
int main()
{
	char str[] = "CodeWithGeeks";
	printf("%20s\n", str);
	printf("%-20s\n", str);
	printf("%20.5s\n", str);
	printf("%-20.5s\n", str);
	return 0;
}
				
			

Output:-
     CodeWithGeeks
CodeWithGeeks
          CodeW
CodeW

Some important points

    1. Left alignment is indicated by a minus(-) symbol.
    2. If the characters are fewer than the size of the width, the remaining space is filled with space, and if it is greater than, it is written as is without truncation.
    3. The precision and field width are separated by a period(.) sign.

Precision indicates the minimum number of digits in an integer, the maximum number of characters in a string, and the number of digits in a floating value after the decimal component.

The scanf function

Syntax

				
					scanf(char*format, arg1, arg2,...., argn)
				
			

Working

This function takes standard keyboard input and stores it in the appropriate variable. The number of objects successfully read is returned. The formal parameters arg1, agr2,…., argn must all be pointers.

Examples

1) Decimal Integer: “%d”

				
					#include <stdio.h>
int main()
{
	int a = 0;
	scanf("%d", &a);
	printf("%d\n", a);
	return 0;
}
				
			

Output:-
2
2

2) Octal or Hexadecimal Integer: “%i”

				
					#include <stdio.h>
int main()
{
	int a = 0;
	scanf("%i", &a);
	printf("%d\n", a);
	scanf("%i", &a);
	return 0;
}
				
			

Output:-
017
15
0xf

3) Double floating-point number: “%lf”

				
					#include <stdio.h>
int main()
{
	double a = 0.0;
	scanf("%lf", &a);
	printf("%lf\n", a);
	return 0;
}
				
			

Output:-
3.14
3.140000

4) String input: “%s”

				
					#include <stdio.h>
int main()
{
	char str[50];
	scanf("%s", str);
	printf("%s\n", str);
	return 0;
}
				
			

Input:-
CodeWithGeeks
Output:-
CodeWithGeeks

4) Character input: “%c”

				
					#include <stdio.h>
int main()
{
	char ch;
	scanf("%c", &ch);
	printf("%c\n", ch);
	return 0;
}
				
			

Output:-
S
S

Note:- In the case of hexadecimal numbers, when we take “%x” as the format specifier, there is no need to add 0x before the input number.

Minimum Field Width Specifier

If we want to show an output that takes up the least amount of screen real estate, it can be accomplished by putting an integer number after the percent sign in the format specifier.

Example

				
					#include<stdio.h>
int main()  
{  
   int x=900;  
   printf("%8d", x);  
   printf("\n%-8d",x);  
   return 0;  
}
				
			

Output:-
900
900

Here, “%8d” displays the value after 8 spaces, and “%-8d” aligns it to the left.

Specifying Precision

The Dot (.) operator can be used to specify value precision in a C compiler, followed by an integer and format specifier.

Example

				
					#include<stdio.h>
int main()  
{  
   float x=12.2;  
   printf("%.2f", x);  
   return 0;
}
				
			

Output:-
12.20

Here, “%.2f” specifies the value precision as 2, which means, the compiler will print the value taken as input up to 2 decimal places.

Difference between Format Specifiers and Escape Sequences

Escape Sequence

      1. It is a sequence of characters that represents a different meaning when used in a string literal.
      2. It remains fixed for most languages, like C, C++, Java, etc.
      3. It generally begins with the backslash (\) symbol.
      4. Examples are: \a, \b, \n, etc.

Format Specifier

      1. It is used to define various data types, to allow us to work with many types of data.
      2. It is usually fixed for the C language.
      3. It begins with the percent (%) symbol.
      4. Examples are: %d, %f, %c, etc.