Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
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.
How many types of format specifiers are there in C?
6.2.
What is %n format specifiers in C?
6.3.
What is %d %s %f in C?
6.4.
What is %d and %i in C?
7.
Conclusion
Last Updated: Mar 27, 2024
Easy

Format Specifiers in C

Author Surbhi Sharma
0 upvote
Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM

Introduction

C is a well-liked programming language renowned for its solid and practical features. It is often employed in the creation of several applications and systems. Format specifiers are one of C's key components. This blog will walk you through learning format specifiers in C and how to use them in regular coding practices.

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

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

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;
}

 

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;
}

 

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;
}

 

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;
}

 

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;
}

 

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;
}

 

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;
}

 

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;
}

 

Output

Address Printing Format Specifier - %p

Frequently Asked Questions

How many types of format specifiers are there in C?

In C, several format specifiers determine how data should be formatted when using functions like printf and scanf. The format specifiers %d for integers, %f for floating-point values, %s for strings, %c for characters, and others are frequently used.

What is %n format specifiers in C?

The %n format specifier in C is used to get the number of characters written in the output. It is typically used to calculate the length of the output string.

What is %d %s %f in C?

In C, you can print or read numbers by using the format specifier %d.  For strings (arrays of characters), the format specifier %s is used in C.  In C, the format specifier %f is used to floating-point numbers, such as decimals.

What is %d and %i in C?

In C, the format specifiers %d and %i are both used to read and output integers. In most instances, they are equivalent. Integers in other forms, such as octal (if prefixed with 0) and hexadecimal (if prefixed with 0x), can be represented by %i instead of %d, which is more frequently used for decimal integer values.

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 -

Happy Coding!

Live masterclass