Thread: Quick question, what does the Auto keyword do?

  1. #1
    Registered User HelpfulPerson's Avatar
    Join Date
    Jun 2013
    Location
    Over the rainbow
    Posts
    288

    Quick question, what does the Auto keyword do?

    I saw a complete list of C keywords and I didn't know what auto did or some other keywords did.

    The ones I can remember that I've never understood fully were :

    Code:
    register
    auto
    ...
    Edit : The last one I didn't know was volatile.
    Last edited by HelpfulPerson; 07-21-2013 at 01:46 PM.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    register is a request for the compiler to keep that variable available on the (much faster) registers, internally. Pretty much not used anymore, because the compilers are smarter than in decades past. The compiler is free to set up the variables, as it wishes, in any case.

    auto is the normal (default) variable, we just don't use it. Inside any function, "int i" for instance, is an auto variable, and has limited scope. Contrasts with static variables, which have an extended scope.

    volatile warns the compiler that this variable can have NO assumptions made - typically because another sensor or program, will be changing it, in addition to the current function. Basically, it forces the compiler to re-check it's most current value, before it works with it, even though the function did not change it, since the last time it was given a value.

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Auto, register, static, and extern are the four storage classes that C variables can have, with auto being the default. So most variables are implicitly auto. register variables behave just like auto variabes. Register serves as a hint to the compiler to put a variable in a register instead of on the stack. Most modern compilers treat auto and register variables identically.

    In C++11 auto has a different meaning.


    Volitile is a qualifier that works identially to const. It is used for values that may be modified externally to your program. Unlike in Java, it is not sufficent for multithreaded access.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  4. #4
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    Volitile is a qualifier that works identially to const. It is used for values that may be modified externally to your program. Unlike in Java, it is not sufficent for multithreaded access.
    Huh? Volatile and const are quite different. In fact, almost the opposite.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I think King Mir is referring to its use from a syntax point of view.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Stoned Witch Barney McGrew's Avatar
    Join Date
    Oct 2012
    Location
    astaylea
    Posts
    420
    @Adak: Scope applies to identifiers and storage duration applies to objects. An example:

    Code:
    void f() { static int x; }
    'x' would have block scope (and no linkage), and the object associated with 'x' would have static storage duration.

  7. #7
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by Barney McGrew View Post
    @Adak: Scope applies to identifiers and storage duration applies to objects. An example:
    Code:
    void f() { static int x; }
    'x' would have block scope (and no linkage), and the object associated with 'x' would have static storage duration.
    x would actually have "internal" linkage as opposed to no linkage.
    Types of linkage:
    external: global, non-static vars and funcs
    internal : static vars and funcs
    none: local vars, typedef names, enum consts

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by oogabooga
    x would actually have "internal" linkage as opposed to no linkage.
    Types of linkage:
    external: global, non-static vars and funcs
    internal : static vars and funcs
    none: local vars, typedef names, enum consts
    x is local, so it has no linkage. You're probably thinking of static variables at file scope.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by cyberfish View Post
    Huh? Volatile and const are quite different. In fact, almost the opposite.
    I meant that as a qualifier, it can be used any place const can. As laserlight says, it's got the same syntax.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  10. #10
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by laserlight View Post
    x is local, so it has no linkage. You're probably thinking of static variables at file scope.
    You're right! I wasn't thinking at all, really.

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by Barney McGrew View Post
    @Adak: Scope applies to identifiers and storage duration applies to objects. An example:

    Code:
    void f() { static int x; }
    'x' would have block scope (and no linkage), and the object associated with 'x' would have static storage duration.
    What is "the object associated with 'x'?

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Adak
    What is "the object associated with 'x'?
    The "region of data storage in the execution environment" that is associated with the variable named x, the contents of which represents a value as determined by the type of x.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Adak View Post
    What is "the object associated with 'x'?
    It is the entity which, in that definition, is referred to by the name x. The properties of that object include its location in memory, its type (i.e. int), and its value.

    An object doesn't necessarily have a name. This particular one does.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  14. #14
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Thanks for the definition, grumpy and laserlight.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. "this" keyword question
    By Toonzaka in forum C++ Programming
    Replies: 4
    Last Post: 08-12-2010, 09:32 AM
  2. proxy.pac (proxy auto config) question
    By boreder in forum C Programming
    Replies: 2
    Last Post: 01-20-2009, 03:13 AM
  3. message box LPCWSTR manipulation and keyword question
    By stanlvw in forum Windows Programming
    Replies: 11
    Last Post: 05-27-2008, 12:36 PM
  4. A simple question about "virtual" keyword
    By meili100 in forum C++ Programming
    Replies: 2
    Last Post: 03-07-2008, 01:05 PM
  5. auto keyword
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 03-09-2002, 12:44 PM