Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do people use __ (double underscore) so much in C++

Tags:

c++

syntax

I was taking a look through some open-source C++ code and I noticed a lot of double underscores used within in the code, mainly at the start of variable names.

return __CYGWIN__; 

Just wondering: Is there a reason for this, or is it just some people's code styles? I would think that it makes it hard to read.

like image 830
Nathan W Avatar asked Oct 22 '08 03:10

Nathan W


People also ask

Why do we use double underscore?

Double underscores are used for fully private variables. If your class is intended to be subclassed, and you have attributes that you do not want subclasses to use, consider naming them with double leading underscores and no trailing underscores.

What does double underscore mean in C?

In C, symbols starting with an underscore followed by either an upper-case letter or another underscore are reserved for the implementation. You as a user of C should not create any symbols that start with the reserved sequences.

Why _ is used in C?

The programmer uses a single underscore _ as the name for the second argument to the main function. He uses an old style declaration for main() somewhat equivalent to modern int main(int t, int _, char *a) .

What are _ used for?

An underscore, _, also called an underline, low line or low dash, is a line drawn under a segment of text. In proofreading, underscoring is a convention that says "set this text in italic type", traditionally used on manuscript or typescript as an instruction to the printer.


2 Answers

From Programming in C++, Rules and Recommendations :

The use of two underscores (`__') in identifiers is reserved for the compiler's internal use according to the ANSI-C standard.

Underscores (`_') are often used in names of library functions (such as "_main" and "_exit"). In order to avoid collisions, do not begin an identifier with an underscore.

like image 66
maccullt Avatar answered Sep 23 '22 15:09

maccullt


Unless they feel that they are "part of the implementation", i.e. the standard libraries, then they shouldn't.

The rules are fairly specific, and are slightly more detailed than some others have suggested.

All identifiers that contain a double underscore or start with an underscore followed by an uppercase letter are reserved for the use of the implementation at all scopes, i.e. they might be used for macros.

In addition, all other identifiers which start with an underscore (i.e. not followed by another underscore or an uppercase letter) are reserved for the implementation at the global scope. This means that you can use these identifiers in your own namespaces or in class definitions.

This is why Microsoft use function names with a leading underscore and all in lowercase for many of their core runtime library functions which aren't part of the C++ standard. These function names are guaranteed not to clash with either standard C++ functions or user code functions.

like image 34
CB Bailey Avatar answered Sep 26 '22 15:09

CB Bailey