CITS2002 Systems Programming  
prev
next CITS2002 CITS2002 schedule  

Standard cin and cout I/O streams

Similar to C11's use of its standard I/O header file and library functions, C++ employs its iostream header and functions.

A little confusingly, it is possible to mix C11 and C++ I/O in the same programs, but it's not always safe to mix them in the same statement.

#include <iostream>

using namespace std;

int main(void)
{
    int x;

    cin >> x;
    cout << x/3 << ' ' << x*2;

    return 0;
}

In particular, many programmers moving from C11 to C++ prefer C11's printf() family of functions, over C++'s use of appending strings to a file-stream:

#include <iostream>

using namespace std;

void printVector(double x0, double x1, double y0, double y1)
{
//  equivalent to C11's  printf("(%f,%f) -> (%f,%f)\n", x0, y0, x1, y1);

    cout << "(" <<
         x0 << "," << y0 <<
         ") -> ("
	 << x1 << "," << y1 <<
         ")" << endl;
}

 


CITS2002 Systems Programming, Lecture 23, p2, 17th October 2023.