M
maxw_cc
Hi to all,
To explain my question I'll help myself
with the following two code snippets:
/*** file1.c ***/
#include <stdio.h>
void print_it(void);
int i;
int main(void)
{
print_it();
printf("main i = %d\n", i);
return 0;
}
/*** file2.c ***/
#include <stdio.h>
int i;
void print_it(void)
{
i = 2;
printf("print_it i = %d\n", i);
}
As you can see I have a
tentative definition for object `i',
in both TUs.
According to C99 6.9.2 p2, one can deduce
that both of these tentative definitions, will
turn into real definitions at the end of the
TUs with initializer zero.
Then I think that in this case I will be having,
*two definitions* for the same identifier with
external linkage!
Then I would be violating a semantics constraint.
Specifically the one pointed out in C99 6.9 p5.
To my surprise, I didn't receive any diagnostics
from the compiler. I compiled this program using
this command line:
$ gcc -g -Wall -ansi -pedantic -o filex file1.c file2.c
I even ran the executable generated, and it ran OK:
$ filex
print_it i = 2
main i = 2
What am I getting wrong?
What is my error?
What am I misunderstanding here?
Thank you very much in advance,
Max
To explain my question I'll help myself
with the following two code snippets:
/*** file1.c ***/
#include <stdio.h>
void print_it(void);
int i;
int main(void)
{
print_it();
printf("main i = %d\n", i);
return 0;
}
/*** file2.c ***/
#include <stdio.h>
int i;
void print_it(void)
{
i = 2;
printf("print_it i = %d\n", i);
}
As you can see I have a
tentative definition for object `i',
in both TUs.
According to C99 6.9.2 p2, one can deduce
that both of these tentative definitions, will
turn into real definitions at the end of the
TUs with initializer zero.
Then I think that in this case I will be having,
*two definitions* for the same identifier with
external linkage!
Then I would be violating a semantics constraint.
Specifically the one pointed out in C99 6.9 p5.
To my surprise, I didn't receive any diagnostics
from the compiler. I compiled this program using
this command line:
$ gcc -g -Wall -ansi -pedantic -o filex file1.c file2.c
I even ran the executable generated, and it ran OK:
$ filex
print_it i = 2
main i = 2
What am I getting wrong?
What is my error?
What am I misunderstanding here?
Thank you very much in advance,
Max