Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > why this program is wrong ?

Reply
Thread Tools

why this program is wrong ?

 
 
johnnash
Guest
Posts: n/a
 
      02-28-2008
#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.
 
Reply With Quote
 
 
 
 
Joachim Schmitz
Guest
Posts: n/a
 
      02-28-2008
johnnash wrote:
> #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


 
Reply With Quote
 
 
 
 
johnnash
Guest
Posts: n/a
 
      02-28-2008
On Feb 28, 11:58 pm, "Joachim Schmitz" <nospam.j...@schmitz-
digital.de> wrote:
> johnnash wrote:
> > #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


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).
 
Reply With Quote
 
Joachim Schmitz
Guest
Posts: n/a
 
      02-28-2008
johnnash wrote:
> On Feb 28, 11:58 pm, "Joachim Schmitz" <nospam.j...@schmitz-
> digital.de> wrote:
>> johnnash wrote:
>>> #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

>
> 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.


 
Reply With Quote
 
johnnash
Guest
Posts: n/a
 
      02-28-2008
On Feb 29, 12:05 am, "Joachim Schmitz" <nospam.j...@schmitz-
digital.de> wrote:
> johnnash wrote:
> > On Feb 28, 11:58 pm, "Joachim Schmitz" <nospam.j...@schmitz-
> > digital.de> wrote:
> >> johnnash wrote:
> >>> #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

>
> > 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.


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()
{
....
}
 
Reply With Quote
 
santosh
Guest
Posts: n/a
 
      02-28-2008
johnnash wrote:

> #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
$

 
Reply With Quote
 
johnnash
Guest
Posts: n/a
 
      02-28-2008
On Feb 29, 12:11 am, santosh <santosh....@gmail.com> wrote:
> johnnash wrote:
> > #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
> $


In this program can you please tell me why a's value is first zero is
it because default value for extern is zero ?
 
Reply With Quote
 
santosh
Guest
Posts: n/a
 
      02-28-2008
johnnash wrote:

> On Feb 29, 12:11 am, santosh <santosh....@gmail.com> wrote:
>> johnnash wrote:
>> > #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
>> $

>
> 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.

 
Reply With Quote
 
johnnash
Guest
Posts: n/a
 
      02-28-2008
On Feb 29, 12:20 am, santosh <santosh....@gmail.com> wrote:
> johnnash wrote:
> > On Feb 29, 12:11 am, santosh <santosh....@gmail.com> wrote:
> >> johnnash wrote:
> >> > #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
> >> $

>
> > 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.


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 ?
 
Reply With Quote
 
Micah Cowan
Guest
Posts: n/a
 
      02-28-2008
Joachim Schmitz wrote:
> johnnash wrote:
>> 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.


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).

--
Micah J. Cowan
Programmer, musician, typesetting enthusiast, gamer...
http://micah.cowan.name/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
why why why why why Mr. SweatyFinger ASP .Net 4 12-21-2006 01:15 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
Add/Remove Program Glitch: Asks If I Want To To Remove Wrong Program ? Robert11 Computer Support 6 08-02-2004 09:02 PM
why this program is wrong on Tru64 OS ? jose luis fernandez diaz C++ 2 11-26-2003 07:46 AM
Wrong abt semi-finished program - why no output? Blankdraw C Programming 3 08-10-2003 10:58 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57