EnvelopedDevil
EnvelopedDevil

Reputation: 658

Why is the word `false` written blue and the word `FALSE` written purple in Visual Studio?

Why does Visual Studio change the word color depending on the way it is entered:

false with blue, but FALSE with purple.
true with blue but TRUE with purple.

Is there any difference in the meaning of them and if yes what is it?

Upvotes: 30

Views: 4637

Answers (3)

Jay
Jay

Reputation: 2686

The syntax highlighting is just for the user so you can understand that the word you typed in is a keyword for the language. Every language has their own set of keywords.

Just to clarify, that is not the 'compiler' changing the color. That is visual studio doing that for you. Based on the the extension of the file. I.e (.cpp .java .py .js) the software will change for different words.

The capital TRUE and FALSE are specific to Windows API (more specifically--macros), and as for the lower case, it is specific to c++ language.

Upvotes: 5

Bathsheba
Bathsheba

Reputation: 234805

true and false are keywords in C++ so your IDE (not the compiler) is painting them blue.

TRUE and FALSE are often defined by various headers, primarily for compatibility with C and older C++ compilers where true and false are not keywords.

As for their equivalence, the C++ standard does not define sizeof(true) and sizeof(false) to be 1 but they will be the same as sizeof(bool). Footnote 69 for C++ standard:

sizeof(bool) is not required to be 1.

You'll probably find that sizeof(TRUE) and sizeof(FALSE) are sizeof(int) since TRUE and FALSE are often defined as int types, but it would be unwise to assume this.

Upvotes: 36

Sneftel
Sneftel

Reputation: 41513

false is a keyword in C++; it's blue for the same reason for is blue. FALSE is a preprocessor macro declared by the Windows API; it's purple for the same reason MYFILE_H_DEFINED is purple. If you go into the editor preferences for C++, you'll see the colors MSVC is using for different identifiers.

Incidentally, TRUE and FALSE are WinAPI-specific and are a throwback to C, and should not be used except when communicating with the WinAPI.

Upvotes: 38

Related Questions