Defining an extern variable in multiple files in C [duplicate] - c

This question already has answers here:
Why can I define a variable twice in C?
(1 answer)
C - Globally defined vars in different files displaying external linkage without extern modifier
(1 answer)
Advantage of using extern in a header file
(1 answer)
How do I use extern to share variables between source files?
(19 answers)
Closed 1 year ago.
0.c
extern int num;
int main(){
return num;
}
1.c
int num;
2.c
int num = 5;
Above compiles fine with gcc 0.c 1.c 2.c. Why don't I get a multiple definition error during linking?
Isn't int num; under 1.c a full definition?:
tentative definition becomes a full definition if the end of the translation unit is reached and no definition has appeared with an initializer for the identifier.
Or is this undefined behaviour?

Related

Is having multiple tentative definitions in separate files undefined behaviour? [duplicate]

This question already has answers here:
Why can I define a variable twice in C?
(1 answer)
C - Globally defined vars in different files displaying external linkage without extern modifier
(1 answer)
Advantage of using extern in a header file
(1 answer)
How do I use extern to share variables between source files?
(19 answers)
Closed 1 year ago.
0.c
extern int num;
int main(){
return num;
}
1.c
int num;
2.c
int num = 5;
Above compiles fine with gcc 0.c 1.c 2.c. Why don't I get a multiple definition error during linking?
Isn't int num; under 1.c a full definition?:
tentative definition becomes a full definition if the end of the translation unit is reached and no definition has appeared with an initializer for the identifier.
Or is this undefined behaviour?

Function declaration not needed when compiling two C files [duplicate]

This question already has answers here:
Is implicit function declaration legal in C89?
(2 answers)
Implicit function declarations in C
(6 answers)
What is the default C -std standard version for the current GCC (especially on Ubuntu)?
(7 answers)
Closed 3 years ago.
I was trying to simultaneously compile the following C files
file1.c
#include<stdio.h>
int main()
{
foo();
return 0;
}
file2.c
#include<stdio.h>
void foo()
{
printf("Hello");
}
I compiled the two files using the following command in linux
gcc file1.c file2.c -o file
It compiled successfully without any warnings and on running it gave the output as 'Hello'
Shouldn't file1.c require a prototype like void foo(). Is there anything in the C standard regarding this ?
Before C99, C had a thing called implicit declaration that allowed you to do that.
If you didn't specify a declaration for foo and you called foo, it was implicitly declared as int foo();.
This, however, was removed from C99 and subsequent standards.

Why doesn't compiler complain the variable is redefined? [duplicate]

This question already has answers here:
About Tentative definition
(2 answers)
Why does redefining a static global variable give a compile-time error when redefining a global variable does not?
(1 answer)
Closed 6 years ago.
I create a simple test.c file:
#include <stdio.h>
int a;
int a = 100;
void printA(void)
{
printf("a is %d\n", a);
}
Compile it to generate object file:
$ gcc -c test.c
$
It is OK! But per my understanding, the variable a should be redefined, is it right?
A variable is local to block it is defined.Once code in block is executed variable goes out of scope.In your case firstly a is defined globally.In second case it is defined inside a function.Thus, to compiler both a is different in both cases not the same.

How to access variable across files in C? [duplicate]

This question already has answers here:
shared global variables in C
(7 answers)
Closed 9 years ago.
I have a file1.c and file2.c. If I define a variable in file1.c, and assign a value to it, how can I access this variable from file2.c?
How should I declare the variable in file1.c and how to retrieve the value from file2.c?
A.c:
int a;
A.h:
extern int a;
That's how. But don't. It's a bad idea.

Double definition of variable [duplicate]

This question already has answers here:
About Tentative definition
(2 answers)
Closed 9 years ago.
As we know extern declaration of variables can be initialized.
#include<stdio.h>
extern int a=5; //will work as both definition and declaration.
int main()
{
}
Why this programme is compiling and running with no error.
extern int a=5; //will work as both definition and declaration.
int main()
{
}
int a; // isn't it second definition??
C has a concept of a "tentative definition". Most definitions without initializers are "tentative", so an arbitrary number of them are allowed, as long as they don't conflict with each other. At the end of the translation unit, the definition of the variable becomes basically the composite of those (e.g., if one definition says the variable is const and another says it's volatile, the variable will end up as both const and volatile.
A definition with an initializer is never tentative, so only one of them is allowed.

Resources