Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

why this program is wrong ?

 
 
santosh
Guest
Posts: n/a
 
      02-28-2008
johnnash wrote:

> On Feb 29, 12:20 am, santosh <(E-Mail Removed)> wrote:
>> johnnash wrote:
>> > On Feb 29, 12:11 am, santosh <(E-Mail Removed)> 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 ?


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>

 
Reply With Quote
 
 
 
 
Doug Miller
Guest
Posts: n/a
 
      02-28-2008
In article <(E-Mail Removed)>, johnnash <(E-Mail Removed)> wrote:
>On Feb 29, 12:11 am, santosh <(E-Mail Removed)> wrote:


>> /* file a.c */
>> int a = 0;


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


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


 
Reply With Quote
 
Mark McIntyre
Guest
Posts: n/a
 
      02-28-2008
Micah Cowan wrote:
> 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;


That's not just a declaration tho.
 
Reply With Quote
 
Micah Cowan
Guest
Posts: n/a
 
      02-28-2008
Mark McIntyre wrote:
> Micah Cowan wrote:
>> 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;

>
> 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[s]".

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

--
Micah J. Cowan
Programmer, musician, typesetting enthusiast, gamer...
http://micah.cowan.name/
 
Reply With Quote
 
Joe Wright
Guest
Posts: n/a
 
      02-29-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.


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;
}

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
 
Reply With Quote
 
Tejas Kokje
Guest
Posts: n/a
 
      03-01-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 are missing definition of "a".

Tejas Kokje
 
Reply With Quote
 
Martin
Guest
Posts: n/a
 
      03-03-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.


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.

--
Martin

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      03-03-2008
Martin <(E-Mail Removed)> writes:
> 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.

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

--
Keith Thompson (The_Other_Keith) <(E-Mail Removed)>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Martin
Guest
Posts: n/a
 
      03-04-2008
> Martin <(E-Mail Removed)> wrote:
>> or used 'void' if it has none.


On Mon, 03 Mar 2008 17:52:16 -0000, Keith Thompson replied:
> 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.

--
Martin

 
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