Open In App

C++ <cstdio>

Last Updated : 23 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
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

DefinitionIt 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 MethodsInput 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 methodsBasic I/O methods have to be provided with a formatted string along with the argument listBasic I/O methods use insertion and extraction operators to output variables and take input to variables.
Data Type InfoMethods 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 MethodsInput and Output are generally faster than iostream.Input and Output are generally slower.
File handling and RedirectionFile 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.

Next Article
Article Tags :
Practice Tags :

Similar Reads