Whitespace in C++


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";
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.

Updated on: 11-Feb-2020

7K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements