Table of contents
1.
Introduction
2.
What are Format Specifiers in C?
3.
Why Do We Use C Format Specifiers?
4.
List of Format Specifiers in C
5.
Examples of Format Specifiers in C
5.1.
1. Character Format Specifier – %c
5.1.1.
Output
5.2.
2. Integer Format Specifier (signed) – %d
5.2.1.
Output
5.3.
3. Unsigned Integer Format Specifier –  %u
5.3.1.
Output
5.4.
4. Floating Point Format Specifier – %f
5.4.1.
Output
5.5.
5. Octal Integer Format Specifier - %o
5.5.1.
Output
5.6.
6. Hexadecimal Integer Format Specifier - %x
5.6.1.
Output
5.7.
7. String Format Specifier – %s
5.7.1.
Output
5.8.
8. Address Printing Format Specifier - %p
5.8.1.
Output
6.
Frequently Asked Questions
6.1.
What are type specifiers in C?
6.2.
What is %f and %d in C?
6.3.
What is %d and %u in C?
7.
Conclusion
Last Updated: Sep 24, 2024
Easy

Format Specifiers in C

Author Surbhi Sharma
0 upvote

Introduction

In C programming, format specifiers are used in input/output functions like printf() and scanf() to specify the type and format of data being read or displayed. They are represented by a percentage sign (%) followed by a character indicating the data type, such as %d for integers, %f for floating-point numbers, and %s for strings. Format specifiers ensure proper interpretation and formatting of data during input and output operations. In this article, we will discuss these in detail like their different types with proper examples for better understanding.

Introduction

What are Format Specifiers in C?

Format specifiers are special characters used in C programming to indicate the kind and format of data that will be input into or produced from the program and combined with the C methods printf() and scanf(). 

They format variables' input and output. They assist in showing data in a specific format, improving the code's readability and comprehension. 

Format specifiers are denoted by percent (%) marks and are followed by a letter designating the formatted data type.

Why Do We Use C Format Specifiers?

Format Specifiers in C are used to receive inputs and print outputs of a certain data type. The symbol '%' is used in every format specifier. We utilise C format specifiers to inform the compiler of the type of data being printed or scanned. This is crucial because the compiler has to understand the proper input or output data formatting.

For example, telling the compiler that the printed data is an integer with the%d format specifier. The output will subsequently be formatted by the compiler as an integer with the required number of digits and, if necessary, a leading zero.

The compiler is informed that the printed data is a character via the %c format specifier. The result will then be formatted as a character with a single quotation around it by the compiler.

Also see, Floyd's Triangle in C

List of Format Specifiers in C

There are several format specifiers in C, and each specifier is used for a specific data type. Here are some of the commonly used format specifiers in C:

Format Specifier Name Used for Purpose
%d Decimal Integers Used to display decimal integers
%f Float Floating-point numbers Used to display floating-point numbers in decimal notation
%c Character Characters Used to display single characters
%s String Strings Used to display strings of characters
%i Integer Integers Used to display decimal integers (same as %d)
%o Octal Octal numbers Used to display integers in octal (base 8) notation
%x Hexadecimal Hexadecimal numbers Used to display integers in hexadecimal (base 16) notation
%lld or %llu Long decimal or unsigned long Long integers Used to display long or unsigned long integers in decimal notation
%u Unsigned Unsigned integers Used to display unsigned integers (non-negative integers)

Examples of Format Specifiers in C

To use format specifiers in C, you need to include them in the printf() and scanf() functions. For example, to print an integer variable using the printf() function, you can use the following code:

1. Character Format Specifier – %c

When printing a character in C, the%c format specifier is utilised. One of the most utilised format specifiers in C is this one.

The compiler is informed that the printed data is a character via the %c format specifier. The result will then be formatted as a character with a single quotation around it by the compiler.

#include <stdio.h>

int main() {
	char ch = 'A';
	printf("The character is: %c", ch);
	return 0;
}
You can also try this code with Online C Compiler
Run Code

 

Output

output of %c

2. Integer Format Specifier (signed) – %d

To print an integer in C, use the%d format specifier. One of the most utilised format specifiers in C is this one.

The compiler is informed that the printed data is an integer by the%d format specifier. The output will subsequently be formatted by the compiler as an integer with the required number of digits and, if necessary, a leading zero.

#include <stdio.h>

int main() {
	int num = 10;
	printf("The value of num is: %d", num);
	return 0;
}
You can also try this code with Online C Compiler
Run Code

 

Output

output of %d

3. Unsigned Integer Format Specifier –  %u

Unsigned integer values can be formatted and printed using the%u format specifier in C. When using the printf function to display the value of an unsigned integer variable, this format specifier is frequently used.


#include <stdio.h>
int main() {
   unsigned int unInt = 42;
   printf("The value of Unsigned int is: %u\n", unInt);
   return 0;
}
You can also try this code with Online C Compiler
Run Code

 

Output

Unsigned Integer Format Specifier –  %u

 

4. Floating Point Format Specifier – %f

In order to print a floating-point number in C, use the%f format specifier. One of the most utilised format specifiers in C is this one.

The compiler is informed that the printed data is a floating-point number by the format specifier %f. The compiler will subsequently format the output as a floating-point number with the correct amount of digits following the decimal point.

#include <stdio.h>

int main() {
	float num = 3.14159;
	printf("The value of num is: %f", num);
	return 0;
}
You can also try this code with Online C Compiler
Run Code

 

Output

output of %f

5. Octal Integer Format Specifier - %o

Octal (base 8) integer values in C are formatted and printed using the%o format specifier. This format specifier is generally used in conjunction with the printf function to show an integer variable's value in octal format.

Here is an illustration of how to use %o:

#include <stdio.h>
int main() {
   int Int = 23;
   printf("The value of given integer in octal is: %o\n", Int);
   return 0;
}
You can also try this code with Online C Compiler
Run Code

 

Output

Octal Integer Format Specifier - %o

6. Hexadecimal Integer Format Specifier - %x

To format and output integer values in hexadecimal (base 16) notation in C, use the%x format specifier. When using the printf function to display the value of an integer variable in hexadecimal form, this format specifier is frequently used.

Here is an example of how to use %x:

#include <stdio.h>
int main() {
   int var = 255;
   printf("The hexadecimal value is: %x\n", var);
   return 0;
}
You can also try this code with Online C Compiler
Run Code

 

Output

Hexadecimal Integer Format Specifier - %x

7. String Format Specifier – %s

To print a string in C, use the%s format specifier.  The compiler is informed that the printed data is a string by the format specifier %s. The compiler then formats the output as a string and is then contained in quotation marks.

#include <stdio.h>

int main() {
	char name[50];
	printf("Enter your name: ");
	scanf("%s", name);
	printf("Your name is %s\n", name);
	return 0;
}
You can also try this code with Online C Compiler
Run Code

 

Output

Output of %s

8. Address Printing Format Specifier - %p

Memory addresses in C can be formatted and printed using the%p format specifier. To display the address of a pointer variable or any other memory location, it is commonly combined with the printf function.

Here is an illustration of how to use %p:

#include <stdio.h>
int main() {
   int var = 24;
   int* ptr = &var;
   printf("Address of Variable: %p\n", (void*)ptr);
   return 0;
}
You can also try this code with Online C Compiler
Run Code

 

Output

Address Printing Format Specifier - %p

Frequently Asked Questions

What are type specifiers in C?

Type specifiers in C define the type of variables or functions, such as int, char, float, double, and are used in declarations.

What is %f and %d in C?

In C, %f is used to format floating-point numbers, while %d is used to format decimal integers in formatted output.

What is %d and %u in C?

In C, %d formats signed decimal integers, while %u formats unsigned decimal integers in output functions like printf.

Conclusion

With format specifiers, a crucial component of C programming, variables' input and output can be formatted in a particular way. The many format specifiers in C and ways in which we can utilize them were covered in this blog. Additionally, we looked at various C format specifier use examples.

Check out our other related articles -

Live masterclass