×

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

10 Best IDEs for C or C++ Developers in 2024

Nobody can deny the fact that C and C++ were the first programming languages used by significant developers worldwide. Even now, newcomers who want to start programming are most frequently...

6 minutes read.

Attributes in C

Attributes are one of modern C++ key features. It is the process by which initiator can add more information to the language instead of introducing new keywords for every attribute....

4 minutes read.

C Language Environment Setup

To compile C program, we must have GCC compiler installed on our machine. In this C tutorial, all the examples are compiled and tested using GCC compiler. Although we can...

3 minutes read.

Tower of Hanoi in C

What is Tower of Hanoi? The Tower of Hanoi is a gaming problem that was created in 1883 by a French mathematician named Édouard Lucas. The Tower of Hanoi temple in...

4 minutes read.

Beep() function in C

Introduction: Beep is a function which is used in C programming language. The beep function uses to make a beep sound. In the C language, when we want to generate...

3 minutes read.

DSA Program in C

How is a Data Structure Implemented? The foundational terms of a data structure are the following terms. Data may be arranged using data structures to make it easier to use. Each data...

20 minutes read.

Find occurrence of substring in C using function

Introduction: In this article, we discuss finding the occurrence of a substring in C using function. This software takes strings and substrings as input and counts the occurrences of substrings within...

3 minutes read.

Flexible Array Members in a Structure in C

There is flexibility to declare array from c99 generation onwards that declaration of the collections can be made possible without even mentioning its dimension, which means that the displays are...

4 minutes read.

Builtin functions of GCC compiler

Certain built-in functions are present in the GCC compiler. These features are as follows: Function_ built in_popcount (x) The number of 1s in data of the integer type can be counted using...

3 minutes read.

Length of an Array Function in C

In C, there is no built-in function to get the length of an array. However, there are a few ways you can determine the length of an array. It is necessary...

15 minutes read.

String Handling functions in C

String :- The String is the collection of characters. Every String ends with the null character, and the String is enclosed in the double quotations .ie, "javaTpoint". If we see any...

5 minutes read.

C program to Store Information of Students Using Structure

What is the Structure in C? User-defined data types include structures. Structures aid your ability to combine things of various categories into a single group. Like arrays, it operates similarly. A...

3 minutes read.

Prime Number code in C

Prime Number Programs in C language In this tutorial, we will learn how to check whether the given number is a prime number or not, and how to print all the...

4 minutes read.

Double Specifier in C

What is a double specifier? The term double denotes the double data type in C. It more accurately depicts floating point numbers. The idea that it has twice the precision of...

3 minutes read.

Else If Ladder in C

What is Else If Ladder: When there are multiple options and a user has to decide from the options, we use Else If Ladder. Basically, it is an extension of if...

3 minutes read.

C Math Library

C Math Library The <math.h> header defines various mathematical functions and one macro. Many functions are available in this library to take double as an argument and then return double as...

4 minutes read.

Bit Stuffing Program in C

What is a Bit Stuffing? The process of inserting one or more extra bits (0) into data is known as bit stuffing. To provide signalling information to a recipient, these are...

3 minutes read.

Types of Array in C

What is an Array? One value can be stored in a variable at once. How many variables will you need if you have 100 values? Well, the answer isn't 100. It...

14 minutes read.

Compilation Process in C language

What is the Compilation Process?  The compilation is a method whereby the source code is converted into object code. It is achieved with compiler assistance. The compiler tests the source code for syntactic...

3 minutes read.

Static function in C

The functions in the C programming language are by default global. This means the programmer can easily access the function which is outside from the file where it was initially...

3 minutes read.