- C - Home
- C - Overview
- C - Features
- C - History
- C - Standards
- C - Environment Setup
- C - Program Structure
- C - Hello World
- C - Compilation Process
- C - Comments
- C - Basic Syntax
- C - User Input
- C - printf Function
- C - Format Specifiers
- Lexical Elements in C
- C - Tokens
- C - Keywords
- C - Identifiers
- Variables and Constants
- C - Variables
- C - Constants
- C - Const Qualifier
- C - Linkage
- Data Types and Type Conversions
- C - Data Types
- C - Literals
- C - Escape Sequences
- C - Booleans
- C - Integer Promotions
- C - Character Arithmetic
- C - Type Conversion
- C - Type Casting
- Operators in C
- C - Operators
- C - Arithmetic Operators
- C - Unary Operators
- C - Relational Operators
- C - Logical Operators
- C - Bitwise Operators
- C - Assignment Operators
- C - Increment and Decrement Operators
- C - Ternary Operator
- C - sizeof Operator
- C - Operator Precedence
- C - Miscellaneous Operators
- Decision Making & Control Statements
- C - Decision Making
- C - if Statement
- C - if...else Statement
- C - if...else if Ladder
- C - Nested if Statements
- C - Switch Statement
- C - Nested Switch Statements
- C - Switch Case Using Range
- Loops in C
- C - Loops
- C - For Loop
- C - While Loop
- C - Do...while Loop
- C - For Loop vs While Loop
- C - Nested Loop
- C - Infinite Loop
- C - Break Statement
- C - Continue Statement
- C - Goto Statement
- Functions in C
- C - Functions
- C - Function Prototype
- 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
- C - Predefined Identifier __func__
- 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
- C - Dynamic Arrays
- Strings in C
- C - Strings
- C - Array of Strings
- C - Character Arrays
- C - Special Characters
- Pointers in C
- C - Pointers
- C - Initialization of Pointer Arrays
- C - Applications of Pointers
- C - Dereference Pointer
- C - NULL Pointer
- C - void Pointer
- C - Const Pointers & Pointer to Const
- C - Dangling Pointers
- C - Pointer Arithmetics
- C - Pointers and Arrays
- C - Pointer vs Array
- C - Pointer to an Array
- C - Array of Pointers
- C - Pointers vs. Multi-dimensional Arrays
- C - Pointer to Pointer
- C - Chain of Pointers
- C - Character Pointers and Functions
- C - Passing Pointers to Functions
- C - Return Pointer from Functions
- C - Function Pointers
- C - Array of Function Pointers
- C - Pointers to Structures
- C - Near, Far and Huge Pointers
- C - Restrict Keyword
- User-Defined Data Types
- C - Structures
- C - Structures and Functions
- C - Arrays of Structures
- C - Self-Referential Structures
- C - Dot (.) Operator
- C - Lookup Tables
- C - Enumeration (or enum)
- C - Structure Padding and Packing
- C - Nested Structures
- C - Anonymous Structure and Union
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Flexible Array Members in Structures
- C - Structures vs Unions
- Memory Management in C
- C - Memory Layout
- C - Memory Management
- C - Memory Address
- C - Storage Classes
- C - Dynamic Array Resizing
- C - Memory Leaks
- File Handling in C
- C - File Handling
- C - Input & Output
- C - File Operations
- C - Formatted Output
- C - getc, getchar, getch, getche
- Preprocessors in C
- C - Preprocessors
- C - Pragmas
- C - Macros
- C - Working of Preprocessor
- C - Preprocessor Operators
- C - Header Files
- C - Custom Header Files
- 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
- C - Online Compiler
Formatted Output in C
In the C programming language, output functions perform the essential task of displaying or storing information in a well-formatted manner. The Standard Library <stdio.h> provides functions that allow programmers to control how data is presented. Among these, the three most commonly used functions are −
- printf() − Displays formatted output on the standard output (console).
- sprintf() − Stores formatted output in a string buffer (memory).
- fprintf() − Writes formatted output to a file stream.
Although these functions share a common structure and formatting style, each serves a distinct purpose depending on the output target or destination. First, we will look at the common format specifiers used in these functions, and then we will discuss each function in detail with examples.
Following are the common format specifiers −
-
%dor%i: Integer -
%f: Floating-point number -
%c: Character -
%s: String -
%xor%X: Hexadecimal -
%o: Octal -
%%: Literal percent sign
printf() - Printing to the Console
In C, printf is the most commonly used function to display information on the standard output device (usually the terminal or console). It is used for debugging, user interaction, and displaying program results.
The syntax of the printf() function is as follows −
int printf(const char *format, ...);
Here, format is a string that contains text and format specifiers, which are placeholders for the values to be printed. The ellipsis (...) indicates that the function can take a variable number of arguments corresponding to the format specifiers.
Example of using printf()
In this C example, we demonstrate the working of the printf() function −
#include <stdio.h> int main() { int age = 25; float height = 5.8; char grade = 'A'; char name[] = "Aman Kumar"; printf("Name: %s\n", name); printf("Age: %d years\n", age); printf("Height: %.1f feet\n", height); printf("Grade: %c\n", grade); return 0; } Following is the output of the code −
Name: Aman Kumar Age: 25 years Height: 5.8 feet Grade: A
sprintf() - Storing in a String Buffer
In C, sprintf() function is used to format and store a series of characters in a string buffer. It is similar to printf(), but instead of printing to the console, it writes the formatted output to a character array (string).
The syntax of the sprintf() function is as follows −
int sprintf(char *str, const char *format, ...);
Here, str is a pointer to the character array where the formatted string will be stored, format is a string containing text and format specifiers, and the ellipsis (...) indicates that the function can take a variable number of arguments corresponding to the format specifiers.
Example of using sprintf()
In this C example, we demonstrate the working of the sprintf() function −
#include <stdio.h> int main() { int age = 25; float height = 5.8; char grade = 'A'; char name[] = "Aman Kumar"; char buffer[100]; sprintf(buffer, "Name: %s\nAge: %d years\nHeight: %.1f feet\nGrade: %c\n", name, age, height, grade); // Print the formatted string stored in buffer printf("%s", buffer); return 0; } Following is the output of the code −
Name: Aman Kumar Age: 25 years Height: 5.8 feet Grade: A
fprintf() - Writing to a File
In C, fprintf() function is used to format and write a series of characters to a file stream. It is similar to printf(), but instead of printing to the console, it writes the formatted output to a file.
The syntax of the fprintf() function is as follows −
int fprintf(FILE *stream, const char *format, ...);
Here, stream is a pointer to a FILE object that identifies the file where the formatted output will be written, format is a string containing text and format specifiers, and the ellipsis (...) indicates that the function can take a variable number of arguments corresponding to the format specifiers.
Example of using fprintf()
In this C example, we demonstrate the working of the fprintf() function −
#include <stdio.h> int main() { FILE *file = fopen("output.txt", "w"); if (file == NULL) { printf("Error opening file!\n"); return 1; } int age = 25; float height = 5.8; char grade = 'A'; char name[] = "Aman Kumar"; fprintf(file, "Name: %s\n", name); fprintf(file, "Age: %d years\n", age); fprintf(file, "Height: %.1f feet\n", height); fprintf(file, "Grade: %c\n", grade); fclose(file); return 0; } This code will create a file named output.txt with the following content.
Name: Aman Kumar Age: 25 years Height: 5.8 feet Grade: A
Conclusion
In this article, we have discussed the formatted output functions in C programming language. These functions are essential for displaying and storing information in a structured manner. By using format specifiers, we can control how different data types are represented in the output. The printf(), sprintf(), and fprintf() functions provide flexibility in directing output to various destinations, such as the console, memory buffers, or files. Understanding these functions is crucial for effective C programming and data presentation.