why this program is wrong ?

J

johnnash

#include<stdio.h>

extern int a;

call()
{

a = 3;

}

main()
{

call();

printf("%d",a);

}

I thought once you declare a variable as extern it can be seen
anywhere in the program.
 
J

Joachim Schmitz

johnnash said:
#include<stdio.h>

extern int a;

call()
{

a = 3;

}

main()
{

call();

printf("%d",a);

}

I thought once you declare a variable as extern it can be seen
anywhere in the program.
You declared ("somewhere out there is a variable called a and it is an int")
it but didn't define it.
Drop the extern, it's not needed here. It would be needed in a different
module (file) though.

Bye, Jojo
Bye, Jojo
 
J

johnnash

You declared ("somewhere out there is a variable called a and it is an int")
it but didn't define it.
Drop the extern, it's not needed here. It would be needed in a different
module (file) though.

Bye, Jojo
Bye, Jojo

So you mean to say extern is only to be used in .h files for
declaration purpose so that we can define the variable in the .c
files(which includes the .h file).
 
J

Joachim Schmitz

johnnash said:
So you mean to say extern is only to be used in .h files for
declaration purpose so that we can define the variable in the .c
files(which includes the .h file).
Yes.
You can of course use extern in .c files too, instead if #include a header
that does it.
 
J

johnnash

Yes.
You can of course use extern in .c files too, instead if #include a header
that does it.

how ? like declare it at the top of the program and then define it
within a function ?

#include "stdio.h"

extern int x;
...........
...........

a1()

{
int x;
x= 3;
}

a2()
{
....
}
 
S

santosh

johnnash said:
#include<stdio.h>

extern int a;

call()
{

a = 3;

}

main()
{

call();

printf("%d",a);

}

I thought once you declare a variable as extern it can be seen
anywhere in the program.

There is no definition of 'a.' The extern declaration merely informs the
compiler that an int object called 'a' is defined elsewhere in the
program. This is to generate proper code when 'a' is involved. You must
still supply a definition of 'a' either after the declaration in the
same file or in some other translation unit that you would link
together to create the program.

Try this:

/* file a.h */
extern int a;
void call(void);

/* file a.c */
int a = 0;
void call(void) {
a = 3;
}

/* file main.c */
#include <stdio.h>
#include "a.h"

int main(void) {
printf("a = %d\n", a);
call();
printf("After call(), a is now: %d\n", a);
return 0;
}

You must compile a.c and main.c separately and supply both files to the
linker. For gcc one possible sequence of commands is:

$ gcc -Wall -ansi -pedantic -c a.c
$ gcc -Wall -ansi -pedantic -c main.c
$ gcc -Wall -ansi -pedantic -o test a.o main.o
$ ./test
a = 0
After call(), a is now: 3
$
 
J

johnnash

There is no definition of 'a.' The extern declaration merely informs the
compiler that an int object called 'a' is defined elsewhere in the
program. This is to generate proper code when 'a' is involved. You must
still supply a definition of 'a' either after the declaration in the
same file or in some other translation unit that you would link
together to create the program.

Try this:

/* file a.h */
extern int a;
void call(void);

/* file a.c */
int a = 0;
void call(void) {
a = 3;

}

/* file main.c */
#include <stdio.h>
#include "a.h"

int main(void) {
printf("a = %d\n", a);
call();
printf("After call(), a is now: %d\n", a);
return 0;

}

You must compile a.c and main.c separately and supply both files to the
linker. For gcc one possible sequence of commands is:

$ gcc -Wall -ansi -pedantic -c a.c
$ gcc -Wall -ansi -pedantic -c main.c
$ gcc -Wall -ansi -pedantic -o test a.o main.o
$ ./test
a = 0
After call(), a is now: 3
$

In this program can you please tell me why a's value is first zero is
it because default value for extern is zero ?
 
S

santosh

johnnash said:
In this program can you please tell me why a's value is first zero is
it because default value for extern is zero ?

Yes. If you don't explicitly initialise a static object, whatever scope
it has, it is initialised to zero at program start-up. Here I just
initialised it to zero, unnecessary, I'll admit.
 
J

johnnash

Yes. If you don't explicitly initialise a static object, whatever scope
it has, it is initialised to zero at program start-up. Here I just
initialised it to zero, unnecessary, I'll admit.

What is a static object ? but even in that program, a is not defined
until call function where a is defined and assigned the value of 3, am
i right ? So how can you print value of variable if its not even
defined ?
 
M

Micah Cowan

Joachim said:

No.

extern int a = 0;

would have been okay too (though more typing than is necessary).

A variable declaration (with a complete type) without the "extern"
specifier is a definition.
A variable declaration _with_ the "extern" specifier, and
initialization, is a definition.
A variable declaration with the "extern" specifier, and no
initialization, is not a definition (refers to an object that
should be defined elsewhere).
 
S

santosh

johnnash said:
What is a static object ?

It is an object that persists throughout the lifetime of the program and
retains it's last stored value. There are different ways to declare a
static object.

When you declare a variable outside of any function it becomes a file
scope static object. It becomes accessible to all the following
portions of that source file and, with proper extern declarations, in
other modules as well.

When you declare a variable within a function or a block like this:

static TYPE NAME;

it defines a static object that is visible only to that function or
block, but retains it's last stored value even after that function or
block is left by the flow of execution and perhaps reentered later.
but even in that program, a is not defined
until call function where a is defined and assigned the value of 3, am
i right ?

'a' is defined in a.c. The declaration in a.h is meant for inclusion in
other files so that those files know about 'a' and can access it. 'a'
is defined in line 2 of a.c. It is assigned the value 3 in line 4.
So how can you print value of variable if its not even
defined ?

It is defined in a.c whose object code you include in the final
executable. It is not defined in main.c, but the declaration of 'a' in
a.h, which is included in main.c on line 3 ensures that the compiler
can successfully translate main.c and emit the correct object code for
the two printf statements.

The whole purpose of extern is to announce the properties of objects
which are not defined in the current scope.

Maybe you should read a C tutorial. A good one is:

<http://www.eskimo.com/~scs/cclass/cclass.html>
 
D

Doug Miller

In this program can you please tell me why a's value is first zero is
it because default value for extern is zero ?

No, it's zero because it was explicitly initialized to zero (see above).
 
M

Mark McIntyre

johnnash said:
how ? like declare it at the top of the program and then define it
within a function ?

No, that would be defining a different variable with the same name
because it has a different scope.


You can do it like this

file1.c
extern int x; //declaration

file2.c

int x; //definition
 
M

Micah Cowan

Mark said:
That's not just a declaration tho.

Isn't that my point? The claim was that "extern us only to be used ...
for declaration purpose".

(Perhaps you read it as "In .h files, extern should only be used for
declaration purposes"; but that would not make sense in the context of
the previous posts.)
 
J

Joe Wright

johnnash said:
#include<stdio.h>

extern int a;

call()
{

a = 3;

}

main()
{

call();

printf("%d",a);

}

I thought once you declare a variable as extern it can be seen
anywhere in the program.

Your program is better written like this..

#include <stdio.h>

int a;

void call(void) {
a = 3;
}

int main(void) {
call();
printf("%d\n", a);
return 0;
}
 
T

Tejas Kokje

johnnash said:
#include<stdio.h>

extern int a;

call()
{

a = 3;

}

main()
{

call();

printf("%d",a);

}

I thought once you declare a variable as extern it can be seen
anywhere in the program.

You are missing definition of "a".

Tejas Kokje
 
M

Martin

johnnash said:
#include<stdio.h>

extern int a;

call()
{

a = 3;

}

main()
{

call();

printf("%d",a);

}

I thought once you declare a variable as extern it can be seen
anywhere in the program.

It's *wrong* for several reasons. You haven't defined a return type for
the function 'call'. You haven't defined a return type for 'main', or
specified its parameters, or used 'void' if it has none. You haven't
returned an exit status from 'main'. 'a' has been declared but not defined.
 
K

Keith Thompson

Martin said:
It's *wrong* for several reasons. You haven't defined a return type
for the function 'call'.

Legal (but poor style) in C90 (the return type is implicitly int), but
you're right for C99 (implicit int was dropped).
You haven't defined a return type for
main',

Legal (but poor style) in C90 (the return type is implicitly int), but
you're right for C99 (implicit int was dropped).
or specified its parameters,

"int main(void)" is valid. Whether "int main()" is equivalent turns
out to be a rather subtle question; arguments have been made both
ways. But certainly "int main(void)" is better than "main()", or even
"int main()" -- unless for some reason you need portability to ancient
pre-ANSI compilers that don't support prototypes and the "void"
keyword.
or used 'void' if it has
none.

The return type of main is int (unless the implementation allows and
documents a different return type, but there's no good reason not to
use int).
You haven't returned an exit status from 'main'.

In C90, this causes an undefined status to be returned to the calling
environment; strictly speaking, it's not incorrect. In C99, falling
off the end of main() causes a status of 0 to be returned implicitly.
Adding a "return 0;" would be a good idea in any case.
'a' has been
declared but not defined.

<Adam Savage>Well, there's your problem!</Adam Savage>
 
M

Martin

Martin said:
The return type of main is int (unless the implementation allows and
documents a different return type, but there's no good reason not to
use int).

Keith, I was referring to the parameter list. I would never advocate
declaring main with a return type of void. It's just not cricket. ;-)
 

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,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top