Thread: Why is my IDE/compiler allowing me to define a varable twice?

  1. #1
    Registered User
    Join Date
    Mar 2016
    Posts
    110

    Why is my IDE/compiler allowing me to define a varable twice?

    Code:
    int b;
    int b;
    
    int main(){
        return 0;
    }
    My understanding is that int b is a definition, not a declaration and identifiers are not allowed to be defined more than once.

    • GNU GCC Compiler
    • Code Blocks:16.01
    • No flags used apart from -Wall (enable all warnings). Not sure which revision of C its following but if I enable the -std=c99 flag then it also doesnt work.


    Any ideas?

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    I wasn't aware of this behavior, gave it a test just now. It seems that global declarations don't default to zero unless they're the only ones available. Regardless whether you set the first or second "b" to 2, for example, "b" will always be "2". This holds true no matter how many declarations you have or in what order they're made. The only restriction seem to be that you can only have a single definition.
    Last edited by GReaper; 01-16-2017 at 02:37 PM.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    You're allowed to do that with global variables, although only one is allowed to have an initializer.

    Note that you can't do the same with locals that are in the same scope.

    The explanation should be in section 6.2 of the standard. I took a quick look but couldn't find it.

    Here's a somewhat stranger example:
    Code:
    #include <stdio.h>
    
    int b;
    int b, b, b;
    
    int main() {
        printf("%d\n", b);   // prints 42
        return 0;
    }
    
    int b;
    int b = 42;
    int b;

  4. #4
    Registered User
    Join Date
    Mar 2016
    Posts
    110
    Do you guys think it has anything to do with "tentative definitions?" I know the standard makes mention of this.

    Quote Originally Posted by algorism View Post
    You're allowed to do that with global variables, although only one is allowed to have an initializer.

    Note that you can't do the same with locals that are in the same scope.

    The explanation should be in section 6.2 of the standard. I took a quick look but couldn't find it.

    Here's a somewhat stranger example:
    Code:
    #include <stdio.h>
    
    int b;
    int b, b, b;
    
    int main() {
        printf("%d\n", b);   // prints 42
        return 0;
    }
    
    int b;
    int b = 42;
    int b;

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    That's exactly what it's meant to be, a tentative declaration. See here for more on the distinction between declaration and definition: Declarations - cppreference.com I know it's a C++ site, but it's well referenced from C standards and it's in a C section (like this site).

  6. #6
    Registered User
    Join Date
    Mar 2016
    Posts
    110
    But itsnt it a "tentative definition" not "tentative declaration"?

  7. #7
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Quote Originally Posted by Vespasian_2 View Post
    But itsnt it a "tentative definition" not "tentative declaration"?
    Yes, that's it.
    Quote Originally Posted by C99 Standard
    6.9.2 External object definitions

    Semantics

    1 If the declaration of an identifier for an object has file scope and an initializer, the declaration is an external definition for the identifier.

    2 A declaration of an identifier for an object that has file scope without an initializer, and without a storage-class specifier or with the storage-class specifier static, constitutes a tentative definition. If a translation unit contains one or more tentative definitions for an identifier, and the translation unit contains no external definition for that identifier, then the behavior is exactly as if the translation unit contains a file scope declaration of that identifier, with the composite type as of the end of the translation unit, with an initializer equal to 0.

    3 If the declaration of an identifier for an object is a tentative definition and has internal linkage, the declared type shall not be an incomplete type.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Changing varable value from boost::variable_map
    By baxy in forum C++ Programming
    Replies: 1
    Last Post: 02-21-2014, 11:17 PM
  2. Replies: 7
    Last Post: 06-02-2013, 10:59 PM
  3. Allowing 1 too many clients
    By carrotcake1029 in forum Networking/Device Communication
    Replies: 2
    Last Post: 03-01-2009, 05:06 PM
  4. sharing a static varable across classes
    By steve1_rm in forum C++ Programming
    Replies: 2
    Last Post: 01-31-2009, 01:17 AM
  5. Help with varable type
    By St0rmTroop3er in forum C++ Programming
    Replies: 4
    Last Post: 09-05-2003, 02:46 PM

Tags for this Thread