why this program is wrong ?

Discussion in 'C Programming' started by johnnash, Feb 28, 2008.

  1. johnnash

    johnnash Guest

    #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.
     
    johnnash, Feb 28, 2008
    #1
    1. Advertising

  2. 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
     
    Joachim Schmitz, Feb 28, 2008
    #2
    1. Advertising

  3. johnnash

    johnnash Guest

    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).
     
    johnnash, Feb 28, 2008
    #3
  4. 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.
     
    Joachim Schmitz, Feb 28, 2008
    #4
  5. johnnash

    johnnash Guest

    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()
    {
    ....
    }
     
    johnnash, Feb 28, 2008
    #5
  6. johnnash

    santosh Guest

    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
    $
     
    santosh, Feb 28, 2008
    #6
  7. johnnash

    johnnash Guest

    On Feb 29, 12:11 am, santosh <> 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 ?
     
    johnnash, Feb 28, 2008
    #7
  8. johnnash

    santosh Guest

    johnnash wrote:

    > On Feb 29, 12:11 am, santosh <> 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.
     
    santosh, Feb 28, 2008
    #8
  9. johnnash

    johnnash Guest

    On Feb 29, 12:20 am, santosh <> wrote:
    > johnnash wrote:
    > > On Feb 29, 12:11 am, santosh <> 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 ?
     
    johnnash, Feb 28, 2008
    #9
  10. johnnash

    Micah Cowan Guest

    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/
     
    Micah Cowan, Feb 28, 2008
    #10
  11. johnnash

    santosh Guest

    johnnash wrote:

    > On Feb 29, 12:20 am, santosh <> wrote:
    >> johnnash wrote:
    >> > On Feb 29, 12:11 am, santosh <> 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>
     
    santosh, Feb 28, 2008
    #11
  12. johnnash

    Doug Miller Guest

    In article <>, johnnash <> wrote:
    >On Feb 29, 12:11 am, santosh <> 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).
     
    Doug Miller, Feb 28, 2008
    #12
  13. 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
     
    Mark McIntyre, Feb 28, 2008
    #13
  14. 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.
     
    Mark McIntyre, Feb 28, 2008
    #14
  15. johnnash

    Micah Cowan Guest

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

    (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/
     
    Micah Cowan, Feb 28, 2008
    #15
  16. johnnash

    Joe Wright Guest

    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 ---
     
    Joe Wright, Feb 29, 2008
    #16
  17. johnnash

    Tejas Kokje Guest

    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
     
    Tejas Kokje, Mar 1, 2008
    #17
  18. johnnash

    Martin Guest

    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
     
    Martin, Mar 3, 2008
    #18
  19. Martin <> 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) <>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Mar 3, 2008
    #19
  20. johnnash

    Martin Guest

    > Martin <> 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
     
    Martin, Mar 4, 2008
    #20
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jose luis fernandez diaz

    why this program is wrong on Tru64 OS ?

    jose luis fernandez diaz, Nov 25, 2003, in forum: C++
    Replies:
    2
    Views:
    427
    Stephan Br?nnimann
    Nov 26, 2003
  2. Blankdraw
    Replies:
    3
    Views:
    390
    Pieter Droogendijk
    Aug 10, 2003
  3. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    903
    Mark Rae
    Dec 21, 2006
  4. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,968
    Smokey Grindel
    Dec 2, 2006
  5. goosen_cug
    Replies:
    9
    Views:
    326
    Dan Bloomquist
    Nov 10, 2006
Loading...

Share This Page