- C Programming Tutorial
- C - Home
- Basics of C
- C - Overview
- C - Features
- C - History
- C - Environment Setup
- C - Program Structure
- C - Hello World
- C - Compilation Process
- C - Comments
- C - Tokens
- C - Keywords
- C - Identifiers
- C - User Input
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Integer Promotions
- C - Type Conversion
- C - Type Casting
- C - Booleans
- Constants and Literals in C
- C - Constants
- C - Literals
- C - Escape sequences
- C - Format Specifiers
- Operators in C
- C - Operators
- C - Arithmetic Operators
- C - Relational Operators
- C - Logical Operators
- C - Bitwise Operators
- C - Assignment Operators
- C - Unary Operators
- C - Increment and Decrement Operators
- C - Ternary Operator
- C - sizeof Operator
- C - Operator Precedence
- C - Misc Operators
- Decision Making in C
- C - Decision Making
- C - if statement
- C - if...else statement
- C - nested if statements
- C - switch statement
- C - nested switch statements
- Loops in C
- C - Loops
- C - While loop
- C - For loop
- C - Do...while loop
- C - Nested loop
- C - Infinite loop
- C - Break Statement
- C - Continue Statement
- C - goto Statement
- Functions in C
- C - Functions
- C - Main Function
- C - Function call by Value
- C - Function call by reference
- C - Nested Functions
- C - Variadic Functions
- C - User-Defined Functions
- C - Callback Function
- C - Return Statement
- C - Recursion
- Scope Rules in C
- C - Scope Rules
- C - Static Variables
- C - Global Variables
- Arrays in C
- C - Arrays
- C - Properties of Array
- C - Multi-Dimensional Arrays
- C - Passing Arrays to Function
- C - Return Array from Function
- C - Variable Length Arrays
- Pointers in C
- C - Pointers
- C - Pointers and Arrays
- C - Applications of Pointers
- C - Pointer Arithmetics
- C - Array of Pointers
- C - Pointer to Pointer
- C - Passing Pointers to Functions
- C - Return Pointer from Functions
- C - Function Pointers
- C - Pointer to an Array
- C - Pointers to Structures
- C - Chain of Pointers
- C - Pointer vs Array
- C - Character Pointers and Functions
- C - NULL Pointer
- C - void Pointer
- C - Dangling Pointers
- C - Dereference Pointer
- C - Near, Far and Huge Pointers
- C - Initialization of Pointer Arrays
- C - Pointers vs. Multi-dimensional Arrays
- Strings in C
- C - Strings
- C - Array of Strings
- C - Special Characters
- C Structures and Unions
- C - Structures
- C - Structures and Functions
- C - Arrays of Structures
- C - Self-Referential Structures
- C - Lookup Tables
- C - Dot (.) Operator
- C - Enumeration (or enum)
- C - Structure Padding and Packing
- C - Nested Structures
- C - Anonymous Structure and Union
- C - Unions
- C - Bit Fields
- C - Typedef
- File Handling in C
- C - Input & Output
- C - File I/O (File Handling)
- C Preprocessors
- C - Preprocessors
- C - Pragmas
- C - Preprocessor Operators
- C - Macros
- C - Header Files
- Memory Management in C
- C - Memory Management
- C - Memory Address
- C - Storage Classes
- Miscellaneous Topics
- C - Error Handling
- C - Variable Arguments
- C - Command Execution
- C - Math Functions
- C - Static Keyword
- C - Random Number Generation
- C - Command Line Arguments
- C Programming Resources
- C - Questions & Answers
- C - Quick Guide
- C - Cheat Sheet
- C - Useful Resources
- C - Discussion
Format Specifiers in C
Format specifiers in C are certain special symbols used in the formatted console IO functions such as printf() and scanf(), as well as formatted file IO functions such as fprintf() and fscanf().
Format specifiers are formed of a predefined sequence of one or more alphanumeric characters followed by the % symbol. For example, %d, %s, %f, %lf, etc. are some of the format specifiers used in C.
Why Do We Use Format Specifiers in C?
The CPU performs IO operations with input and output devices in a streaming manner. The data read from a standard input device (for example, a keyboard) through the standard input stream is called stdin. Similarly the data sent to the standard output, which is the computer display screen, through the standard output device is called stdout.
The computer receives data from the stream in a text form, however you may want to parse it in variables of different data types such as int, float or a string. Similarly, the data stored in int, float or char variables has to be sent to the output stream in a text format. Format specifier symbols are used exactly for this purpose.
Format Specifiers in printf() Function
The printf() function is the most commonly used standard output function, defined in the stdio.h header file. The prototype of printf() function is as follows −
int printf(format_string, expr1, expr2, . . );
The first argument of this function is a string that is interspersed with one or more format specifiers. There may be one or more expressions as argument after the first. The compiler substitutes each format specifier with the value of its successive expression. The resultant formatted string is then passed to the output stream.
Example
In the following code, we have an int variable age and a float variable percent. The printf() function prints the values of both as below −
#include <stdio.h> int main(){ int age = 18; float percent = 67.75; printf("Age: %d \nPercent: %f", age, percent); return 0; }
Output
When you run the code, it will produce the following output −
Age: 18 Percent: 67.750000
The value of the first variable replaces the first format specifier %d. Similarly, %f is substituted by the value of the percent variable.
Format Specifiers in scanf() Function
Format specifiers are also used to parse the input stream into the variables of required type. The following example highlights how it's done.
Example
In this example, the program asks the user to input age and percent values. They are stored in the int and float variables, respectively.
#include <stdio.h> int main(){ int age; float percent; printf("Enter Age and Percent: \n"); scanf("%d %f", &age, &percent); printf("Age: %d Percent: %f", age, percent); return 0; }
Output
Run the code and check its output −
Enter Age and Percent: Age: 4096 Percent: 0.000000
Types of Format Specifiers
ANSI C defines a number of format specifiers. The following table lists the different specifiers and their purpose −
Format Specifier | Type |
---|---|
%c | Character |
%d | Signed integer |
%e or %E | Scientific notation of floats |
%f | Float values |
%g or %G | Similar as %e or %E |
%hi | Signed integer (short) |
%hu | Unsigned Integer (short) |
%i | Unsigned integer |
%l or %ld or %li | Long |
%lf | Double |
%Lf | Long double |
%lu | Unsigned int or unsigned long |
%lli or %lld | Long long |
%llu | Unsigned long long |
%o | Octal representation |
%p | Pointer |
%s | String |
%u | Unsigned int |
%x or %X | Hexadecimal representation |
A minus symbol (−) tells left alignment.
A number after % specifies the minimum field width. If a string is less than the width, it will be filled with spaces.
A period (.) is used to separate field width and precision.
Integer Format Specifiers
C uses %d for signed integer, %i for unsigned integer, %ld or %li for long integer, %o or %O for octal representation, and %x or %X for hexadecimal representation of an integer.
Example
The following example highlights how integer format specifiers are used in C −
#include <stdio.h> int main(){ int num = 20; printf("Signed integer: %d\n", num); printf("Unsigned integer: %i\n", num); printf("Long integer: %ld\n", num); printf("Octal integer: %o\n", num); printf("Hexadecimal integer: %x\n", num); return 0; }
Output
When you run this code, it will produce the following output −
Signed integer: 20 Unsigned integer: 20 Long integer: 20 Octal integer: 24 Hexadecimal integer: 14
Floating-point Formats
C uses the %f format specifier for single precision float number, %lf for double precision, %Lf for long double number. To represent a floating point number in scientific notation, C uses the %e or %E specifier symbol.
You can specify the width and the precision in the form of number of places after the decimal point. For example, to state the width of number to 4 digits with 2 digits after the decimal point, use the form %4.2f.
Example
Take a look at the following example −
#include <stdio.h> int main(){ float num = 5.347; printf("float: %f\n", num); printf("double: %lf\n", num); printf("Scientific notation: %e\n", num); printf("width and precision: %4.2f\n", num); return 0; }
Output
On running this code, you will get the following output −
float: 5.347000 double: 5.347000 Scientific notation: 5.347000e+000 width and precision: 5.35
String Formats
The char data type in C is actually a subset of int data type. Hence, a char variable with %c format specifier corresponds to the character in single quotes. On the other hand, if you use the %d specifier, then the char variable will be formatted to its ASCII value.
In C, a string is an array of char data. To display an array of chars, C uses the %s specifier.
Example
Take a look at the following example −
#include <stdio.h> int main(){ char ch = 'D'; char word[]="Hello World"; printf("As character: %c\n", ch); printf("As its ASCII value: %d\n", ch); printf("String format: %s", word); return 0; }
Output
When you run this code, it will produce the following output −
As character: D As its ASCII value: 68 String format: Hello World
Format Specifiers in File IO Functions
The stdio.h library defines the functions fscanf() and fprintf() for formatted IO with disk files, instead of standard input/output streams.
Example 1
The following code opens a file in write mode and saves the values of three variables in it.
#include <stdio.h> int main(){ int x,y,z; FILE *fp = fopen("test.txt","w"); x = 10; y = 20; z = 30; fprintf(fp, "%d, %d, %d", x,y,z); fclose(fp); return 0; }
Output
The fprintf() function uses the file represented by the file pointer fp to write the data.
The next example shows how you can open the same file in read mode to read the formatted data.
Example 2
The following program reads back the data from the file by opening it in read mode.
#include <stdio.h> int main(){ int x,y,z; FILE *fp = fopen("test.txt","r"); fscanf(fp, "%d, %d, %d", &x,&y,&z); printf("%d, %d, %d", x,y,z); fclose(fp); return 0; }
Output
The fscanf() function reads the formatted input from fp which is the pointer to the file opened. Here, you will get the following output −
10, 20, 30