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.
main.c:
Code:
#include <stdio.h>
int value = 0; //definition
int main(void)
{
printf("%d",value);
IncrementValue();
printf("%d",value);
return 0;
}
increment.c:
Code:
#include <stdio.h>
extern int value; //decleration
void IncrementValue();
void IncrementValue()
{
value++;
}
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?