Thread: Tentative variable in header file problem

  1. #1
    Registered User
    Join Date
    Sep 2015
    Location
    Slovakia
    Posts
    1

    Tentative variable in header file problem

    Hello. I decided to find out what really tentative variable is. I thought that I understand that, but when I run this code I realized that I don't. Tentative definition without any initialization is initialized to zero, isn't it? I expect that this code is not possible to compile and output from gcc will be something like "Multiple definition error" because:
    If the end of the translation unit is reached and no definition has appeared with an initializer for the identifier, then the tentative definition becomes a full definition, and the object defined has uninitialized (zero-filled) space reserved for it.
    Full definition means that this variable is definied and initialized to zero in this case. That is the reason why I expect Multiple definition error. But It works good and the output was:

    >0<0040da20
    >0<0040da20

    Was that tentative variable mysteriously changed to extern storage class or what? Does it mean that I don't need an extern keyword to share variable among .c files?

    I'm so sorry for ma bad English. I hope you understand my problem. Thanks!

    Code:
    test.h
    
    int a;      //tentative variable without extern
    void test(void);
    
    test.c
    
    #include <stdio.h>
    #include "test.h"
    
    void test(void){
    
        printf(">%d<%p", a,&a);
    }
    
    
    main.c
    
    #include <stdio.h>
    #include "test.h"
    
    int main(void) {
    
    printf(">%d<%p\n", a,&a);
    
         test();
    
    return 0;
    }
    
    
    Last edited by Cpt. Rambler; 09-04-2015 at 02:09 PM.

  2. #2
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,113
    I have absolutely no idea what you mean by a "tentative variable"! There is no such term in C, that I am aware of.

    You are defining "int a" as a global variable. All uninitialized global data is automatically initialized to zero.

    Uninitialized local data picks up garbage values. Local data should ALWAYS be initialized to zero, NULL (Pointers only!), or some legitimate value.

    Another problem is that you are defining it in the header file. Technically you are defining it twice, once in test.c and again in main.c, when test.h is #included into both source files.

    The proper way to do this is in test.h:
    Code:
    extern int a; // declaration of a
    And then "define" it once in only ONE of the files, probably main.c:
    Code:
    int a; // definition of a
    Better still, never define global data! Also, turn on, or turn up the warning level of your compiler to see warnings, or errors!

  3. #3
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    Was that tentative variable mysteriously changed to extern storage class or what?
    O_o

    No. The default for tentative definitions is external linkage which a linker can combine in the case of multiple tentative definitions.

    Does it mean that I don't need an extern keyword to share variable among .c files?
    No. You should preferably use `extern` with an explicit definition.

    The tentative definition mechanic is more of a bandage than an intended feature you should be using.

    Soma
    “Salem Was Wrong!” -- Pedant Necromancer
    “Four isn't random!” -- Gibbering Mouther

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 12-11-2014, 04:53 AM
  2. Why I can not "extern" a variable from header file?
    By meili100 in forum C++ Programming
    Replies: 22
    Last Post: 06-23-2008, 03:58 AM
  3. Replies: 30
    Last Post: 06-19-2006, 12:35 AM
  4. ms vc++ header file problem
    By nextus in forum C++ Programming
    Replies: 4
    Last Post: 02-05-2003, 01:41 AM
  5. Header file problem
    By Unregistered in forum C++ Programming
    Replies: 9
    Last Post: 05-08-2002, 08:24 AM