Home > OS >  Please can anyone explain me this warning while writing a c program(use of extern keyword)
Please can anyone explain me this warning while writing a c program(use of extern keyword)

Time:12-31

file name :- next.c

extern int global_var = 2;

file name :- try.c

#include <stdion.h>
#include "next.c"
void main()
{
printf("%d", global_var);
}

The waring that I am getting

In file include form try.c:2:0:
next.c.1.12: warning: 'globl_var' initialized and declared 'extern'
 extern in global_var = 22;
           ^~~~~~~~~

CodePudding user response:

The warning itself tells you that your code is wrong. The messages can be "translated" into something like: "Declaring a variable as extern and at the same time assigning a value, is wrong.

It seems you have misunderstood the normal way to use include files. You don't do:

#include "next.c"

What you do is

#include "next.h"

That is... a c-file contains source code for a unit. The corresponding h-file contains information about the unit that you want to share with other units (aka c-files).

Try this:

next.h:

extern int global_var;  // Tell other c-files that include next.h
                        // that a int-variable with name global_var
                        // exists

next.c:

int global_var = 2;     // Define and initialize global_var

and in try.c do:

#include "next.h"       // Include next.h to know what the unit next.c
                        // makes available for use in try.c

The above is for a global variable defined by the unit next.c. For functions is pretty much the same.

Assume that next.c implements a function foo that you want try.c to call... Then you do the same, i.e. you write the functions source code in next.c and use next.h to tell other units that the function is available. Like:

next.h:

extern int global_var;  // Tell other c-files that include next.h
                        // that a int-variable with name global_var
                        // exists

void foo(int a, int b);  // Tell other c-files that include next.h
                         // that a function with name foo
                         // exists

next.c:

int global_var = 2;     // Define and initialize global_var

void foo(int a, int b)   // Define foo
{
    ... source code ...
}

and in try.c use it like:

#include "next.h"       // Include next.h to know what the unit next.c
                        // makes available for use in try.c

#include "next.h"       // Include next.h to know what the unit next.c
                        // makes available for use in try.c

int bar()
{
    int x = 0;
    int y = 42;
    foo(x, y);    // Call function foo in unit next.c
    ....
}

CodePudding user response:

extern is used to tell compiler that variable is declared out of scope.

first.c

#include <stdio.h>
#include "second.h"

int main(){

    extern const float pi;

    printf("pi : %f\n" , pi);

    return 0;
}

second.h

const float pi = 3.148;

output

pi : 3.148000

why to use extern?

to prevent local redeclaration of variable and make code more readable and understandable

example error
first.c

#include <stdio.h>
#include "second.h"

int main(){

    extern const float pi;
    const float pi;

    printf("pi : %f\n" , pi);

    return 0;
}

output

first.c: In function 'main':
first.c:7:17: error: redeclaration of 'pi' with no linkage
7 | const float pi;
| ^~
In file included from first.c:2:
second.h:1:13: note: previous definition of 'p
' with type 'float'
1 | const float pi = 3.148;
| ^~

CodePudding user response:

extern tells the compiler that the extern declared symbol is defined in a different source file or rather translation unit .

When you assign a value to an extern symbol, you basically say " the definition is somewhere else, but the definition is [x]"

To fix that, you should "define" the variable somewhere without "extern".

  •  Tags:  
  • c
  • Related