Like Tree2Likes
  • 1 Post By Barney McGrew
  • 1 Post By grumpy

Quick question, what does the Auto keyword do?

This is a discussion on Quick question, what does the Auto keyword do? within the C Programming forums, part of the General Programming Boards category; I saw a complete list of C keywords and I didn't know what auto did or some other keywords did. ...

  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,017
    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,167
    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
    21,311
    I think King Mir is referring to its use from a syntax point of view.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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.
    laserlight likes this.

  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
    21,311
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Apr 2006
    Posts
    2,017
    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
    21,311
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Registered User
    Join Date
    Jun 2005
    Posts
    6,168
    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.
    King Mir likes this.
    Right 98% of the time, and don't care about the other 3%.

  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, 02: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, 12:05 PM
  5. auto keyword
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 03-09-2002, 11:44 AM

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