The <cstdio> header file is a part of the C++ standard library collection that provides the input and output methods of <stdio.h> library of C language. We can use the traditional C-style input and output method in C++ using the <cstdio> library. It also contains all the functions and macros of <stdio.h> library of C language.
We can import <cstdio> library using #include preprocessor directive in our program.
Syntax:
#include <cstdio>
Example:
C++
// C++ program to demonstrate the use of <cstdio> header
// file
#include <cstdio>
using namespace std;
int main()
{
int number = 10;
// using printf() function of C language in C++
printf("Value of variable \"number\": %d", number);
return 0;
}
OutputValue of variable "number": 10
<cstdio> Library Functions
Input and output in C++ are done in the form of a sequence of bytes commonly known as streams. The stream can be of many types depending on where the data is being sent or received. For example,
- Standard Input Stream(stdin): The flow of data in this stream is from the standard input device(keyboard) to the memory.
- Standard Output Stream(stdout): The flow of data in this stream is from memory to the standard output device (monitor).
- File Stream: The flow of data is to or from the specified file.
<cstdio> library functions help us to manipulate these streams according to our needs. Following are all those functions with the streams that they manipulate.
Input and Output Functions
Input and output are done in <cstdio> by the printf() and scanf() functions and its variations.
Function Name | Function Description |
---|
printf() | Used for formatted output to the standard output stream(stdout). |
scanf() | Used to take formatted input from the standard input stream(stdin). |
vprintf() | works similarly to printf() but a variable argument list is used to provide arguments. |
vscanf() | works similarly to scanf() but a variable argument list is used to provide arguments. |
fprintf() | Used for formatted output to file stream. |
fscanf() | Used to take formatted input from the file. |
sprintf() | Formatted output to the specified string (array of characters). |
sscanf() | Formatted input from specified string (array of characters). |
vfprintf() | works similarly to fprintf() but a variable argument list is used to provide arguments. |
vfscan() | works similarly to fscanf() but a variable argument list is used to provide arguments. |
vsprintf() | works similarly to sprintf() but a variable argument list is used to provide arguments. |
vsscanf() | works similarly to sscanf() but a variable argument list is used to provide arguments. |
fwrite() | Used to output data to a stream in the form of data block. |
fread() | Used to take input from a stream in the form of data block. |
gets() | get a string from the standard output stream. |
puts() | put a string in the standard input stream. |
fgets() | get a string from the specified stream. |
fputs() | put a string to the specified stream. |
getc() | get a single character from the standard input stream. |
putc() | put a single character into the standard output stream. |
fgetc() | get a single character from the specified stream. |
fputc() | put a single character into the specified stream. |
File Operation Functions
Sometimes, we need to store output data or take input from an external file. In these cases we can use the following functions of <cstdio> library for doing operations on file:
Function Name | Function Description |
---|
fopen() | Used to open an external file. |
fclose() | Used to close an opened external file. |
ftell() | Used to tell the position of FILE pointer. |
rewind() | Used to set the FILE pointer to the start of the file. |
remove() | Used to delete the given file |
rename() | Used to rename the given file. |
fflush() | Flushes the specified stream. |
freopen() | Redirect already opened to another file or mode. |
setbuf() | Set stream buffer. |
setvbuf() | change stream buffer. |
tmpfile() | Open a temporary file. |
tmpnam() | Generate a temporary name. |
To know more about file-handling operations, click here.
Example:
C++
// C++ program to demonstrate use of cstdio functions
#include <cstdio>
using namespace std;
int main()
{
// using printf()
printf("This is printf() function\n");
// using fprintf() to print output in standard output
// device using stdin pointer
fprintf(stdout, "This is fprintf() function");
return 0;
}
OutputThis is printf() function
This is fprintf() function
Macros of <cstdio> library
Macros are the constants that are defined by the #define preprocessor directive. They are replaced by their values during preprocessing. To know more about macros, click here.
Below is the list of frequently used functions of <cstdio> library:
Macro Name | Macro Description |
---|
NULL | Null pointer constant. |
EOF | Value representing end-of-file. |
SEEK_CUR | Parameter used in fseek() function to specify displacement of FILE pointer from current position. |
SEEK_SET | Parameter used in fseek() function to specify displacement of FILE pointer from the start. |
SEEK_END | Parameter used in fseek() function to specify displacement of FILE pointer from the end. |
stdin | Represent standard input stream. |
stdout | Represent standard output stream. |
Example:
C++
// C++ program to print values of some constant macros
#include <cstdio>
using namespace std;
int main()
{
printf("Value of NULL: %d\n", NULL);
printf("Value of EOF: %d\n", EOF);
return 0;
}
OutputValue of NULL: 0
Value of EOF: -1
Note: Value of NULL and EOF are implementation defined and depends on the compiler vendor.
Difference between cstdio and iostream libraries
As we know that <iostream> header file contains the standard input and output methods of C++. So here is the list of major differences between <cstdio> and <iostream>
Parameters | <cstdio> Library | <iostream> Library |
---|
Definition | It is a standard C++ library that imports C-style input and output methods. | It is a standard C++ library that imports the C++ style input and output methods. |
I/O Methods | Input and output are accomplished using functions such as printf(), scanf(), etc. | Input and output are accomplished using objects such as cin, cout, cerr, etc. |
Namespace used | <cstdio> header file will always import all its identifiers in the std namespace and maybe in the global namespace. | <iostream> header file will always import all its defined identifiers in the std namespace only. |
API of methods | Basic I/O methods have to be provided with a formatted string along with the argument list | Basic I/O methods use insertion and extraction operators to output variables and take input to variables. |
Data Type Info | Methods of <cstdio> need data type info of the arguments to be entered manually as format specifiers. | Methods of <iostream> automatically detect the data type of the arguments provided. |
Speed of Methods | Input and Output are generally faster than iostream. | Input and Output are generally slower. |
File handling and Redirection | File handling can be done using functions such as fopen(), fscanf(), etc. and redirection can be done using freopen(). | We need to include <fstream> library for file handling and redirection purposes. |
Similar Reads
C++ <cstring>
The <cstring> library is a part of the standard C++ library collection that provides the commonly used methods for C-Style string manipulation. It is inherited from the <string.h> library of C language. We can import the <cstring> header file using #include preprocessor directive a
5 min read
C++ 11 - <cstdint> Header
<cstdint> header in C++ ensures the portability of integer types with specialized width and signedness across various systems. The absence of standardization for integer types caused issues in coding and constructing portable programs before the advent of. In this article, we will explore the
3 min read
C++ Std vs Stl
The full form of std is standard and it is a namespace. All the identifiers are declared inside the std namespace, in other words, a namespace provides scope to identifiers such as function names, variable names, etc. defined inside it. It is a feature especially available in C++ and is not present
4 min read
strol() function in C++
The strtol() function in C++ interprets the contents of a string as an integral number of the specified base and return its value as a long int.This function also sets an end pointer that points to the first character after the last valid numeric character of the string, if there is no such characte
3 min read
cout in C++
In C++, cout is an object of the ostream class that is used to display output to the standard output device, usually the monitor. It is associated with the standard C output stream stdout. The insertion operator (<<) is used with cout to insert data into the output stream.Let's take a look at
2 min read
C++ printf() Function
printf() function is originally declared under the <cstdio>header file. It prints the formatted string to the standard output stdout. Syntax: int printf(const char*word, .......) Parameters: word: represents the string that needs to be printed on the standard output stdout,....... : represents
3 min read
C String Functions
C language provides various built-in functions that can be used for various operations and manipulations on strings. These string functions make it easier to perform tasks such as string copy, concatenation, comparison, length, etc. The <string.h> header file contains these string functions.Th
6 min read
C++ STL Tutorial
âRecent Articlesâ on C++ STL ! âCoding Problemsâ on C++ STL ! The Standard Template Library (STL) is a set of C++ template classes to provide common programming data structures and functions such as vector, lists, stacks, etc. Standard Template Library (STL) Algorithms Introduction to STL Sorting Se
2 min read
C++17 - <charconv> Header
The C++ <charconv> header provides many functions for converting the character sequences to numerical values and vice-versa. It is considered better than the <cstdlib> header file functions for the same purpose. The functions provided by the <charconv> header file are generally fas
5 min read
C++ 11 - <cinttypes> Header
The header offers type aliases and functions for manipulating integer types with particular sizes and signedness, as a part of the C++11 standard. Its aim is to present a standard group of integer types across diverse platforms and architectures. <cinttypes> header The <cinttypes> header
2 min read