Open In App

C++ <cstdio>

Last Updated : 13 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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.

C++ cstdio

 

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;
}


Output

Value 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;
}


Output

This 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;
}


Output

Value 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.

Frequently Asked Questions (FAQs)

1. Should I use cstdio or iostream header file in C++?

While coding in C++, it is recommended to use <iostream> rather than <cstdio> because of the reasons mentioned above. <iostream> in C++ is specifically made to overcome the limitations of input and output methods of C language.

2. Can I use methods of both printf() / scanf() and cin / cout together in C++?

Yes, we can use methods of both printf() / scanf() (which are functions of cstdio) and cin / cout (objects of iostream) together as they are synchronized with each other by default and calls to the methods of both the libraries can be intermixed.

We can turn this synchronization off using this method. It will also increase the speed of iostream methods.

3. Difference between cstdio and stdio.h libraries

  • C language doesn’t support cstdio library. It is only part of standard C++. So if we want our program to run on both C and C++, we should use stdio.h header file.
  • When used in C++, <stdio.h> header file will import all its identifiers in the global namespace and maybe in the std namespace while <cstdio> will import all its identifiers in the std namespace and maybe in the global namespace (depending on the compiler vendor).

4. Should I use <cstdio> or <stdio.h> header file?

If we want our program to run both in C and C++ programming languages, then we should include <stdio.h> because the <cstdio> header file is only part of C++, not C language.



Like Article
Suggest improvement
Share your thoughts in the comments