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

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.

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?

Defining an extern variable in multiple files in C [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.

Behavior of extern keyword in C [duplicate]

This question already has an answer here:
Why extern int a ; initialization giving error locally but not globally? [duplicate]
(1 answer)
Closed 5 years ago.
I surprised when compiled following program on GCC compiler. It's successfully worked. Compiler gives only warning.
warning: 'i' initialized and declared 'extern' [enabled by default] extern int i = 10; ^
My code:
#include <stdio.h>
//Compiler version gcc 4.9
extern int i = 10;
int main()
{
printf("%d\n",i);
return 0;
}
Output:
10
Why doesn't give compiler error? Is it undefined behavior?
You should not put your main function body in a header, but rather in a .c-file. Vice versa, you should not put extern in a .c-file but only in a header. That is the difference between declaration and definition.
Extern means, make this variable known, but there is no memory to be reserved for it. The compiler now says: Ok, I know you want this variable to be used, but it is only promised to be there, not actually defined.
The compiler anyway does not know about other objects (other .c-files) that might define this variable. So it keeps it to the linker to actually try to gather all variables.
If the linker now does not find that variable elsewhere, it implicitly makes the variable local, but warns about the broken C-standard.

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.

Resources