Basic Input/Output In C++

Basic Input/Output In C++

The statements that are used to get data and then to  are assign it to variables are known as input statements.

The statements that are used to receive data from the computer memory and then to send it to the output devices are called output statements.


Following are commonly used input/output statements:

The "cout" object ---Output Stream

The "cout" is pronounced as 'See Out'. The 'cout' stands for console output.

The 'cout' is a C++ predefined object. It is used as an output statement to display output on the computer screen. It is a part of iostream header file.

Flow of data from one location to another location is called stream. 

The syntax of 'cout' is:

cout<< const1/var1 [<< consts2/var2......];

where

cout  ==========>> name of output stream object.
<<     ==========>>put to operator or insertion operator. It directs the output to the output device.

EXAMPLE:

Write a program to print a message on screen using "cout" object.

#include <iostream.h>
#include <conio.h>
main ( )
{
clrscr ( );
cout<<"C++ is a powerful programming lamguage";
cout<<"UNIX operating system is written in C++";
}

Note: In the above program, the clrscr() function has been used to clear the computer screen. This function is a part of the "conio.h"  header file. This file has been included as preprocessor directive.

The Escape Sequence

Special non printing characters are used to control printing on the output device. These are called escape sequence. These characters are not printed. These are used inside string constants or independently. These are written in single or double quotes.

An escape sequence is a combination of backslash '\' and a code character. The backslash is called the control character.

For example, to transfer the printing control to the next line the escape sequence is written as \n.

Thus to transfer printing control to next line, the output statement is written as;

cout<<"I Love Pakistan\n";

other commonly used escape sequence characters are:

\a      a stands for alert of alarm.
\b      "b" stands for backspace.
\t       "t" stands for tab.
\n       "n" stands for new line.
\f        "f" stand for form feed.
\r        "r" stands for carriage return
\\         these are two backslash characters.
example:      cout<<"A:\\xyz"; 
 Output :      A:\xyz
\"       It is used to print double quotation marks (" ")
          cout<<"\"Pakistan\"";
\'        It is used to print single quotation marks.
          cout<<"\'Pakistan\'";

The "endl" Manipulator

Manipulators are operators that are used with the put to operator (<<) to control format of data. The endl is an important and the most commonly used manipulator in C++.

The endl stands for end of line. It has same effect as the escape sequence  "\n". This is a predefined iostream manipulator.

For example

cout<< "C++ \n"<<"Programming \n"<<"Language";

 is equivalent to

cout<<"C++ "<< endl<<"Programming "<< endl<<"Language"; 
Creative Commons License
Pakistan How by Tahira Aijaz is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.