×

Format Specifier in C

Format Specifier in C

Format specifiers can be described as an operator that is used to print the data referred by any object or variable in combination with the printf () function. Without using the format specifiers, even if a value is stored in a specific variable, you cannot print the value stored in the variable.

By implementing these format specifiers in a printf () function, you can retrieve the data stored in the variables and print them onto the console screen. Format specifiers start with a percentage operator and then have a special character to identify the type of data.

List of format Specifiers in C

Format Specifier Description
%f Float format specifier
%c Character format specifier
%s String format specifier
%d Integer format specifier
%o Unsigned octal number for integer
%u Unsigned integer format specifier

Float Format Specifier

The %f format specifier is implemented to reflect the fractional values. This is implemented within the function printf () to print the fractional or floating value stored in the variable. Whenever you need to print any data that is fractional or floating, use the %f format specifier.

Syntax:

printf (“%f”, <variable name>);

Code:

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

Output:

Format Specifier in C

Character Format Specifier

To represent characters, the per cent c format specifier is implemented. This is used for printing the character stored in a variable with the function printf (). You should include the %c format specifier when you want to print character information.

Syntax:

printf (“%c”, <variable name>);

Code:

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

Output:

Format Specifier in C

String Format Specifier

For the representation of strings, the percent s format specifier is implemented. This is used for printing a string stored in the character array variable in printf function. The %s format specifier should be implemented when you need to print a string.

Syntax:

printf (“%c”, <variable name>);

Code:

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

Output:

Format Specifier in C

Integer Format Specifier

To represent integer values, the %d format specifier is implemented. It is used for printing the integer value stored in the variable with the function printf ().

Syntax:

printf (“%d”, <variable name>);

Code:

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

Output:

Format Specifier in C

Unsigned Octal Number for Integer

The specifier for the %o format is implemented to display integer values. This is implemented with function printf () to print the octal number integer value stored in the variable.

Syntax:

printf (“%ld”, <variable name>);

Code:

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

Output:

Format Specifier in C

Unsigned Integer Format Specifier

For fetching values from the address of a variable having unsigned decimal integer stored in the memory, the %u format specifier is implemented. This is used for printing the unsigned integer variable inside the printf () function.

Syntax:

printf (“%u”, <variable name>);

Code:

int main()  
 {   
   int a=40;  
   int c= -40;  
   printf("Value of b is:%u",a);  
   printf("\nValue of c is:%u",c);  
     return 0;  
 }   

Output:

Format Specifier in C

Related Topics

Conio.h in C

Header files are the source files with the extension of .h. In c language header files are referred to as the helping files and they contain definitions of various functions...

4 minutes read.

Star Program in C Language

Star Program in C Star patterns are a sequence of * or any other character used to construct any pattern or any like-square geometric form, triangle, hollow square, pyramid, rhombus, etc.  Many programmers worldwide highly...

4 minutes read.

Local Labels in C

Anyone who has written programs in the C programming language is required to be familiar with the "go to" and "labels" used in C to navigate between functions. "Local labels"...

4 minutes read.

Simple hash() function in C

Introduction In this context, we briefly discuss HASH FUNCTION, HASHING or HASH TABLE in C. It is a function used to map data and mapped arbitrary sizes to the fixed-size values. The...

7 minutes read.

Keywords in C

A keyword is a word that has a predetermined meaning. The C compiler knows the purposes of the keywords. The objectives of these keywords cannot be modified. Keywords are the...

9 minutes read.

Nested loop in C

Definition of Nested Loop A nested loop is generally used when we want to run a loop statement inside another loop statement. This kind of loop is also known as a...

3 minutes read.

Enumeration (Enum) in C

Enumeration (Enum) in C: In the C programming language, the enum is referred to as an enumerated type. Enum is a user defined data type consisting of integer values and...

3 minutes read.

Recursion in C

Recursion in C: In the C programming language, the concept known as recursion exists that is a technique in which a function calls itself either directly or indirectly. It allows...

4 minutes read.

Type qualifiers in C

Type qualifiers in C: In the C programming language, type qualifiers are the keywords that prepend to the variables to change their accessibility, i.e., we can tell that the type...

4 minutes read.

String in C

String is a collection of character or group of characters. In array, string of character is terminated by a null value “\0” and enclose between double quote. We can declare...

2 minutes read.

How to Merge Array in C?

Introduction Merging arrays is a common task for many developers but might be difficult for beginners of C programming. Fortunately, our guide will show you how to quickly merge arrays in...

6 minutes read.

While Loop in C programming examples

Introduction There is always a header file containing all the essential data regarding inputs and outputs of various functions in the C programs. The following statement describes how to use/add header file...

22 minutes read.

Sum of digits in C++

Sum of digits in C++ There are various ways to find the sum of the digits of a number in C++. We can use containers like arrays or other simple cases...

4 minutes read.

C program to compare the two strings

C program to compare the two strings Strings can be compared either by using the string function or without using string function. First, we will look at how we can compare the strings with...

4 minutes read.

Stack implementation in C

Stack implementation in C Stack stores the data in a particular order. It is a linear data structure that follows the principle of the Last In First Out (LIFO) technique where...

3 minutes read.

Pointer in C

Pointer is a variable that is used to contain the address of another variable. We can easily create a pointer in C language. Example: int *ptr Symbol Description & (ampersand sign) Address of an operator...

1 minute read.

Add two numbers using the function in C

Here we will learn how to add two numbers by creating function in C. Let’s learn this with help of example. Code: - #include <stdio.h> int add_two_no(int a, int b); int main(){   int first_num,...

1 minute read.

strtok() and strtok_r() functions in C with examples

strtok() and strtok_r() functions in C with examples C offer various strtok() and strtok r() methods for a string to be separated by a certain delimiter. Dividing a string is a...

2 minutes read.

Bigint (BIG INTEGERS) in C with Example

The maximum number of digits that a long, long int can have in C/C++ is 20. The issue is how to store the 22-digit number, which is difficult to do...

9 minutes read.

Fahrenheit to Celsius in C

Before going into conversion, we have to know what Fahrenheit and Celsius mean, these are both units to measure the temperature.In our daily life, we use both Fahrenheit and Celsius,...

1 minute read.