
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
Format specifiers in C
The format specifiers are used in C for input and output purposes. Using this concept the compiler can understand that what type of data is in a variable during taking input using the scanf() function and printing using printf() function. Here is a list of format specifiers.
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 |
%n | Prints nothing |
%% | Prints % character |
These are the basic format specifiers. We can add some other parts with the format specifiers. These are like below −
A minus symbol (-) sign tells left alignment
A number after % specifies the minimum field width. If string is less than the width, it will be filled with spaces
A period (.) is used to separate field width and precision
Example
#include <stdio.h> main() { char ch = 'B'; printf("%c
", ch); //printing character data //print decimal or integer data with d and i int x = 45, y = 90; printf("%d
", x); printf("%i
", y); float f = 12.67; printf("%f
", f); //print float value printf("%e
", f); //print in scientific notation int a = 67; printf("%o
", a); //print in octal format printf("%x
", a); //print in hex format char str[] = "Hello World"; printf("%s
", str); printf("%20s
", str); //shift to the right 20 characters including the string printf("%-20s
", str); //left align printf("%20.5s
", str); //shift to the right 20 characters including the string, and print string up to 5 character printf("%-20.5s
", str); //left align and print string up to 5 character }
Output
B 45 90 12.670000 1.267000e+001 103 43 Hello World Hello World Hello World Hello Hello
We can use these format specifiers for the scanf() function also in the same manner. So we can take the input from scanf() like above how we have printed.
- Related Articles
- Space format specifiers in Java
- What are different format specifiers used in C language?
- C program to print characters without using format specifiers
- What are type specifiers in C++?
- What are access specifiers in C#.NET?
- The "E" and "e" custom specifiers in C#
- What are the different access specifiers in C#.NET?
- What are the differences between public, protected and private access specifiers in C#?
- Add grouping specifiers for large numbers in Java
- Format TimeSpan in C#
- C# Currency ("C") Format Specifier
- Get the drive format in C#
- String format for DateTime in C#
- String format for Double in C#
- Sortable ("s") Format Specifier in C#
