4
#include <sys/types.h>
//Line 2: typedef unsigned int uint;
//Line 3: typedef unsigned int uint;
int main() {
    uint a;
    return 0;
}

Given the above C code, it's compiled successfully, for uint is defined in <sys/types.h>. Even though it's not standardized, it's added there for Sys V compatibility, commented in the code.

Uncomment the second line of the above code still results successful compiling. As I understand it's not allowed to redefine one type, confirmed by uncommenting the second and third lines, which will result in one compiling error.

How come the compiler is smart enough to know uint is defined in standard library or user's code? Both gcc and clang give consistent behavior.

Edit: Linking is not part of the game in this case. The error is reproduced with compile only, i.e (-c option).

Line number is added to reduce confusion.

Edit:

Uncomment the second line of the above code still results successful compiling. As I understand it's not allowed to redefine one type, confirmed by uncommenting the second and third lines, which will result in one compiling error.

I have no idea why I wrote this. Apparently, uncommenting Line 2 and Line 3 doesn't result in compilation error for gcc. Clang gives error for the default compiling options is much more strict and could be tuned by passing some parameters.

Here describes whether multiple typedef is allowed or not, which turns out to be quite complicated. Anyway just try to avoid duplicate typedef.

Community
  • 1
  • 1
Albert Netymk
  • 1,102
  • 8
  • 24
  • I am confused by your question. The linker know where code is, and in which section it was built. The compiler just sees a type that you or someone else has defined. If you could elaborate on your question more, it sounds almost like you are describing a language that helps build programs, whose components run based on certain permissions. I know nothing about Ada, but your description reminds me of descriptions I've heard of Ada. Again, if you could edit your original post and explain more, it would be helpful. – octopusgrabbus Mar 10 '13 at 21:08
  • 1
    My gcc doesn't get mad with 2 `typedef unsigned int uint`. Now if the second one tries to redeclare it to something else, then I get an error. What version are you using ? – cnicutar Mar 10 '13 at 21:09
  • the type is only compiler magic.. – Aniket Inge Mar 10 '13 at 21:09
  • @cnicutar neither does mine :-) I was editing, and I thought I must check out what my gcc says about the something like above – Aniket Inge Mar 10 '13 at 21:10

2 Answers2

2

Repeating a declaration is perfectly valid in C, so if you uncomment both lines as you describe you will not see any error, contrary to what you say.

Having two different declarations for a name would be an error.

Repeating a definition is an error as well, but a typedef is not a definition (despite the def), it is a declaration.

Chris Dodd
  • 119,907
  • 13
  • 134
  • 226
1

Standard library is also user code, usually written by another user.

Uncomment the second line of the above code still results successful compiling. As I >understand it's not allowed to redefine one type, confirmed by uncommenting the second and third lines, which will result in one compiling error.

On my gcc it does not. (version 4.5.3)

How come the compiler is smart enough to know uint is defined in standard library or user's code? Both gcc and clang give consistent behavior.

The compiler knows no distinction between user code and the one in the standard library. Although the compiler can distinguish between standard library files and user code, I really do not see any reason to do so. All it sees is textual data that it can lex/parse/codegen.

Aniket Inge
  • 25,375
  • 5
  • 50
  • 78