0

What does __G signify in C?

I'm using GCC 4.9.

I'm using latest MinGW version.

I'm compiling with -std=gnu11.

I have the following C (being compiled with GCC as C11) code:

#ifndef __G
#define __G 0
#endif

It compiles fine.

But now, while compiling with the latest MinGW, i get the following:

In file included from ../common/sysbase/sysbase.h:6:0,
             from Monitor.c:5:
../common/sysbase/sysbase_chk.h:3:13: error: expected ';', ',' or ')' before numeric constant
#define __G 0

compilation terminated due to -Wfatal-errors.

GCC under MinGW64 seems to be using `__G for something.

I could not find it in any header.

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
user3525723
  • 313
  • 1
  • 8
  • 2
    It's an identifier. It happens to be one that's reserved to the implementation, so you shouldn't use it in your own code; either the compiler or a header that's part of the standard library is free to define it for its own purposes. – Keith Thompson Jun 20 '14 at 01:14
  • But do you know from which API/standard does it come from? – user3525723 Jun 20 '14 at 01:19
  • I know identifiers starting with "__" are to be assumed reserved words. But my usage here is just that! I'm creating an API, and i use "__G" and "__LE" as macros to identify the way words are stored in memory in the target machine... Now, after more than a year compiling codes with those headers, will I have to change aaalll my codes? :( – user3525723 Jun 20 '14 at 01:21
  • I have no idea. It probably doesn't come from any API or standard. The ISO C standard says it's reserved to the implementation. Do not use it yourself. Doing so causes your program's behavior to be undefined. If it's an include guard, I suggest using something like `#ifndef HEADER_NAME_H` – Keith Thompson Jun 20 '14 at 01:22
  • 2
    "reserved words" means reserved for the implementation (i.e. the compiler plus whatever else is required to implement the C standard), not for yourself! You should pick a naming convention that does not overlap with reserved identifiers; and you'll have to let other users of your code know about this. – M.M Jun 20 '14 at 01:38
  • In case you're curious what `__G` is expanding to, you can run `gcc -std=gnu11 -E ` to see the output after preprocessing. – Tavian Barnes Jun 26 '14 at 17:48

1 Answers1

4

__G is simply an identifier. I'm not aware of any standard usage of it, but since it starts with two underscores, it's reserved to the implementation for all purposes. The word "implementation" here refers to the C compiler and library, not to any code you write (unless you're implementing part of the compiler or C standard library yourself.)

(The standard says that any identifier starting with two underscores, or with an underscore and an uppercase letter, are reserved for any use; any identifier starting with an underscore is reserved for use with file scope. See section 7.1.3 of the N1570 C standard draft.)

You should not use it in your own code. Doing so causes your program's behavior to be undefined.

The C implementation (either the compiler, the linker, a header, or the library) is permitted to define __G in any way it likes. Apparently this is what you've run into.

In answer to a question in your comment:

Now, after more than a year compiling codes with those headers, will I have to change aaalll my codes?

Yes, that's exactly what I'm saying. Your usage of __G has always been incorrect; you just happen not to have run into any visible symptoms until now.

(It's at least conceivable that previous versions of MinGW defined it in some way that didn't cause your code to fail to compile, but that could have cause it to misbehave.)

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631