NO

Author Topic: Is extern declared by default ?  (Read 3742 times)

post from old forum

  • Guest
Is extern declared by default ?
« on: September 13, 2004, 09:47:40 PM »
Hello Pelle,

int iTest is not declared as extern
in testmodule.c, but it is used like this.
(output of the example prog: 12345)

Is extern default?

Holger.


main.c
------------------------
#include <stdio.h>
#include "test.h"
int iTest;

int main(int argc, char *argv<>)
{
iTest=1111;
test();
printf("iTest: %d",iTest);

return 0;
}
----------------------------

testmodule.c
-------------------------
#include "test.h"
int iTest; //not declared as extern !
void test(void){
iTest=12345;
}
----------------------

test.h
-------------------
//emty line
void test(void);
---------------------

Holger Buick

post from old forum

  • Guest
Is extern declared by default ?
« Reply #1 on: September 13, 2004, 09:48:05 PM »
Hello Holger,
Yes, in this context it is. If you want the variable to be visible in one file only, use static: 'static int iTest'.

Pelle

post from old forum

  • Guest
Is extern declared by default ?
« Reply #2 on: September 13, 2004, 09:48:31 PM »
Hello Pelle,
thanks,

Holger.

post from old forum

  • Guest
Is extern declared by default ?
« Reply #3 on: September 13, 2004, 09:49:08 PM »
Hello Pelle,
when reading in a book, that if extern is not declared, the linker
should give a warning,
I tried the code with an old LCC version.
Here the result was the same as with PellesC.

But with Boland compiler it gives a warning and the
output is 1111 !
(Compiled as C++, I think).

The problem arrived, when I was wondering, why my
windowhandler was corrupt, what itself was difficult do
discover. So messages were sent to the wrong window,
because I had declared the HWND with a same name in another
module, what mixed up the values of the handlers.

Holger.


-------- V IDE --------

D:\Progs\Borland\BCC55\bin\make -fD:\Progs\Vide\projects\test\Makefile.v
> MAKE Version 5.2 Copyright (c) 1987, 2000 Borland
> D:\Progs\Borland\BCC55\bin\ILink32 @MAKE0000.@@@
> Turbo Incremental Link 5.00 Copyright (c) 1997, 2000 Borland
! Warning: Public symbol '_iTest' defined in both module D:\PROGS\VIDE\PROJ
* ECTS\TEST\TESTMODULE.OBJ and D:\PROGS\VIDE\PROJECTS\TEST\MAIN.OBJ
---- Make finished ----

post from old forum

  • Guest
Is extern declared by default ?
« Reply #4 on: September 13, 2004, 09:49:39 PM »
Hello Holger,

If you use 'extern int varname;', and don't define 'varname' anywhere, you get a linker error. If you just use 'int varname;', 'varname' will also be globally visible, but with slightly different attributes. In this case, all 'varname' will be grouped together, and the largest size will be used (if you also use, for example, 'double varname;').

It *might* be possible to add a warning, but I'm afraid this can cause unwanted warnings too. False warnings are just as bad as no warnings. I feel it's too late to do anything in version 2.90, but I will look at this for a future version.

Pelle