What is the use of the extern keyword in c?

Asked

Viewed 854 times

1

1 answer

3


Dude, after researches I understood that extern serves to indicate that a variable has already been defined in another part of the program as a whole. For example, if you divide a giant code into 2 parts. You should indicate to the compiler that you are using these variables in a block (file 2) but they have been initialized in another block (code 1).

for example:

Code 1:

int count;
float sum;
int main(void)
{
    ...
    return 0;
}

Code 2:

extern int count;
extern float sum;
int RetornaCount(void)
{
    return count;
}

Here’s a link that explains much better than me: http://mtm.ufsc.br/~Azeredo/cursoC/aulas/ca20.html

I hope I’ve helped.

  • Correct. And one of the utilities of the files . h is precisely to contain the statements of variables with extern, to avoid errors of copying and pasting.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.