C naming conventions: structure and function

While studying the possibilities of processing requests for system signals in the sigaction.h header sigaction.h I noticed that the structure and function that returns int were named sigaction .

Even if this seems semantically correct, since the compiler should be able to output between two definitions, why are duplicate sigaction definitions of the valid C syntax?

+4
source share
2 answers

In C, structure tags live in a separate namespace from other names. The string is called struct sigaction , and the function is just sigaction .

+7
source

The compiler can separate structure tags (also unions and enumerations) because they follow the keywords struct / union / enum , respectively (C11 §6.2.3p1).

Then the declarations must be unique within the "namespace" according to §6.7p3.

Since the structure tags and function identifiers (which are regular identifiers) are not in the same namespace, the collision is fine.

When it comes to use, you cannot do:

 typedef struct _test { int a; } test; void test(void) { } 

The compiler will tell you:

 test.c:5:6: error: 'test' redeclared as different kind of symbol void test(void) { ^ test.c:3:3: note: previous declaration of 'test' was here } test; 
+4
source

All Articles