- C Programming Tutorial
- C - Home
- Basics of C
- C - Introduction
- C - Features
- C - Basics
- C - History
- C - Structure of C Program
- 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 - String Functions
- C - Static Keyword
- C - Random Number Generation
- C - Command Line Arguments
C Programming - C Format Specifiers
![]() Share with a Friend |
C Programming - C Format Specifiers
C Format Specifiers
Format specifiers in C are used in input/output functions like printf() and scanf() to specify the type of data to be displayed or read. They provide a way to define how variables should be formatted or interpreted.
List of Format Specifiers
- Integer Format Specifiers
| Specifier | Description | Example |
|---|---|---|
|
%d |
Signed decimal integer |
123, -45 |
|
%i |
Signed decimal integer (same as %d) |
123, -45 |
|
%u |
Unsigned decimal integer |
123 |
|
%o |
Unsigned octal |
017 |
|
%x |
Unsigned hexadecimal (lowercase letters) |
0x1a |
|
%X |
Unsigned hexadecimal (uppercase letters) |
0x1A |
- Floating-Point Format Specifiers
| Specifier | Description | Example |
|---|---|---|
|
%f |
Decimal floating-point number |
3.14, -2.718 |
|
%e |
Scientific notation (lowercase) |
2.5e+3 |
|
%E |
Scientific notation (uppercase) |
2.5E+3 |
|
%g |
Automatically selects %f or %e |
3.14, 2.5e+3 |
|
%G |
Automatically selects %f or %E |
3.14, 2.5E+3 |
|
%a |
Hexadecimal floating-point (lowercase) |
-0x1.91eb8p+1 |
|
%A |
Hexadecimal floating-point (uppercase) |
-0X1.91EB8P+1 |
- Character and String Format Specifiers
| Specifier | Description | Example |
|---|---|---|
|
%c |
Single character |
'A' |
|
%s |
String |
"Hello" |
- Pointer Format Specifier
| Specifier | Description | Example |
|---|---|---|
|
%p |
Memory address (pointer) |
0x7ffde0c8 |
- Special Format Specifiers
| Specifier | Description | Example |
|---|---|---|
|
%% |
Prints a literal % character |
% |
Modifiers for Format Specifiers
Modifiers are used to specify the width or size of the data to be read or displayed.
Length Modifiers
| Modifier | Description | Example Specifier |
|---|---|---|
|
h |
Short integer |
%hd, %hu |
|
l |
Long integer |
%ld, %lu, %lf |
|
ll |
Long long integer |
%lld, %llu |
|
L |
Long double |
%Lf |
|
z |
Size of size_t |
%zu |
Width and Precision
- Width: Minimum number of characters to print.
- Precision: Specifies the number of digits after the decimal point for floating-point values.
Syntax:
C
%[flags][width][.precision][length]specifier
Example:
C
printf("%10d", 42); // Prints 42 with a width of 10 (right-aligned)
printf("%.2f", 3.14159); // Prints 3.14 (2 digits after decimal)
Examples of Usage
- Integer
C
int num = 123;
printf("Decimal: %d\n", num);
printf("Octal: %o\n", num);
printf("Hexadecimal: %x\n", num);
- Floating-Point
C
float pi = 3.14159;
printf("Value of pi: %.2f\n", pi); // Prints pi up to 2 decimal places
- Character and String
C
char ch = 'A';
char str[] = "Hello, World!";
printf("Character: %c\n", ch);
printf("String: %s\n", str);
- Pointer
C
int num = 42;
printf("Memory address: %p\n", &num);
Example Program
C
#include <stdio.h>
int main() {
int num = 42;
float pi = 3.14159;
char ch = 'A';
char str[] = "Hello";
printf("Integer: %d\n", num);
printf("Floating-point: %.2f\n", pi);
printf("Character: %c\n", ch);
printf("String: %s\n", str);
printf("Pointer address: %p\n", &num);
return 0;
}
Output:
Integer: 42
Floating-point: 3.14
Character: A
String: Hello
Pointer address: 0x7ffeec8c
Key Points
- Use the appropriate specifier for the variable type to ensure proper formatting.
- Use precision and width modifiers to control the output appearance.
- Always match the data type of the variable with the corresponding specifier in printf() or scanf(). Mismatched types can lead to undefined behavior.
