another noob question

This is a discussion on another noob question within the C Programming forums, part of the General Programming Boards category; Howdy agin, I am onto scope and storage classes now, My question is if i declare a global variable in ...

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    14

    another noob question

    Howdy agin,

    I am onto scope and storage classes now, My question is if i declare a global variable in 1.c and want to use it in 2.c do I have to use the extern specifier? I am just not understanding how the use an allusion.



    Code:
    int x = 0;          /*global variable */
    extern int y;    /* an allusion to a  global variable */
    when they say an allusion, do they mean it just mirrors over?

    Thank you agin for your time.

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,184
    Yes, you do.
    The compiler does not see the contents if 1.c, so for 2.c to actually use variables in 1.c, the compiler must be aware of those variables.
    So to do that, we tell the compiler that these variables exist by declaring them exactly as they are defined in the other source file (without initializing them) and slapping "extern" before them.
    But you do know that globals variables are dangerous, yes?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  3. #3
    Registered User
    Join Date
    Dec 2008
    Posts
    14
    Yes, I have been reading the boards here alot, and know that global vairables and gets is bad :O)
    I just figured that to really know C, i shouldn't skip over the parts that are bad, know how to use them but dont.

    Thank you very much for the fast answer!!!!

  4. #4
    Registered User
    Join Date
    Dec 2008
    Posts
    14
    Another noob question if anyone has the time.

    I understand why y increase's becase it is permenant (static) but why wouldnt x increase also?

    Code:
    #include <stdio.h>
    
    main() {
        
        int i;
        
        for (i=0; i<5; i++) {
            int x = 0;
            static int y = 0;
            printf("x=%d, y=%d\n", x++, y++);
        }
        
        
        return (0);
    }

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,184
    No, because x incremented AFTER it's printed by printf (postfix). Then x is destroyed since it goes out of scope and reinitialized to 0.
    And main returns int.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. quick noob question
    By thanatos1 in forum C# Programming
    Replies: 2
    Last Post: 06-17-2009, 08:28 PM
  2. Noob printf question
    By lolguy in forum C Programming
    Replies: 3
    Last Post: 12-14-2008, 07:08 PM
  3. Very noob question :(.
    By GamerProduction in forum Tech Board
    Replies: 4
    Last Post: 04-14-2007, 05:40 AM
  4. noob question
    By unclebob in forum C++ Programming
    Replies: 9
    Last Post: 09-24-2006, 08:48 AM
  5. Noob question ( little dos program )
    By Demon1s in forum C++ Programming
    Replies: 13
    Last Post: 04-04-2003, 08:28 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21