Automatic variable

This is a discussion on Automatic variable within the C Programming forums, part of the General Programming Boards category; Hello everyone, I have searched and found the term automatic variable means function local variable, compared with static and global ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    1,579

    Automatic variable

    Hello everyone,


    I have searched and found the term automatic variable means function local variable, compared with static and global variable. Is that correct?


    thanks in advance,
    George

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, that's what it means.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    It means a variable that will be destroyed as soon as it goes out of scope (i.e. not a pointer, since you manually have to free() them)

    I think I've also seen an auto keyword, although I don't see why you would ever need to use it since it's auto by default?

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, auto is a keyword in C. It doesn't provide much usefullness tho'. In Bjarne's book, it says "If you absolutely must be explicit about this, C++ provides the redundant keyword auto" [This is the ONLY reference to auto anywhere in the book].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,246
    Quote Originally Posted by cpjust View Post
    It means a variable that will be destroyed as soon as it goes out of scope (i.e. not a pointer, since you manually have to free() them)

    I think I've also seen an auto keyword, although I don't see why you would ever need to use it since it's auto by default?
    I, for one, am glad for the presence of the auto keyword. There are rumblings afoot that this keyword will be co-opted to a new use in the new C++ standard. It will mean "whatever type makes this declaration valid." Should prove extremely useful for metaprogramming.

    Example

    Code:
    auto x = boost::some_crazy_object();
    Where the type of boost::some_crazy_object() might be some enormous 5000-character long template typename, which you neither care to infer nor could even if you wanted to. "auto" in this case means "that type," so you can easily declare x without having to figure out what exact type is being used.

    Also, I hope they allow it for return types from template functions (allowing it for normal functions would be unreasonable and probably impossible to implement):

    Code:
    template <typename T>
    auto my_func(T blah)
    {
        return some_crazy_thing(blah);
    }
    Where the return type is whatever type some_crazy_thing(blah) is.

    Can't wait!

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by brewbuck View Post
    I, for one, am glad for the presence of the auto keyword. There are rumblings afoot that this keyword will be co-opted to a new use in the new C++ standard. It will mean "whatever type makes this declaration valid." Should prove extremely useful for metaprogramming.

    Example

    Code:
    auto x = boost::some_crazy_object();
    Where the type of boost::some_crazy_object() might be some enormous 5000-character long template typename, which you neither care to infer nor could even if you wanted to. "auto" in this case means "that type," so you can easily declare x without having to figure out what exact type is being used.

    Also, I hope they allow it for return types from template functions (allowing it for normal functions would be unreasonable and probably impossible to implement):

    Code:
    template <typename T>
    auto my_func(T blah)
    {
        return some_crazy_thing(blah);
    }
    Where the return type is whatever type some_crazy_thing(blah) is.

    Can't wait!
    I highly doubt they would change the meaning of auto since it would completely break backwards compatibility with C & current C++.

  7. #7
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,246
    Quote Originally Posted by cpjust View Post
    I highly doubt they would change the meaning of auto since it would completely break backwards compatibility with C & current C++.
    It's being seriously discussed. Might not happen but I don't "highly doubt" it.

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    It would break backwards compatibility with existing C and C++ programs that actually use the auto keyword. I don't know if this is better than introducing a new keyword or not, because a new keyword could have been used for a variable name in existing code.

    Or the new keyword could be named something like _Bool. But that is hard to type and so they introduced a new header for bool, an easier name to swallow.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #9
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> There are rumblings afoot that this keyword will be co-opted to a new use in the new C++ standard.

    off-topic, but in addition to this I believe a typeof keyword is being considered as well, eg:

    Code:
    typedef typeof(insane_template_instantiation) type;
    or was I just daydreaming?
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. variable being reset
    By FoodDude in forum C++ Programming
    Replies: 1
    Last Post: 09-15-2005, 12:30 PM
  2. float/double variable storage and precision
    By cjschw in forum C++ Programming
    Replies: 4
    Last Post: 07-28-2003, 06:23 PM
  3. Beginner question
    By Tride in forum C Programming
    Replies: 30
    Last Post: 05-24-2003, 08:36 AM
  4. static variable
    By Roaring_Tiger in forum C Programming
    Replies: 9
    Last Post: 04-01-2003, 12:12 PM
  5. Variable question I can't find answer to
    By joelmon in forum C++ Programming
    Replies: 3
    Last Post: 02-12-2002, 03:11 AM

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