automatic variables

This is a discussion on automatic variables within the C Programming forums, part of the General Programming Boards category; I like C, but I have never had to use "automatic variables." I am going through my Petzold book and ...

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    51

    automatic variables

    I like C, but I have never had to use "automatic variables." I am going through my Petzold book and it mentions static and automatic variables. Does C have automatic varaibles, or is this something specific to windows programming? I vaguely remember them in prolog.

    Thanks in advance

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,799
    Yes, C has automatic variables. You've probably used them in almost every single C program you ever wrote without realising it. These are the same thing:

    Code:
    auto int var;
    and...

    Code:
    int var;
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    51
    In the book he mentions Local automatic variables being unique to each thread. How do you declare these?

  4. #4
    Registered User
    Join Date
    Mar 2004
    Posts
    494
    a local variable is visible within a block {},has block scope, for example.
    Code:
    int x =0;  /* this is a global variable*/
    int main() 
    { 
    /* whereas */
    
    int i; /*local variable*/
    .
    .
    .
    return 0;
    }
    Last edited by InvariantLoop; 03-08-2005 at 11:10 AM.
    When no one helps you out. Call google();

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    A variable is local to the code block it resides in. If it doesn't reside inside a block (pair of braces), it is a global variable. To further the point:
    Code:
    void foo( void )
    {
        /* variables declared here are local to foo */
        int localtofoo;
    
        {
            /* variables declared here are local to this inner block in foo */
            int localtoinnerfoo;
    
            {
                /* variables declared here are local to this inner block... */
                int localtothisinnerfoo;
    
            }
    
            /* localtothisinnerfoo is now gone... */
    
        }
    
        /* localtoinnerfoo is now gone... */
    
    }
    They're not global, meaning, they're not available outside this function. If we had declared a variable outside this function, (ie: global) it could be accessed from all parts within the function. They're also not static, and as such, they go out of scope when their code block ends, and you cannot return pointers to them to use them further.

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    51

    Thank you for the help

    Since in the book he writes:

    Local automatic varaibles in a function are unique to
    each thread, because they are stored on the stack and
    each hread has it's own stack.

    I took it that automatic varaibles were something else. Thank you for clearing that up for me.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 15
    Last Post: 09-30-2008, 02:12 AM
  2. Automatic variables in external structure definition
    By Boxknife in forum C Programming
    Replies: 2
    Last Post: 06-23-2008, 12:56 PM
  3. Static vs. Automatic Variables
    By Trekkie in forum C++ Programming
    Replies: 5
    Last Post: 11-14-2005, 01:55 PM
  4. why automatic variables are assigned with garbage values
    By srinivasg in forum C Programming
    Replies: 1
    Last Post: 11-08-2005, 06:14 AM
  5. Automatic vs. Static Duration
    By JMB in forum C++ Programming
    Replies: 7
    Last Post: 10-14-2002, 07:16 PM

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