Thread: auto data type

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    1

    auto data type

    Can anyone explain me what is auto data type in C?

    Thanks in advance

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    No.

    There is no auto data type in C.

    There is an auto keyword. It doesn't do anything, except perhaps explicitly state that you do not want the variable it applies to to be static.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    From MSDN:
    Quote Originally Posted by http://msdn.microsoft.com/en-us/library/6k3ybftz(VS.80).aspx
    auto Keyword

    The auto storage-class specifier declares an automatic variable, a variable with a local lifetime. It is the default storage-class specifier for block-scoped variable declarations.

    auto declarator ;

    Remarks

    An auto variable is visible only in the block in which it is declared. Declarations of auto variables can include initializers, as discussed in Initialization. Since variables with auto storage class are not initialized automatically, you should either explicitly initialize them when you declare them, or assign initial values to them in statements within the block. The values of uninitialized auto variables are undefined. (A local variable of auto or register storage class is initialized each time it comes in scope if an initializer is given.)

    An internal static variable (a static variable with local or block scope) can be initialized with the address of any external or static item, but not with the address of another auto item, because the address of an auto item is not a constant.

    Few programmers use the auto keyword in declarations because all block-scoped objects not explicitly declared with another storage class are implicitly automatic. Therefore, the following two declarations are equivalent:
    Code:
    // auto_keyword.cpp
    int main()
    {
       auto int i = 0;    // Explicitly declared as auto.
       int j = 0;    // Implicitly auto.
    }
    "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

  4. #4
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    I've often wondered why they even have an auto keyword? Are there ANY circumstances when you'd actually need to use it?
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Well, C++ is going to use auto for variables whose type is automatically determined from the initialization:

    Code:
    auto i = function_returning_enormously_complicated_templated_object();
    In C it probably wouldn't make much sense, since you won't have types which are mind-bogglingly complicated. So it's just a historic relict?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  6. #6
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    > So it's just a historic relict?
    Or a futuristic relict

  7. #7
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I once used a compiler that for whatever freaking reason did not automatically assume all locals are auto. So for all of the folks who wonder how come people are always spouting off about the standards for both C and C++, its little things like this that are where it all begins. I would hardly consider this a desireable behavior for a compiler to demonstrate.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. how to make our own data type ?
    By Masterx in forum C++ Programming
    Replies: 6
    Last Post: 11-21-2008, 05:04 AM
  3. Replies: 0
    Last Post: 03-20-2008, 07:59 AM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. C diamonds and perls :°)
    By Carlos in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 05-16-2003, 10:19 PM