Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I'm sorry, but the answer this website gives to 1. is wrong. See for yourself:

  int i;
  int i = 10;
  
  int main(int argc, char* argv[]){
  	return 0;
  }
  
Try to compile it. It doesn't work (gcc.exe (GCC) 5.3.0), the error is:

  a.cc:2:5: error: redefinition of 'int i'
   int i = 10;
       ^
  a.cc:1:5: note: 'int i' previously declared here
   int i;
       ^
Either I misunderstood the author and this example, or I do know C.


Judging by the .cc extension, you are compiling this with a C++ compiler. Quoting from Annex C (which documents the incompatibilities between C++ and ISO C) of the C++ standard:

   Change: C++ does not have “tentative definitions” as in C E.g., at
   file scope,

   int i;
   int i;

   is valid in C, invalid in C++. This makes it impossible to define
   mutually referential file-local static objects, if initializers are
   restricted to the syntactic forms of C. For example,

   struct X { int i; struct X *next; };
   static struct X a;
   static struct X b = { 0, &a };
   static struct X a = { 1, &b };

   Rationale: This avoids having different initialization rules for
   fundamental types and user-defined types.
   
   Effect on original feature: Deletion of semantically well-defined
   feature.

   Difficulty of converting: Semantic transformation.

   Rationale: In C++, the initializer for one of a set of
   mutually-referential file-local static objects must invoke a
   function call to achieve the initialization.

   How widely used: Seldom.


facepalm of course, even if I use gcc, if I compile a.cc it switches to the c++ compiler. Thanks.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: