Thread: Storage class 'extern' necassary?

  1. #1
    Registered User
    Join Date
    May 2017

    Storage class 'extern' necassary?

    The task is to create two .c-files and try to use a global variable with storage class extern, e.g. 'extern int value', to see the use of it or how it works etc. I wrote following simply program.

    #include <stdio.h>
    int value = 0; //definition
    int main(void)
      return 0;
    #include <stdio.h>
    extern int value; //decleration
    void IncrementValue();
    void IncrementValue()
    The thing is we were told to only use two .c-files and not even a .h-file. Because according to c - How do I use extern to share variables between source files? - Stack Overflow you only sould declare extern-variables in a h-file.

    And I didn't really give much attention to the text beyond the guidelines, because apperently it could confuse beginners or so)

    And the thrid answer here: How do I share variables between different .c files? - Stack Overflow describes the style of my program too. So, if you only wirte "int value;" you get a tentative definition and with extern you do suppress this. I.e. in general you only write one time "int value;" in any c.file overall to avoid multiply tentative definitions, instead you write in all c.files then "extern int value" so that these c-files know that there is this variable with any value. But the weird thing is, it even works without extern, i.e. with several tentative definitions.

    Is that right or did I confuse something?
    Last edited by quarkse; 05-24-2017 at 07:49 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    Well with only two source files, the solution is a rather artificial example where it's easy to get away without a header file.

    But in any larger program, putting the extern in the header file is the way to ensure consistency across all compilation units.

    Quote Originally Posted by c99 draft
    6.9.2 External object definitions
    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.
    Index of /jtc1/sc22/wg14/www/docs/n869
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2013
    Quote Originally Posted by quarkse View Post
    So, if you only wirte "int value;" you get a tentative definition and with extern you do suppress this.
    For "int value" without the extern, the source code is declaring an actual instance of a global variable. For "extern int value", the source code is declaring that value refers to an external integer. Note that C source code can include both "extern int value" and "int value" in the same source code. This simplifies the case where "extern int value" is declared in an include file used by multiple source code files, one of which that also declares the actual instance of "value". In more complicated cases, a combination of #define, #ifdef, #ifndef, ... , are used to work with common include files.
    Last edited by rcgldr; 05-24-2017 at 09:22 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. storage class errors
    By suryak in forum C Programming
    Replies: 4
    Last Post: 10-16-2011, 02:42 AM
  2. static storage class
    By roaan in forum C Programming
    Replies: 4
    Last Post: 09-08-2009, 04:57 PM
  3. storage class specifier
    By -EquinoX- in forum C Programming
    Replies: 5
    Last Post: 11-06-2008, 04:07 PM
  4. storage class specified for parameter
    By mart_man00 in forum C Programming
    Replies: 2
    Last Post: 06-01-2003, 02:44 PM
  5. Storage Class
    By Blanket in forum C++ Programming
    Replies: 5
    Last Post: 04-15-2003, 09:54 PM

Tags for this Thread