[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c

macro conventions

Bill Cunningham

5/13/2011 4:56:00 PM

In defining macros it is of course proper style to use all caps for
example:

#define AVG 14

There are other conventions too. In trying to learn what is the
diference in having one underscore and two underscores in front of or behine
a macro? For example:

#define __FILE__

Two underscores if front of and behind the define. And here:

#define _GNU_SOURCE

One underscore in front and no underscores at the end. I hope this is clear.
I want to be sure of these things.

Bill


23 Answers

pete

5/13/2011 5:09:00 PM

0

Bill Cunningham wrote:
>
> In defining macros it is of course proper style
> to use all caps for
> example:
>
> #define AVG 14
>
> There are other conventions too. In trying to learn what is the
> diference in having one underscore and two underscores
> in front of or behine a macro? For example:
>
> #define __FILE__
>
> Two underscores if front of and behind the define. And here:
>
> #define _GNU_SOURCE
>
> One underscore in front and no underscores at the end.
> I hope this is clear.
> I want to be sure of these things.

The more important thing,
is to realise that your C implementation uses
some macro conventions which you are not supposed to use.
Macros with leading underscores are an example of that.

ISO/IEC 9899:1999 (E)

7.1.3 Reserved identifiers
1 Each header declares or defines all identifiers listed
in its associated subclause,
and optionally declares or defines identifiers listed
in its associated future library directions subclause
and identifiers which are always reserved either
for any use or for use as file scope identifiers.

? All identifiers that begin with an underscore
and either an uppercase letter or another underscore
are always reserved for any use.


--
pete

Shao Miller

5/13/2011 5:15:00 PM

0

On 5/13/2011 12:56, Bill Cunningham wrote:
> In defining macros it is of course proper style to use all caps for
> example:
>
> #define AVG 14
>
> There are other conventions too. In trying to learn what is the
> diference in having one underscore and two underscores in front of or behine
> a macro? For example:
>
> #define __FILE__
>
> Two underscores if front of and behind the define. And here:
>
> #define _GNU_SOURCE
>
> One underscore in front and no underscores at the end. I hope this is clear.
> I want to be sure of these things.
>

From the C99 draft with filename 'n1265.pdf' and its section 6.10.8,
point 4, we find that "Any other predefined macro names shall begin with
a leading underscore followed by an uppercase letter or a second
underscore."

In section 7.1.3, point 1, we find that "All identifiers that begin with
an underscore and either an uppercase letter or another underscore are
always reserved for any use" as well as "All identifiers that begin with
an underscore are always reserved for use as identifiers with file scope
in both the ordinary and tag name spaces."

I don't think there's anything special about underscores beyond that,
but I could be mistaken.

The two following underscores you noted in '__FILE__' are not special,
as far as I know; they're merely part of that standard macro name.

ralph

5/13/2011 6:15:00 PM

0

On Fri, 13 May 2011 12:56:20 -0400, "Bill Cunningham"
<nospam@nspam.invalid> wrote:

> In defining macros it is of course proper style to use all caps for
>example:
>
>#define AVG 14
>
> There are other conventions too. In trying to learn what is the
>diference in having one underscore and two underscores in front of or behine
>a macro? For example:
>
>#define __FILE__
>
>Two underscores if front of and behind the define. And here:
>
>#define _GNU_SOURCE
>
>One underscore in front and no underscores at the end. I hope this is clear.
>I want to be sure of these things.
>
>Bill
>

In general:

The use of two underscores ('__') followed by an uppercase letter in
identifiers is reserved for the compiler's internal implementation.

The use of one Underscore ('_') followed by either an upper or lower
case letter in identifiers are reserved for the vendor's
implementation or extensions to the language.

Traditionally macros are all caps to make them more visible within
code made up of mostly lower case characters. There are several nasty
side-effects and nuances associated with macros that makes anything
you can do to make them stand-out useful.

The actual 'rules' and conventions are more complicated and others
will be along shortly to quote chapter and verse. However, in order to
avoid collisions, best practice is simply never create a macro with an
underscore for your own use and use always use uppercase.

-ralph

Keith Thompson

5/13/2011 6:42:00 PM

0

Shao Miller <sha0.miller@gmail.com> writes:
[...]
> From the C99 draft with filename 'n1265.pdf' and its section 6.10.8,
> point 4, we find that "Any other predefined macro names shall begin with
> a leading underscore followed by an uppercase letter or a second
> underscore."
[...]

You mean n1256.pdf, not n1265.pdf.

It's available at <http://www.open-std.org/jtc1/sc22/wg14/www/docs/n12....

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.ne...
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Bill Cunningham

5/15/2011 3:09:00 AM

0

Keith Thompson wrote:

> You mean n1256.pdf, not n1265.pdf.
>
> It's available at
> <http://www.open-std.org/jtc1/sc22/wg14/www/docs/n12....

All right got it. Thanks Keith.

Bill


Bill Cunningham

5/15/2011 3:11:00 AM

0

ralph wrote:

> The actual 'rules' and conventions are more complicated and others
> will be along shortly to quote chapter and verse. However, in order to
> avoid collisions, best practice is simply never create a macro with an
> underscore for your own use and use always use uppercase.

Thanks.

Bill

>
> -ralph


Bill Cunningham

5/15/2011 8:01:00 AM

0

ralph wrote:

> The actual 'rules' and conventions are more complicated and others
> will be along shortly to quote chapter and verse. However, in order to
> avoid collisions, best practice is simply never create a macro with an
> underscore for your own use and use always use uppercase.
>
> -ralph

Isn't there also some conventions used in kernel source code. The Linux
kernel source that I have looked at (and its been awhile) has some pretty
different uses in declaring macro names too. But if I am understanding you
and others in this thread for anything I will be dealing with just the all
caps will do.

Bill


Angel

5/15/2011 11:15:00 AM

0

On 2011-05-15, Bill Cunningham <nospam@nspam.invalid> wrote:
>
> Isn't there also some conventions used in kernel source code. The Linux
> kernel source that I have looked at (and its been awhile) has some pretty
> different uses in declaring macro names too. But if I am understanding you
> and others in this thread for anything I will be dealing with just the all
> caps will do.

Kernel programming is very different from normal application
programming on many levels.

For example, you cannot use any of the standard libraries, so
you cannot use the usual printf() or malloc(). (The kernel source does
provide kprintf() and kalloc() though, but they don't work quite the
same.)

And yes, the kernel also has its own macro conventions.


--
"C provides a programmer with more than enough rope to hang himself.
C++ provides a firing squad, blindfold and last cigarette."
- seen in comp.lang.c

Keith Thompson

5/15/2011 1:51:00 PM

0

ralph <nt_consulting64@yahoo.net> writes:
[...]
> The actual 'rules' and conventions are more complicated and others
> will be along shortly to quote chapter and verse. However, in order to
> avoid collisions, best practice is simply never create a macro with an
> underscore for your own use and use always use uppercase.

TO be clear, it's leading underscores that should be avoided. FOO_BAR,
for example, is perfectly ok. Trailing underscores are also ok, though
I wouldn't use them without a good reason.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.ne...
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Malcolm McLean

5/15/2011 3:29:00 PM

0

On May 15, 4:50 pm, Keith Thompson <ks...@mib.org> wrote:
> ralph <nt_consultin...@yahoo.net> writes:
>
> TO be clear, it's leading underscores that should be avoided.  FOO_BAR,
> for example, is perfectly ok.  Trailing underscores are also ok, though
> I wouldn't use them without a good reason.
>
One problem with underscores is that they are not pronounced. So it
can be hard to remember whether it's FOO_BAR or FOOBAR. Also,
experience with websites has shown that people prefer words
runtogetherratherthan separated_out_by_underscores, if spaces aren't
an option.