×

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

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.

Dos.h Header File in C Language

Dos.h is a header file in C. Interrupt handling, sound generation, date and time functions, and other tasks can be performed using the functions in this library. This is exclusive to...

4 minutes read.

gets() function in C

gets() is a pre-define or built-in function present in the stdio.h header file. stdio stands for standard input and output. gets() function is used to read a stream and store...

4 minutes read.

SJF Scheduling Program in C

The SJF (shortest job first) or the shortest job next is the programming scheduling in the C. It is one of the CPU scheduling programming. The SJF is defined as...

4 minutes read.

fopen() function in C

fopen() function, is one of the file handling functions. It is used to open the existing file and perform operations on the file. If the file does not exist, it...

3 minutes read.

Merge Sort in C

The merge sort follows the principle of the divide and conquers algorithm. Firstly it divides the input of an array into two halves and then calls itself for the two...

4 minutes read.

Comments in C

Comments are used to comment on the line of code in the program. Comments are a way of inserting remarks and reminders into code without affecting its behavior. The compiler...

1 minute read.

Find a subarray with a given sum.

Find a subarray with a given sum. The simple solution is to recognize all subarrays one by one and to check each subarray's sum. The quick solution follows the following program. Algorithm: From...

4 minutes read.

Commenting in C

Commenting in C Commenting in the C language is used to give out the information about the lines of the code which are included. It is one of the things that...

3 minutes read.

Example of Iteration in C

The iterations in the C language are the statements which are executed number of times until a certain condition is reached. These iterations are regarded as “Loops” in C language....

3 minutes read.

Difference between Scope and Lifetime in C

In this article, we will discuss about the key differences between scope and lifetime in C, including their definitions, applications, and how they relate to one another. By the end...

8 minutes read.

Data Structures And Algorithms in C

C language is one of the most flexible and simple languages. So, it is always better to learn any concept in C language compared to other languages. Let us look...

13 minutes read.

C program that does not Suspend when Ctrl+Z is Pressed

In Programming when a program breakdown and runs surprisingly in the terminal compiler the developer has the ability to prevent the program from running unequivocally. For expressly halting the program,...

3 minutes read.

Errors in C

Errors in C Errors are nothing but problems or faults that pretty much occur in all the programming languages. Errors make the behavior of the program seem abnormal, and even the...

4 minutes read.

Caesar Cipher Program in C

What is Caesar Cipher? The Caesar Cipher is a type of substitution cipher that is named after Julius Caesar, who is said to have used it to encrypt messages sent to...

2 minutes read.

Array Of Structures in C

The C programming language allows us to store multiple elements of the same type in arrays. We can use strings to store multiple elements of a character data type. Still,...

4 minutes read.

Exit control loop in C

To examine the termination condition for an exit, we usually use an exit control loop in C. If the evaluation for termination condition results in true, then the control would...

3 minutes read.

How to lock top row in Excel

How to lock top row in excel While working with an Excel spreadsheet, the user utilizes the rows and columns to enter the details under various headings. Sometimes while scrolling the...

4 minutes read.

Simple Programs in C Language

In this article, we will discuss the basic programs in C language. Addition of two values in C. Swapping two values. Finding the odd or even values. Finding vowels and consonants in C. Finding the...

11 minutes read.

Java Array vs ArrayList

As we all know, arrays are linear data structures that allow you to add elements to them continuously in memory address space, whereas ArrayList is a Collection framework class. Despite...

4 minutes read.