Printing Output
The cout object along with insertion operator is used to print text on the screen.
Note: cout is pronounced as "see-out".
If you want to print anything on screen you have to write cout, then insertion operator, then your text in double quotation marks in the main function.
Note: Do not forget to put ; at the end.
Adding line breaks
\n moves the cursor to the next line. It is used inside the quotation marks.
endl manipulator is also used to add line breaks. It is used outside the quotation marks.
Different Escape Sequences
| Escape Sequence | Code | Description |
|---|---|---|
| \n | cout<<"\n"; | New line |
| \t | cout<<"\t"; | Tab - prints four spaces |
| \a | cout<<"\a"; | Beep sound |
| \b | cout<<"\b"; | Backspace |
| \f | cout<<"\f"; | Form feed |
| \r | cout<<"\r"; | Carriage return |
| \\ | cout<<"\\"; | It adds backslash |
| \' | cout<<"\'"; | Single quote |
| \? | cout<<"\?"; | Question mark |
| \0 | cout<<"\0"; | Null character |
Printing Numbers
If we want to print numbers in C++, we store them in variables then print variables on screen. We will discuss about this in detail in variable section. Here is a brief description.
Note: If we write numbers in double quotes, these are not treated as numbers. Everything inside double quotation marks is a string literal which is text.
Practice Exercises
Complete these exercises to reinforce your learning and earn XP