Thread: can i initialise variable name as '$'?

  1. #1
    kotin
    Join Date
    Oct 2009
    Posts
    132

    can i initialise variable name as '$'?

    Hi,

    Earlier I studied that variable name should follow the below rulse

    "Rules for Constructing Variable Names
    (A) A variable name is any combination of 1 to 31 alphabets,
    digits or underscores. Some compilers allow variable names
    whose length could be up to 247 characters. Still, it would be
    safer to stick to the rule of 31 characters. Do not create
    unnecessarily long variable names as it adds to your typing
    effort.
    (B) The first character in the variable name must be an alphabet or
    underscore.
    (C) No commas or blanks are allowed within a variable name.
    (D) No special symbol other than an underscore (as in gross_sal)
    can be used in a variable name.
    Ex.: si_int
    m_hra
    pop_e_89"





    But

    my doubt is i am able to compile the below program with the integer variable name as '$'

    Code:
    #include<stdio.h>
    int main()
    {
            int $;
            int j;
            $=10;
            printf ("%d\n",$);
            return 0;
    }


    can any one help me to know about this? is is correct variable declaration?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Is this going to be the new 'trick question' of the week? Tired of trying to swap variables without a temp, now they've got everyone asking if '$' is a valid variable name? Because I'm pretty sure someone asked that last week too.


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

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Dollar Signs - Using the GNU Compiler Collection (GCC)

    And
    Code:
    $ cat foo.c
    #include<stdio.h>
    int main()
    {
            int $;
            int j;
            $=10;
            printf ("%d\n",$);
            return 0;
    }
    
    $ gcc foo.c
    
    $ gcc -Wall -ansi -pedantic foo.c
    foo.c:4:13: warning: '$' in identifier or number
    foo.c: In function ‘main’:
    foo.c:5: warning: unused variable ‘j’
    Understand that there are always differences between what the standard says (and what ALL compilers should support), and what your current compiler will let you get away with (and what any other compiler can legitimately reject).

    If you program within the limits of the standard, you maximise your chances of having your code work on other compilers, and (importantly), you maximise your chance of being able to USE other compilers without a lot of tedious re-learning every time the environment changes.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    kotin
    Join Date
    Oct 2009
    Posts
    132
    HI salem,

    is it depend on compiler designs ? am i right?

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    It's actually dependant on the environment. All identifier names that a conforming C compiler must support have an initial nondigit character, followed by any of all the lower case letters or uppercase letters, digits, or also including the underscore. I dug up the information for someone else once in the C standard, I'm sure you can do that if you try.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    The variable naming rules are specified by the C standards.
    C Draft Standards

    Anything else your compiler seems to give you is an implementation detail that is NOT portable.
    Use it if you want, but don't come crying when you stop using foo compiler, and start using bar compiler.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by quzah View Post
    Is this going to be the new 'trick question' of the week? Tired of trying to swap variables without a temp, now they've got everyone asking if '$' is a valid variable name? Because I'm pretty sure someone asked that last week too.
    Quzah.
    What I'm wondering is why new programmers seem to automatically leap to cheats and tricks... there's a guy in the Windows forum saying he's a new programmer and asking about alternate NTFS streams, like that's something we do on an ordinary day... then there's that bunch who want to write self-booting code or access the hardware directly...

    If I were asked, I'd advise them to learn programming *at all* before they even think about messing with hacks, cracks and flacks...

    Yes you can have a variable named $ in C ... but how does that make you a better programmer?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 02-14-2010, 04:14 PM
  2. How to initialise char array?
    By te5la in forum C++ Programming
    Replies: 3
    Last Post: 09-18-2008, 06:32 AM
  3. Is there a quicker way to initialise an array?
    By webznz in forum C Programming
    Replies: 5
    Last Post: 10-25-2007, 03:45 AM
  4. Does C++ new initialise it's value to zero?
    By vaibhav in forum C++ Programming
    Replies: 12
    Last Post: 06-08-2006, 04:10 AM
  5. Replies: 2
    Last Post: 11-16-2003, 10:16 PM