Doubt in variable declaration and definition in C

extern int a;
is it a declaration or a definition? the default value for extern type is zero so it can be treated as a "definition"...but why not for
static int a;

Replies

  • gaurav.bhorkar
    gaurav.bhorkar
    When you write extern int a; - it is a declaration not a definition. The extern keyword says that the variable is defined outside the function block.

    The following code will explain this:
    void func( );
    
    void main ( )
    {
        func ( );
    }
    
    void func ( )
    {
        extern int var; //declaring a variable var which is outside the scope of this function (i.e. our global variable)
        printf ("%d", var);
    }
    
    int var = 10; //Defining a global variable. This can be in another file also
    Note that we have declared (and defined) our global variable 'var' outside the func( ) block, and also way below the func ( ).
    Had we defined the global variable 'var' above the func() block, we wouldn't have to declare it using the extern keyword.

You are reading an archived discussion.

Related Posts

#include #include void main() { int a=1,b=1,c=1; int p; clrscr(); p= a++ + b++ || c++ + ++a ; printf("a=%d\n,p=%d",a,p); getch(); } Actual output : a = 2 p =...
hi, this is zatin singhal. actually i want to do communication between two computers using serial port, basically a non IP based communication. It is possible in all languages but...
hi, this is zatin singhal. actually i want to do communication between two computers using serial port, basically a non IP based communication. It is possible in all languages but...
AHem, I rearely post in this section, and this time around i couldnot contain my excitement. i have just now attended a seminar held at our company premises , and...
China: Awesome gentleman builds homemade flying contraption powered by eight motorcycle engines – Boing Boing