Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


does the underscore or double underscore mean something in c++?

Posted on 2003-03-16
Medium Priority
Last Modified: 2010-10-05
there are some variables that have some leading underscores (_) or (__) infront of a variable...does that mean anything?

I am not looking for answers like, leading underscores for a variable name is valid. I understnad that...but i have seen some codes, for example, look below.

class CMyClass  
     __declspec(dllexport)  CString SayHello (CString strName);
     __declspec(dllexport)  CMyClass();
     __declspec(dllexport)  virtual ~CMyClass();


why do they design declspecto have 2 leading underscores? There are some with just 1 leading underscores...

from the books i read for c++, i dont see any explanation of this...

by the way, i asked a question last week, but didnt close it yet, i have an answer to accept...but i cant find that question...how do i see what are the qeustions i have asked in experts-exchange.com??

Question by:takkie

Expert Comment

ID: 8147340
If I recall correctly, the underscores are placed there to help the linker when it compiles the object.   And if I'm not mistaken, underscores indicate whether arguments are passed on the stack or in registers (maybe not all compilers).  Sometimes the function will be prefixed by an @ symbol as well, however that ends up in the object code and isn't valid for declaring functions.

Either way, declaring variables with underscores etc is valid.  I generally use it myself to avoid conflict with the API I'm using.  Some operating systems will have a structure named identically to something I want to use in my application.  I'll generally declare them as  struct __mystructure  just to avoid complications.

Author Comment

ID: 8147379
hi cybeonix,

i dont really understand what you mean, can you rephrase it in a simple wordings?

LVL 20

Expert Comment

ID: 8147588
The C and C++ standards reserve symbols beginning with double underscores for implementations and they should not be defined by user programs (they can be used, but not defined). This allows vendors, writers of application libraries, etc a way to create symbols that have some assurance of not conflicting with users' programs.
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Expert Comment

ID: 8147639
In simple term:

Double __ : reserved identifiers that are compiler dependent (introduced by vendor, not by standard C++)
ex: __fastcall (BCB); __declspec (VC++)
Single _  : reserved identifiers that are platform (machine/operating system) dependent
ex: _read, _geninterrupt (DOS/windows); _pthread (linux)

But this is only convention, there are some violation :)
LVL 15

Expert Comment

ID: 8147761
The C and C++ language standards reserve identifiers starting with either two underscores or an underscore followed by an upper-case letter for the compiler implementation.

The C++ standard further says "Each name having two consecutive underscores is reserved to the implementation for use as a name with both extern "C" and extern "C++" linkage."  This specification may be intended to apply only when the two underscores are at the beginning of the name, but it doesn't say that.

Otherwise, the standards don't specify any difference between names that start with two underscores and names that start with one and an upper-case letter.

To find the questions you have asked, click on your name "takkie" in the From: field of your question on this page.  That will take you to your Member Profile, where all the questions you have asked are listed.

Author Comment

ID: 8149369
in other words, unless i WANT to define a variable as

int __counter = 2;

i dont have to worry about the __ or _, right? Becuase I dont have to create variables with _ or __ myself anyway, its a vendor specific kind of thing...am i correct?
LVL 15

Accepted Solution

efn earned 200 total points
ID: 8149434
I think you're on the right track.

While you are allowed to create names starting with an underscore and a lower-case letter, many (though not all) experts recommend that you simply leave all names starting with an underscore to compiler implementers, that is, don't start any of the names you make up with an underscore.  That's a safe practice and easy to remember.

You don't really have to worry about the difference between names starting with one underscore or two.  All you have to do is be sure to include the leading underscore(s) when you are using some compiler or library facility like __FILE__ that is named that way.

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
Suggested Courses

572 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question