Thread: Why is A$ a valid variable name?

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    5

    Why is A$ a valid variable name?

    Can someone help me understand why A$ compiles with Visual Studio 2005 as an int variable with no compiler error?

    Thanks,
    Jeff

    Code:
    main()
    {
       int A$;
       A$ = 17;
    }

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Well heck Jeff... Why not?




    (And BTW... that version of VS is like 4 generations behind the times... VC++ 2010 is the big deal now)

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You're maybe thinking of BASIC where A$ would be a string variable. But C has no string data type. Every string in C is a composite of a char variable, with an end of string marker char. This: BOB is just three char's, not a string. BOB'\0' is a string, but you don't see the '\0' char at all.

    So a variable with a $ at the end of it's name is not indicative of a string or any certain type of variable, at all.

  4. #4
    Registered User
    Join Date
    Sep 2011
    Posts
    5
    Thanks for the quick reply Tater.

    The site C Programming/Variables - Wikibooks, open books for an open world states the following:

    Naming VariablesVariable names in C are made up of letters (upper and lower case) and digits. The underscore character ("_") is also permitted. Names must not begin with a digit. Unlike some languages (such as Perl and some BASIC dialects), C does not use any special prefix characters on variable names.

    A Book on C by Al Kelley and Ira Pohl states

    A variable name, also called an identifier, consists of a seqence of letters, digits, and underscores, but may not start with a digit.

    Nobody seems to talk about '$' being allowed in a C variable name.

    I have VS 2010 on another computer but the one with 2005 has a nicer screen

    Thanks,
    Jeff

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Jeff Hettick View Post
    Thanks for the quick reply Tater.

    The site C Programming/Variables - Wikibooks, open books for an open world states the following:

    Naming VariablesVariable names in C are made up of letters (upper and lower case) and digits. The underscore character ("_") is also permitted. Names must not begin with a digit. Unlike some languages (such as Perl and some BASIC dialects), C does not use any special prefix characters on variable names.

    A Book on C by Al Kelley and Ira Pohl states

    A variable name, also called an identifier, consists of a seqence of letters, digits, and underscores, but may not start with a digit.

    Nobody seems to talk about '$' being allowed in a C variable name.

    I have VS 2010 on another computer but the one with 2005 has a nicer screen

    Thanks,
    Jeff
    If nobody told you to breath would you stop?

    It works... who cares why it works... and what difference would it make if it didn't?

  6. #6
    Registered User
    Join Date
    Sep 2011
    Posts
    5
    Excellent question! I'm taking a C Programming class and the instructor asked in a programming exercise. Technically, I just have to asnwer if the variable compiles correctly. It just bugs me that it compiles correctly, and I can assign values to it like any other variable, but the experts don't mention '$' in variable names.

    Thanks,
    Jeff

  7. #7
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    C99 actually defines what is an acceptable variable name. Look at these:
    The C99 Draft (N869, 18 January, 1999)
    The C99 Draft (N869, 18 January, 1999)

    Technically, with the identifier A$, A is a nondigit and $ is a character that your environment must support. I also used C99 only because I have easy access to its standard and it is recent enough for me. Other versions of the standard have also explained the syntax in similar ways.

    edit - OK, well I want you to look at sections A.1.3 and 5.2.1. I'm not sure why my links are ........ed.
    Last edited by whiteflags; 09-10-2011 at 04:19 PM.

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Jeff Hettick View Post
    It just bugs me that it compiles correctly, and I can assign values to it like any other variable, but the experts don't mention '$' in variable names.

    Thanks,
    Jeff
    Probably because nobody actually uses it...

    The thing is, there's no reason to prevent it... So what's the big deal?

  9. #9
    Registered User
    Join Date
    Sep 2011
    Posts
    5
    WhiteFlags,

    I found section Annex J 5.2 of the C99 specification that states:

    "Characters other than the underscore _, letters, and digits, that are not part of the basic
    source character set (such as the dollar sign $, or characters in national character sets)
    may appear in an identifier (6.4.2)."


    This seems to answer the question. Thanks for the help!

    Jeff

  10. #10
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by Jeff Hettick View Post
    WhiteFlags,

    I found section Annex J 5.2 of the C99 specification that states:

    "Characters other than the underscore _, letters, and digits, that are not part of the basic
    source character set (such as the dollar sign $, or characters in national character sets)
    may appear in an identifier (6.4.2)."


    This seems to answer the question. Thanks for the help!

    Jeff
    Good work, I haven't seen many people that are willing to dig through the standard. Keep up that ethic.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by AndrewHunter View Post
    Good work, I haven't seen many people that are willing to dig through the standard. Keep up that ethic.
    You must be new here.


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

  12. #12
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by quzah View Post
    You must be new here.
    Quzah.
    LOL, above and beyond trying to prove someone wrong, for the pedantic.......
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by AndrewHunter View Post
    LOL, above and beyond trying to prove someone wrong, for the pedantic.......
    :boggle: Isn't that what the standard is for? You're trying to tell me I've been using it wrong this whole time?!


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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Is this valid?
    By binnyshah in forum C++ Programming
    Replies: 4
    Last Post: 07-09-2010, 11:44 AM
  2. Socket file descriptor valid and then not valid
    By Florian in forum C Programming
    Replies: 3
    Last Post: 05-22-2010, 08:23 AM
  3. Is this valid
    By ssharish2005 in forum C Programming
    Replies: 2
    Last Post: 12-25-2007, 03:26 AM
  4. Is this valid?
    By Boksha in forum C++ Programming
    Replies: 2
    Last Post: 06-29-2002, 01:55 PM
  5. Valid variable and function names
    By sean in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 06-29-2002, 12:30 PM