×

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

Array to function in C

Array to function in C We can create functions that receive an array as an argument. To pass an array into a function, we need to write the name of the...

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.

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 ()...

3 minutes read.

Self-referential structure in C

Self-referential structure in C A self-referential structure is a structure that can have members which point to a structure variable of the same type. They can have one or more pointers...

3 minutes read.

Addition of Matrix in C

The mathematical operation of includingor greater matrices is called the addition of matrices. A matrix is a square series of numbers, symbols, words, letters, and different things. In this article,...

3 minutes read.

String literals in C

Constants refer to the fixed values of the program that may not alter during the compiling ad execution. These fixed values are known as literals. In other words, the values...

3 minutes read.

Return array from function in C

Return array from function in C C programming does not require the return to a function of a whole array as an argument. However, you can return a pointer to an array without...

2 minutes read.

While-Loop in C

Syntax of While Loop The syntax that has been used for the while loop in C programming language is: while (termination condition) {   // the body of the loop  } Working of While-Loop Initially, we...

3 minutes read.

Fibonacci Series in C Using For Loop

In this C article, we will let you know about the procedure of displaying the Fibonacci series of the first “n” positive integers. The syntax of for loop used in the...

4 minutes read.

Pointer arithmetic in C

In the C programming language, a pointer is an address which stores a numeric value. Hence, a developer can perform several arithmetic operations on the same just as one does...

4 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.

How to measure time taken by a function in C?

Measuring the time taken by a function is quite a complex task, because numerous methods are frequently not transferable to other platforms, measuring the execution time of a C programs...

5 minutes read.

For loop in C Programming

In the C programming language, the for loop is used to repeatedly iterate over a set of instructions or a section of code. It is frequently used to traverse array-based...

3 minutes read.

Nested if-else statement in C

If we use an if-else statement within another if statement in a C program, it is called a nested if-else statement in C. It helps to check the condition inside...

3 minutes read.

Classification of Programming language

 Classification of Programming language  The programming language represents a set of instructions compiled along with each other to perform a specific task provided by the CPU (Central Processing Unit). A programming...

3 minutes read.

Inline Function in C

Inline Function in C A normal function will become an inline function when the function prototype of that function is prepended with the keyword "inline". Inline functions are the function where...

4 minutes read.

Kruskal algorithm in C

Given a weighted graph, Kruskal's algorithm generates a spanning tree with the lowest possible weights. Start by creating an edge list for the given graph, including the weights. Sort the...

3 minutes read.

Math functions in C

Math functions in C: In the C programming language, the compiler allows the developer to perform mathematical operations through the header’s functions, represented by <math.h>. The <math.h> header defines various mathematical...

3 minutes read.

File Operations in C

Why do I need the file? All data will be lost when the program exits. Saving data to a file keeps it safe even if the program stops working. If there...

6 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.