Category: C++ Input Output Operations

https://zain.sweetdishy.com/wp-content/uploads/2026/02/Input-Output-Operations.png

  • Manipulators in C++

    Manipulators in C++ are special functions or objects that are used with insertion (<<) and extraction (>>) operators (cin/cout) to control the input and output streams. You can change the format to display the output or how to read an input. The manipulators are declared in <iostream> and <iomanip> headers.

    Types of Manipulators in C++

    The manipulators can be classified into following categories −

    Output Stream Manipulators

    The output stream manipulators are used to control display properties such as field width, fill characters, and numeric display options. The below lists all the output stream manipulators −

    ManipulatorsDefinitionSyntax
    endlIt inserts a newline and flushes the output buffer.cout << “Tutorials” << endl;
    setw(n)It sets the field width for the next output operation.cout << setw(5) << 57;
    setfill()It sets the fill character.cout << setfill(‘*’) << setw(5) << 42;
    showpointIt displays decimal point for floating point numbers.cout << showpoint << 7.0;
    noshowpointIt hides the decimal point.cout << noshowpoint << 7.0;
    showposIt is used for displaying ‘+’ sign for positive numbers.cout << showpos << 57;
    noshowposIt hides ‘+’ sign for positive numbers.cout << noshowpos << 57;
    flushIt flushes the output stream without newline.cout << “Data” << flush;
    endsIt is used for inserting a null character and then flushes.cout << “String” << ends;

    Here is an example illustrating the use of some of the output stream manipulators in C++ −

    #include <iostream>#include <iomanip>usingnamespace std;intmain(){
    
        cout <<"C++ Output Manipulators Examples:"<< endl;
        cout <<"\nThis is the first line"<< endl;
        cout <<"This is the second line using endl"<< endl;
        cout << endl;
    
        cout <<setw(5)<<57<<" (Field width 5) using setw()"<< endl;
        cout <<setw(10)<<40<<" (Field width 10) using setw()"<< endl;
    
        cout <<"\n";
        cout <<setfill('*')<<setw(8)<<42<<" Filled with * using setfill()"<< endl;
        cout <<setfill('-')<<setw(8)<<123<<" Filled with - using setfill()"<< endl;
        cout <<setfill(' ');// Reset to space
    
        cout <<"\nDecimal Point Display:"<< endl;
        cout <<"With showpoint: "<< showpoint <<7.0<<", "<<3.0<< endl;
        cout <<"With noshowpoint: "<< noshowpoint <<7.0<<", "<<3.0<< endl;return0;}

    The output of the above code is as follows −

    C++ Output Manipulators Examples:
    
    This is the first line
    This is the second line using endl
    
       57 (Field width 5) using setw()
            40 (Field width 10) using setw()
    
    ******42 Filled with * using setfill()
    -----123 Filled with - using setfill()
    
    Decimal Point Display:
    With showpoint: 7.00000, 3.00000
    With noshowpoint: 7, 3
    

    Input Stream Manipulators

    The input manipulators control whitespaces and also handles input stream during read operations.

    ManipulatorsDefinitionSyntax
    wsIt removes the whitespace character.cin >> ws >> str;
    noskipwsIt avoids skipping whitespaces.cin >> noskipws >> ch;

    The following example shows the application of input stream manipulators ws and noskipws −

    #include <iostream>#include <sstream> usingnamespace std;intmain(){
        string text ="   A B";   
        cout <<"Original Text: "<< text << endl;
        
        istringstream iss(text);char ch;
    
        iss >> ws >> ch;
        cout <<"Using ws: '"<< ch <<"'"<< endl;// Reset stream
        iss.clear();
        iss.seekg(0);
        
        iss >> noskipws >> ch;
        cout <<"Using noskipws: '"<< ch <<"'"<< endl;return0;}

    The output of the above code is as follows. Here ws skips all the white spaces while noskipws prints the white space without skipping it.

    Original Text:    A B
    Using ws: 'A'
    Using noskipws: ' '
    

    Alignment Manipulators

    Alignment manipulators are used to control alignment of output within its field width.

    ManipulatorsDefinitionSyntax
    leftIt left aligns the output within field width.cout << left << setw(10) << “Hi”;
    rightIt right aligns the output within field width.cout << right << setw(10) << “Hi”;
    internalIt is used to insert padding between sign and value.cout << internal << setw(6) << -57;

    In this example, we have used left and right manipulators that aligns the number to left and right respectively while internal sets the alignment between ‘-‘ and number.

    #include <iostream>#include <iomanip>usingnamespace std;intmain(){int num =123;int negNum =-123;
    
        cout <<"Left aligned   : '"<< left <<setw(5)<< num <<"'"<< endl;
        cout <<"Right aligned  : '"<< right <<setw(5)<< num <<"'"<< endl;
        cout <<"Internal align : '"<< internal <<setw(5)<< negNum <<"'"<< endl;return0;}

    The output of the above code is as follows:

    Left aligned   : '123  '
    Right aligned  : '  123'
    Internal align : '- 123'
    

    Floating Point Manipulators

    Floating point manipulators are used to control the decimal precision and set the notation to represent the decimal numbers.

    ManipulatorsDefinitionSyntax
    setprecision(n)It sets the precision for decimal output. It specifies upto how many decimal points, we want the output.cout << setprecision(2) << 1.41421356;
    fixedIt represents the given number in fixed-point notation.cout << fixed << 1.41421356;
    scientificIt represents the given number in scientific notation.cout << scientific << 1234.5;

    The following example sets the precision of given number using setprecision(). The fixed and scientific gives result upto 6 decimal places if setprecision() manipulator is not used.

    #include <iostream>#include <iomanip>usingnamespace std;intmain(){double num =1.41421356;
        cout <<"Original number:"<< num << endl;
        cout <<"Default: "<<setprecision(5)<< num 
             << endl;
        cout <<"Fixed: "<< fixed <<setprecision(3)<< num << endl;
        cout <<"Scientific: "<< scientific <<setprecision(3)<< num << endl;return0;}

    The output of the above code is as follows:

    Original number:1.41421
    Default: 1.4142
    Fixed: 1.414
    Scientific: 1.414e+00
    

    Numeric Base and Case Manipulators

    The numeric base manipulators are used to convert and set the base of the given number. Case manipulators specifies whether you want your hex and scientific output in uppercase or lowercase.

    ManipulatorsDefinitionSyntax
    decIt sets the given value as decimal output.cout << dec << 57;
    octIt sets the given value as octal output.cout << oct << 57;
    hexIt sets the given value as hexadecimal output.cout << hex << 57;
    setbaseIt is used for setting the numeric base as decimal, octal, or hexadecimal in the output.cout << setbase(16) << 42;
    showbaseIt is used to display the base prefix in the output.cout << showbase << hex << 42;
    noshowbaseIt hides base prefix in the output.cout << noshowbase << hex << 42;
    uppercaseIt represents hex and scientific in uppercase letters.cout << uppercase << hex << 255;
    nouppercaseIt represents hex and scientific in lowercase letters.cout << nouppercase << hex << 255;

    The following example demonstrates the use of base and case manipulators where numeric base manipulators sets or converts the base of the given number into decimal, hex or oct. The case manipulator converts the given value into lower case and upper case.

    #include <iostream>#include <iomanip>usingnamespace std;intmain(){int number =255;
         cout <<"Original number:"<< number << endl;
    
         cout <<"Decimal: "<< dec << number <<", "<<"Octal: "<< oct << number <<", "<<"Hex: "<< hex << number << endl;// Reset to decimal
         cout << dec;
    
         cout <<"\nSetting base using setbase():"<< endl;
         cout <<"Base 10: "<<setbase(10)<< number <<", "<<"Base 8: "<<setbase(8)<< number <<", "<<"Base 16: "<<setbase(16)<< number << endl;
    
         cout <<"\nHiding base prefix using noshowbase:"<< endl;
         cout << noshowbase;
         cout <<"Decimal: "<< dec << number <<", "<<"Octal: "<< oct << number <<", "<<"Hex: "<< hex << number << endl;
    
         cout <<"\nDisplaying base prefix using showbase:"<< endl;
         cout << showbase;
         cout <<"Decimal: "<< dec << number <<", "<<"Octal: "<< oct << number <<", "<<"Hex: "<< hex << number << endl;
    
         cout <<"\nLowercase hex using nouppercase: "<< nouppercase 
              << hex << showbase <<255<<", "<<171<< endl;
         cout <<"Uppercase hex uppercase: "<< uppercase << hex 
              << showbase <<255<<", "<<171<< endl;return0;}

    The output of the above code is as follows:

    Original number:255
    Decimal: 255, Octal: 377, Hex: ff
    
    Setting base using setbase():
    Base 10: 255, Base 8: 377, Base 16: ff
    
    Hiding base prefix using noshowbase:
    Decimal: 255, Octal: 377, Hex: ff
    
    Displaying base prefix using showbase:
    Decimal: 255, Octal: 0377, Hex: 0xff
    
    Lowercase hex using nouppercase: 0xff, 0xab
    Uppercase hex uppercase: 0XFF, 0XAB
    

    Boolean Manipulators

    Boolean manipulators are used to represent the boolean values as either true/false or 0/1.

    ManipulatorsDefinitionSyntax
    boolalphaIt displays booleans as true/false.cout << boolalpha << true;
    noboolalphaIt displays booleans as 1/0.cout << noboolalpha << true;

    Here is an example demonstrating the use of boolean manipulators where boolalpha and noboolalpha display boolean values as true/false and 1/0, respectively.

    #include <iostream>#include <iomanip>usingnamespace std;intmain(){
        cout <<"Using boolalpha for comparison"<< endl;int a =10, b =20, c =10;
    
        cout << boolalpha;
        cout <<"a = "<< a <<", b = "<< b <<", c = "<< c << endl;
        cout <<"a == b: "<<(a == b)<<", "<<"a == c: "<<(a == c)<<", "<<"a < b: "<<(a < b)<<", "<<"b > c: "<<(b > c)<< endl;
    
        cout <<"\nUsing noboolalpha for comparison"<< endl;
        cout << noboolalpha;
        cout <<"a = "<< a <<", b = "<< b <<", c = "<< c << endl;
        cout <<"a == b: "<<(a == b)<<", "<<"a == c: "<<(a == c)<<", "<<"a < b: "<<(a < b)<<", "<<"b > c: "<<(b > c)<< endl;return0;}

    The output of the above code is as follows:

    Using boolalpha for comparison
    a = 10, b = 20, c = 10
    a == b: false, a == c: true, a < b: true, b > c: true
    
    Using noboolalpha for comparison
    a = 10, b = 20, c = 10
    a == b: 0, a == c: 1, a < b: 1, b > c: 1
    

    Time and Date Manipulators

    You can format the time in the given format or parse the formatted time and date using time and date manipulators.

    ManipulatorsDefinitionSyntax
    put_timeIt is used to format and output time in the specified format.cout << put_time(&tm, “%Y-%m-%d”);
    get_timeIt is used for parsing the formatted time input.cin >> get_time(&tm, “%Y-%m-%d”);

    The following example demonstrates time and date manipulators to format and parse the date and time using put_time and get_time respectively:

    #include <iostream>#include <iomanip>#include <sstream>#include <ctime>usingnamespace std;intmain(){
         time_t now =time(0);
         tm *ltm =localtime(&now);
    
         cout <<"Current date and time using put_time: "<<"\n";
         cout <<put_time(ltm,"%Y-%m-%d %H:%M:%S")<< endl;
    
         string dateStr ="2025-09-08 14:30:00";
         tm t ={};
         istringstream ss(dateStr);
    
         cout <<"Custom date and time using get_time: "<<"\n"<< dateStr << endl;
         ss >>get_time(&t,"%Y-%m-%d %H:%M:%S");return0;}

    The output of the above code is as follows:

    Current date and time using put_time: 
    2025-09-08 07:19:03
    Custom date and time using get_time: 
    2025-09-08 14:30:00
    

    Conclusion

    Manipulators in C++ are special functions that are used with input and output streams (cin and cout) to change the output format and how to read an input. Manipulators in C++ can be further categorized into the following types: output stream manipulators, input stream manipulators, alignment manipulators, floating point manipulators, numeric base and case manipulators, boolean manipulators, time and date manipulators.

  • Cout in C++

    The predefined object cout is an instance of ostream class that displays the output to the user. The cout object is said to be attached to the standard output device, which usually is the screen. The cout object is used in conjunction with the stream insertion operator (<<) that inserts the data into the output stream and displays the data on the screen.

    The syntax for using cout object with a single variable and multiple variables is given below −

    // For single variable
    cout << variable_name;

    Or,

    // For multiple variables
    cout << variable1 << variable2 <<...<< variableN;

    where,

    • << is the insertion operator.
    • variable_name, variable1,…, variableN are the variable names whose values we want to display.

    Read this chapter to get a good understanding of how cout works in C++.

    Displaying Output with cout

    The following example demonstrates how to display a single integer and multiple integers as output using cout to show the sum of the numbers −

    #include <iostream>usingnamespace std;intmain(){int num1 =2;// Displaying single value
        cout <<"Num1: "<< num1 << endl;int num2 =2, num3 =5;// Displaying multiple values
        cout <<"Num2: "<< num2 <<", Num3: "<< num3 << endl;
        cout <<"Sum: "<< num1 + num2 + num3 << endl;return0;}

    The output of the above code is as follows −

    Num1: 2
    Num2: 2, Num3: 5
    Sum: 9
    

    Display Arrays with cout

    This example demonstrated how to display an array as output using cout −

    #include <iostream>usingnamespace std;intmain(){int arr[5]={1,2,3,4,5};int n =5;
         
        cout <<"Array elements: ";for(int i =0; i < n; i++){// Displaying array elements
            cout << arr[i]<<" ";}
        cout << endl;return0;}

    The output of the above code is as follows −

    Array elements: 1 2 3 4 5 
    

    C++ cout Object Functions

    Below is a table of most commonly used functions of C++ cout object −

    FunctionsDefinition
    cout.put()The cout.put() function writes a single character to the output stream.
    cout.write()It writes a specified number of characters from a character array or string to the output stream.
    cout.flush()The flush() function displays all pending output by forcing the output buffer to empty immediately.
    cout.good()It checks the state of output stream and returns true if the output stream is in a good state with no errors.
    cout.bad()It checks the state of output stream and returns true if an error occurs in the stream that can not be recovered.
    cout.fail()It returns true for both recoverable and non-recoverable errors in the output stream.
    cout.clear()It clears the error flags of the stream and reset it to good state. The clear() function is also used to set error states of output stream.
    cout.width()The cout.width() function is used to set the minimum field width for the next output operation.
    cout.precision()It sets the precision for floating-point numbers displayed in the output stream.
    cout.fill()It sets the fill character that is used to pad output when the width is greater than the number of characters to be displayed.
    cout.seekp()It is used to set the position of the put pointer in the output stream. It is generally used with file streams (ofstream).
    cout.tellp()It returns the current position of the put pointer in the output stream. It is generally used with file streams (ofstream).
    cout.eof()It returns true upon reaching end of file in the output stream.
    cout.rdbuf()It returns the stream buffer object of cout that can be used to write to or redirect the output stream directly.

    Here are the example codes of each member function listed above in the table −

    put() write() flush() width() precision()

    In this example, we have used the cout.put() function to display “Hello C++”, printing each character at a time in the output stream −

    #include <iostream>usingnamespace std;intmain(){
    
        cout <<"Displaying characters: ";
        cout.put('H');
        cout.put('e');
        cout.put('l');
        cout.put('l');
        cout.put('o');
        cout.put(' ');
        cout.put('C');
        cout.put('+');
        cout.put('+');
        cout << endl;return0;}

    The output of the above code is given below −

    Displaying characters: Hello C++
    

    good() bad() fail() clear() fill()

    In this example, we are checking the state of the output stream. For successful operations, it prints success message and for failed operations, it prints a message of output stream error.

    #include <iostream>usingnamespace std;intmain(){int num =345;
    	cout <<"Displaying number: "<< num << endl;if(cout.good()){
    		cout <<"Output stream is in good state"<< endl;}else{
    		cout <<"Output stream has errors"<< endl;}return0;}

    The output of the above code is as follows −

    Displaying number: 345
    Output stream is in good state
    

    Conclusion

    In this chapter, we understood the cout object and its usage along with its member functions and their respective example codes.

  • Cin in C++

    The predefined object cin is an instance of istream class that accepts the user input. The cin object is said to be attached to the standard input device, which usually is the keyboard. The cin object is used in conjunction with the stream extraction operator (>>) that extracts the data from the input stream and stores the extracted data in a variable.

    The syntax for using the cin object with a single variable and multiple variables is given below −

    // For single variable
    cin >> variable_name;or// For multiple variables
    cin >> variable1 >> variable2 >>...>> variableN;
    
    where,>> is the extraction operator.
    variable_name, variable1, variable2,..., variableN are 
    the variable names where we store the input values.

    Read this chapter to get a good understanding of how the cin object works. We have used plenty of examples to explain all of its features in detail.

    Taking User Input with cin

    The following example demonstrates how to take a single integer and multiple integers as input using cin to calculate the sum of the numbers −

    #include <iostream>usingnamespace std;intmain(){int num1;
         cout <<"Enter number num1: ";
         cin >> num1;
         cout <<"Num1: "<< num1 << endl;int num2, num3;
         cout <<"Enter the numbers num2 and num3: ";
         cin >> num2 >> num3;
         cout <<"Num2: "<< num2 <<", Num3: "<< num3
              <<"\n"<<"Sum: "<< num1 + num2 + num3 << endl;return0;}

    The output of the above code is as follows −

    Enter number num1: 2
    Num1: 2
    Enter the numbers num2 and num3: 2 5
    Num2: 2, Num3: 5
    Sum: 9
    

    Taking Array Input with cin

    This example demonstrated how to take an array as input using cin:

    #include <iostream>usingnamespace std;intmain(){int arr[5];int n =5;
         
        cout <<"Enter "<< n <<" array elements: ";for(int i =0; i < n; i++){// Taking array elements input
            cin >> arr[i];}
    
        cout <<"Array elements: ";for(int i =0; i < n; i++){
            cout << arr[i]<<" ";}return0;}

    The output of the above code is as follows −

    Enter 5 array elements: 1 2 3 4 5
    Array elements: 1 2 3 4 5
    

    C++ cin Object Functions

    Below is a table of most commonly used functions of C++ cin object −

    FunctionsDefinition
    cin.get()The cin.get() function reads a single character including whitespace from input stream.
    cin.getline()It reads a line of text from user input along with whitespaces until it reaches end of the line or newline character.
    cin.read()You can specify the number of characters you want to read from input stream using cin.read() function.
    cin.putback()The cin.putback() function puts a character back into the input stream that was removed after using get() function to read the character.
    cin.peek()It looks at the next character in the input stream without removing it unlike get() function that removes the character.
    cin.good()It checks the state of input stream and returns true if the input stream is in a good state with no errors.
    cin.bad()It checks the state of input stream and returns true if an error occurs in the stream that can not be recovered.
    cin.fail()It returns true for failed operations on the input stream.
    cin.clear()It clears the error flags of the stream and reset it to good state.
    cin.ignore()The cin.ignore() function is used to skip and discard characters from the input buffer so they are not read by the next input operation.
    cin.gcount()It returns the count of characters extracted by the last unformatted input operation including whitespaces.
    cin.seekg()It is used in file handling to set the position of the get pointer in the input stream. It is generally used with file streams (ifstream).
    cin.eof()It returns true upon reaching end of file in the input stream.
    cin.rdbuf()It returns the stream buffer object of cin that can be used to read from or redirect the input stream directly.

    Here are the example codes of each member function listed above in the table −

    get() getline() read() putback() peek()

    In this example, we have used the cin.get() function to read the first character of input stream −

    #include <iostream>usingnamespace std;intmain(){
        cout <<"Enter any character: ";char ch = cin.get();
        cout <<"Entered character: '"<< ch <<"'"<< endl;char ch2 = cin.get();
        cout <<"Entered character: '"<< ch2 <<"'"<< endl;return0;}

    The output of the above code is given below. Here we have used the get() function twice. The first time it return the character ‘a’ and second time it returns a blank space.

    Enter any character: a b c
    Entered character: 'a'
    Entered character: ' '
    

    good() bad() fail() clear() ignore()

    In this example, we are validating the user input for an integer. For integer input, it prints the integer and for non-integer inputs, it prints a message of invalid input.

    #include <iostream>usingnamespace std;intmain(){int num;
    	cout <<"Enter an integer: ";
    	cin >> num;if(cin.good()){
    		cout <<"Valid input. Given number: "<< num << endl;}else{
    		cout <<"Invalid input. Input number is not an integer"<< endl;}return0;}

    The output of the above code is as follows −

    Enter an integer: 345
    Valid input. Given number: 345
    
    Enter an integer: abcd
    Invalid input. Input number is not an integer
    

    gcount() eof()

    Here is an example of cin.gcount() function returning the count of input characters including the whitespaces −

    #include <iostream>usingnamespace std;intmain(){char buffer[100];
        cout <<"Enter text: ";
        cin.getline(buffer,100);
        cout <<"Entered Text: "<< buffer << endl;
        cout <<"Count of characters: "<< cin.gcount()<< endl;return0;}

    The output of the above code is as follows:

    Enter text: Tutorials Point
    Entered Text: Tutorials Point
    Count of characters: 16 
    

    Conclusion

    In this chapter, we understood the cin object and its usage along with its member functions and their respective example codes.

  • C++ Basic Input/Output

    The C++ standard libraries provide an extensive set of input/output capabilities which we will see in subsequent chapters. This chapter will discuss very basic and most common I/O operations required for C++ programming.

    C++ I/O occurs in streams, which are sequences of bytes. If bytes flow from a device like a keyboard, a disk drive, or a network connection etc. to main memory, this is called input operation and if bytes flow from main memory to a device like a display screen, a printer, a disk drive, or a network connection, etc., this is called output operation.

    I/O Library Header Files

    There are following header files important to C++ programs −

    Sr.NoHeader File & Function and Description
    1<iostream>This file defines the cin, cout, cerr and clog objects, which correspond to the standard input stream, the standard output stream, the un-buffered standard error stream and the buffered standard error stream, respectively.
    2<iomanip>This file declares services useful for performing formatted I/O with so-called parameterized stream manipulators, such as setw and setprecision.
    3<fstream>This file declares services for user-controlled file processing. We will discuss about it in detail in File and Stream related chapter.
    4<bits/stdc++.h>This header file includes most of the standard C++ libraries, which adds a wide range of functionalities without the need to specify each library individually. This is particularly helpful during coding contests.

    The Standard Output Stream (cout)

    The predefined object cout is an instance of ostream class. The cout object is said to be “connected to” the standard output device, which usually is the display screen. The cout is used in conjunction with the stream insertion operator, which is written as << which are two less than signs as shown in the following example.

    Example

    #include <iostream>usingnamespace std;intmain(){char str[]="Hello C++";
     
       cout <<"Value of str is : "<< str << endl;}

    When the above code is compiled and executed, it produces the following result −

    Value of str is : Hello C++
    

    The C++ compiler also determines the data type of variable to be output and selects the appropriate stream insertion operator to display the value. The << operator is overloaded to output data items of built-in types integer, float, double, strings and pointer values.

    The insertion operator << may be used more than once in a single statement as shown above and endl is used to add a new-line at the end of the line.

    The Standard Input Stream (cin)

    The predefined object cin is an instance of istream class. The cin object is said to be attached to the standard input device, which usually is the keyboard. The cin is used in conjunction with the stream extraction operator, which is written as >> which are two greater than signs as shown in the following example.

    Example

    #include <iostream>usingnamespace std;intmain(){char name[50];
     
       cout <<"Please enter your name: ";
       cin >> name;
       cout <<"Your name is: "<< name << endl;}

    When the above code is compiled and executed, it will prompt you to enter a name. You enter a value and then hit enter to see the following result −

    Please enter your name: cplusplus
    Your name is: cplusplus
    

    The C++ compiler also determines the data type of the entered value and selects the appropriate stream extraction operator to extract the value and store it in the given variables.

    The stream extraction operator >> may be used more than once in a single statement. To request more than one datum you can use the following −

    cin >> name >> age;
    

    This will be equivalent to the following two statements −

    cin >> name;
    cin >> age;
    

    The Standard Error Stream (cerr)

    The predefined object cerr is an instance of ostream class. The cerr object is said to be attached to the standard error device, which is also a display screen but the object cerr is un-buffered and each stream insertion to cerr causes its output to appear immediately.

    The cerr is also used in conjunction with the stream insertion operator as shown in the following example.

    Example

    #include <iostream>usingnamespace std;intmain(){char str[]="Unable to read....";
     
       cerr <<"Error message : "<< str << endl;}

    When the above code is compiled and executed, it produces the following result −

    Error message : Unable to read....
    

    The Standard Log Stream (clog)

    The predefined object clog is an instance of ostream class. The clog object is said to be attached to the standard error device, which is also a display screen but the object clog is buffered. This means that each insertion to clog could cause its output to be held in a buffer until the buffer is filled or until the buffer is flushed.

    The clog is also used in conjunction with the stream insertion operator as shown in the following example.

    Example

    #include <iostream>usingnamespace std;intmain(){char str[]="Unable to read....";
     
       clog <<"Error message : "<< str << endl;}

    When the above code is compiled and executed, it produces the following result −

    Error message : Unable to read....
    

    You would not be able to see any difference in cout, cerr and clog with these small examples, but while writing and executing big programs the difference becomes obvious. So it is good practice to display error messages using cerr stream and while displaying other log messages then clog should be used.

    Input/Output Using bits/stdc++

    The bits/stdc++.h is a non-standard header file in C++, as it does not belong to the official C++ Standard Library. Instead, it is a GCC-specific header file that includes most of the standard libraries in C++. You can also use bits/stdc++.h for the input and output without using another library.

    Example

    #include <bits/stdc++.h>usingnamespace std;intmain(){int number;
      string name;// #include <iostream>// #include <string>
      cout <<"Welcome to TutorialsPoint!"<< endl;// Input of user's name and number
      cout <<"Please enter your name: ";
      cin >> name;
      cout <<"Please enter a number: ";
      cin >> number;
      cout <<"Hello,"<< name <<" You entered "<< number << endl;// Demonstrating some STL features// #include <vector>
      vector<int> numbers;
      cout <<"Enter 4 numbers: ";for(int i =0; i <4;++i){int temp;
        cin >> temp;
        numbers.push_back(temp);}
      cout <<"You entered the following numbers: ";for(int num : numbers){
        cout << num <<" ";}
      cout << endl;// Sort and display the numbers// #include <algorithm>sort(numbers.begin(), numbers.end());
      cout <<"Sorted numbers: ";for(int num : numbers){
        cout << num <<" ";}
      cout << endl;return0;}

    When the above code is compiled and executed, it produces the following result −

    Welcome to TutorialsPoint!
    Please enter your name: Aman
    Please enter a number: 2006
    Hello, Aman You 2006
    Enter 4 numbers: 2
    0
    0
    6
    You entered the following numbers: 2 0 0 6 
    Sorted numbers: 0 0 2 6
    

    So here, instead of using these multiple headers such as,

    • #include <iostream>
    • #include <string>
    • #include <vector>
    • #include <algorithm>

    We used simply <bits/stdc++.h> as it includes all the necessary standard libraries for input/output operations, string handling, dynamic arrays, and algorithms which simplifies your code and makes it cleaner and more convenient. It is especially used for competitive programming.