?
Solved

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

Posted on 2003-03-16
7
Medium Priority
?
4,212 Views
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  
{
public:
     __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...
anyhelp?

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??

thanks,
takkie
0
Comment
Question by:takkie
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 1

Expert Comment

by:cybeonix
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.
0
 

Author Comment

by:takkie
ID: 8147379
hi cybeonix,

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

thanks.
tak
0
 
LVL 20

Expert Comment

by:jmcg
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.
0
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!

 
LVL 5

Expert Comment

by:Kocil
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 :)
 
0
 
LVL 15

Expert Comment

by:efn
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.
0
 

Author Comment

by:takkie
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?
0
 
LVL 15

Accepted Solution

by:
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.
0

Featured Post

Industry Leaders: 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…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
Suggested Courses
Course of the Month14 days, 4 hours left to enroll

801 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