Multiple declarations in differents files

J

Jean-Claude Arbaut

I have two questions:

* is it allowed to have "int c=1;" in some file and "int c;" in another,
with no other declaration or definition of c ? If I understand the standard,
the second declaration is a "tentative definition", and it implies
initialization to 0, so there should be a multiple definition error. But
with gcc (and I was told, with other compilers as well), it works without
complain.

* is it allowed to have two declarations/definitions in different files,
with different types, e.g. "int c;" "extern char c;". The standard says
"common" variables represent the same object, but I am in doubt. Other
discussions showed that declarations with different types are allowed, but
this case (int/char) was not discussed. And again gcc does not complain.
 
F

forayer

i dont think diff types shud b allowed. chk out the fol:

file 1:
#include <stdio.h>

int x=1;

void readX();

int main()
{
readX();
printf("X=%d\n", x);
return 0;
}



file 2:
#include <stdio.h>

extern int x;
/* extern char x; */
/* extern double x; */

void readX()
{
/* make correspondin changes to scanf & printf */
scanf("%d", &x);
printf("read->%d\n", x);
}



the o/p in each case:

-- extern int x; --
1
read->1
X=1

-- extern char x; --
2
read->2
X=50

-- extern double x; --
2.5
read->2.5
X=0


the "represents same obj" thing stands. try init 'X' in file 1, u'll
get the same results! i use gcc v3.3.4
 
J

Jean-Claude Arbaut

Le 19/06/2005 09:33, dans
(e-mail address removed), « forayer »
i dont think diff types shud b allowed. chk out the fol:

file 1:
#include <stdio.h>

int x=1;

void readX();

int main()
{
readX();
printf("X=%d\n", x);
return 0;
}



file 2:
#include <stdio.h>

extern int x;
/* extern char x; */
/* extern double x; */

void readX()
{
/* make correspondin changes to scanf & printf */
scanf("%d", &x);
printf("read->%d\n", x);
}

It's more or less what I tried with gcc4.


The results are not very surprising:
the o/p in each case:

-- extern int x; --
1
read->1
X=1

expected (same type)

-- extern char x; --
2
read->2
X=50

Quite reasonnable on a little-endian machine:
The '2' char has ASCII number 50, other bytes of x were 0 so you end up with
integer 0.
-- extern double x; --
2.5
read->2.5
X=0

Still expected on LE machine: 2.5 needs only two bits of mantissa, all the
following bits are 0, so the int x is 0.
the "represents same obj" thing stands. try init 'X' in file 1, u'll
get the same results! i use gcc v3.3.4

Well, I *think* I was confused by the signification of "object", but I can't
found where exactly it hurts the standard.
 
L

Lawrence Kirby

I have two questions:

* is it allowed to have "int c=1;" in some file and "int c;" in another,
with no other declaration or definition of c ?

No this invokes undefined behaviour.
If I understand the standard,
the second declaration is a "tentative definition", and it implies
initialization to 0, so there should be a multiple definition error. But
with gcc (and I was told, with other compilers as well), it works without
complain.

Undefined behaviour means that the code is wrong but it doesn't require
the compiler to detect it.
* is it allowed to have two declarations/definitions in different files,
with different types, e.g. "int c;" "extern char c;". The standard says
"common" variables represent the same object, but I am in doubt. Other
discussions showed that declarations with different types are allowed, but
this case (int/char) was not discussed. And again gcc does not complain.

Again, undefined behaviour ebcause declarations for the same object or
function are required to have compatibe types. And, yes, as far as C is
concerned they do refer to the same object.

Lawrence
 
J

Jean-Claude Arbaut

Le 19/06/2005 14:26, dans (e-mail address removed),
« Lawrence Kirby » said:
No this invokes undefined behaviour.


Undefined behaviour means that the code is wrong but it doesn't require
the compiler to detect it.


Again, undefined behaviour ebcause declarations for the same object or
function are required to have compatibe types. And, yes, as far as C is
concerned they do refer to the same object.

Lawrence

Thanks a lot. It makes much more sense to me now.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top