M4 Guide
M4 Guide
The console is the basic interface of computers, normally it is the set composed of the keyboard and the
screen. The keyboard is generally the standard input device and the screen the standard output device.
In the iostream C++ library, standard input and output operations for a program are supported by two data
streams: cin for input and cout for output. Additionally, cerr and clog have also been implemented - these are two
output streams specially designed to show error messages. They can be redirected to the standard output or to a
log file.
Therefore cout (the standard output stream) is normally directed to the screen and cin (the standard input
stream) is normally assigned to the keyboard.
Output (cout)
The cout stream is used in conjunction with the overloaded operator << (a pair of "less than" signs).
The << operator is known as insertion operator since it inserts the data that follows it into the stream that
precedes it. In the examples above it inserted the constant string Output sentence, the numerical constant 120 and
the variable x into the output stream cout. Notice that the first of the two sentences is enclosed between double
quotes (") because it is a string of characters. Whenever we want to use constant strings of characters we must
enclose them between double quotes (") so that they can be clearly distinguished from variables. For example, these
two sentences are very different:
The insertion operator (<<) may be used more than once in a same sentence:
cout << "Hello, " << "I am " << "a C++ sentence";
this last sentence would print the message Hello, I am a C++ sentence on the screen. The utility of repeating the
insertion operator (<<) is demonstrated when we want to print out a combination of variables and constants or more
than one variable:
cout << "Hello, I am " << age << " years old and my zipcode is " << zipcode;
If we suppose that variable age contains the number 24 and the variable zipcode contains 90064 the output
of the previous sentence would be:
It is important to notice that cout does not add a line break after its output unless we explicitly indicate it,
therefore, the following sentences:
cout << "This is a sentence.";
cout << "This is another sentence.";
even if we have written them in two different calls to cout. So, in order to perform a line break on output we must
explicitly order it by inserting a new-line character, that in C++ can be written as \n:
First sentence.
Second sentence.
Third sentence.
To add a new-line, you may also use the endl manipulator. For example:
The endl manipulator has a special behavior when it is used with buffered streams: they are flushed. But
anyway cout is unbuffered by default.
You may use either the \n escape character or the endl manipulator in order to specify a line jump to cout.
Notice the differences of use shown earlier.
Input (cin)
Handling the standard input in C++ is done by applying the overloaded operator of extraction (>>) on the
cin stream. This must be followed by the variable that will store the data that is going to be read. For example:
int age;
cin >> age;
cin can only process the input from the keyboard once the RETURN key has been pressed. Therefore, even
if you request a single character cin will not process the input until the user presses RETURN once the character has
been introduced.
You can also use cin to request more than one datum input from the user:
is equivalent to:
cin >> a;
cin >> b;
In both cases the user must give two data, one for variable a and another for variable b that may be
separated by any valid blank separator: a space, a tab character or
Escape Sequences
Type Meaning
\\ backslash
\? question mark
\a audible bell
\b backspace
\f formfeed
\n newline
\0 NULL character
\r carriage return
\t tab (horizontal)
\v tab (vertical)
Type Conversion is the process of converting one predefined type into another.
When variables of one type are mixed with variables of another type, a type conversion will
occur.
C++ facilitates the type conversion into the following two forms :
• Implicit Type Conversion
• Explicit Type Conversion
Conversion from one data type to another is prone to data loss. This happens when data of a
larger type is converted to data of a smaller type.
abs() and fabs () functions, both are used to retrieve or calculate the absolute value. abs() is
used to calculate the absolute value for integer type numbers whereas fabs() are used for
floating type numbers.
abs( ) and fabs( )
Examples: floor()
Input : 2.5
Output : 2
Input : -2.1
Output : -3
Input : 2.9
Output : 2
trunc( )
trunc() is used to round (truncate) the value toward zero and returns the nearest integral value.
round ( )
round() is used to round the given number to the closest integer.
setprecision( )
setprecision when used along with ‘fixed’ provides precision to floating point numbers correct
to decimal numbers mentioned in the brackets of the setprecision.
setprecision function is part of <iomanip> header.