Whitespace in C++

C++ProgrammingObject Oriented Programming

Whitespace is a term that refers to characters that are used for formatting purposes. In C++, this refers primarily to spaces, tabs, and (sometimes) newlines. The C++ compiler generally ignores whitespace, with a few minor exceptions. For example, all the 4 lines below mean the same thing −

cout << "Hello";
cout                <<          "Hello"     ;

The exceptions where C++ compiler takes whitespace in consideration is inside quotes and for operator detection. So whenever you put in a string, c++ takes note of the whitespace. For example,

"Hello world!"
"Hello     world!"

Both of these are different strings. Also when you use compound operators or any multi-character operator, you cannot put in space between. For example,

<< and < < are different. Similarly, += and + = are different, with the latter not being a valid expression.

Published on 15-Feb-2018 16:17:02