分享

C?变量和函数开头加一个和两个下划线的作用

 ovonel 2011-05-08

C 变量和函数开头加一个和两个下划线的作用

 

变量,函数加一个和两个下划线的用途:


WHAT ABOUT THE UNDERLINE?

The underline can be used as part of a variable name, and adds greatly to the readability of the resulting code. It is used by some, but not all, experienced C programmers. A few underlines are used for illustration in this tutorial. Since most compiler writers use the underline as the first character for variable names internal to the system, you should refrain from using the underline to begin an identifier to avoid the possibility of a name clash. To get specific, identifiers with two leading underscores are reserved for the compiler as well as identifiers beginning with a single underscore and using an upper case alphabetic character for the second. If you make it a point of style to never use an identifier with a leading underline, you will not have a naming clash with the system.

It adds greatly to the readability of a program to use descriptive names for variables and it would be to your advantage to do so. Pascal and Ada programmers tend to use long descriptive names, but most C programmers tend to use short cryptic names. Most of the example programs in this tutorial use very short names for that reason, but a few longer names are used for illustrative purposes.

上面意思就是说加一个_的变量一般都在complier的writer使用的,加两个_ _的are reserved for compiler 和 一个_加上一个大写字母都是这样的情形

防止名字冲突

------------------
1. 一种命名方式,没有实际意义,作用就是突出,防止重名
2. 一般是宏名,在前面后面加__是为了防止和用户定义重名了
3. 不是说了怕和宏重名了吗
4. FILE_是预定义宏,_dbg_msg()是什么.我也不知道,记得看过.  
5. 相信ansi   有标准的说明,好像是一种约定,并非强制性的.但是在  
一些著名的程序中很多的利用.   相信如果有了固定的约定,那么我们还是遵守这样的约定为妙.  
至少别人和自己看以来更容易理解.
6. 这不过是命名的习惯而已
7. 在Function名称加_,一般情况下是系统内部的函数,当然自己也可以定义这样的函数。
8. 一般只有已经广泛使用的系统库函数和宏才有资格使用_甚至__打头,为的是不与用户定义的名字  
冲突,所以B.Stroustup在《The   C++   Programming   Language》中告诫我们一般不要使用_或__  
打头的标志符,这也是一个编程风格的问题吧。
9. __FILE__,__LINE__   都是与定义的宏,使用_   和   __   开始的函数一般都是专用的函数,一般都是于特定系统相关的,如果要想有更好的移植性,应该避免使用。
---------------
10. 包含两个下划线的名称,或者以下划线开头,后跟一个大写字母的名称,是C++标准库的保留名称,在程序中不应使用这类名称。编译器不会检查这类名称,用户只能在程序出错时发现有一个冲突的名称
-------------

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多