Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dollar sign in variable name?

I stumbled on some C++ code like this:

int $T$S; 

First I thought that it was some sort of PHP code or something wrongly pasted in there but it compiles and runs nicely (on MSVC 2008).

What kind of characters are valid for variables in C++ and are there any other weird characters you can use?

like image 736
Valmond Avatar asked Oct 28 '11 07:10

Valmond


People also ask

What is a variable with a dollar sign?

In computer programming, a sigil (/ˈsɪdʒəl/) is a symbol affixed to a variable name, showing the variable's datatype or scope, usually a prefix, as in $foo , where $ is the sigil. Sigil, from the Latin sigillum, meaning a "little sign", means a sign or image supposedly having magical power.

Can you use a dollar sign in a variable name Java?

"Java does allow the dollar sign symbol $ to appear in an identifier, but these identifiers have a special meaning, so you should not use the $ symbol in your identifiers."

Can we use dollar sign in variable in C?

Unlike in other languages (bash, perl), C does not use $ to denote the usage of a variable. As such, it is technically valid. In C it most likely falls under C11, 6.4. 2.

What does dollar sign mean in coding?

When used in a regular expression, the dollar sign is used to represent the end of the line or string. For example, in the following Perl code, if the user's input stored in the $input variable ends with the "example," it would print "I see example." to the screen.


2 Answers

The only legal characters according to the standard are alphanumerics and the underscore. The standard does require that just about anything Unicode considers alphabetic is acceptable (but only as single code-point characters). In practice, implementations offer extensions (i.e. some do accept a $) and restrictions (most don't accept all of the required Unicode characters). If you want your code to be portable, restrict symbols to the 26 unaccented letters, upper or lower case, the ten digits, and the '_'.

like image 181
James Kanze Avatar answered Oct 03 '22 10:10

James Kanze


It's an extension of some compilers and not in the C standard

MSVC:

Microsoft Specific

Only the first 2048 characters of Microsoft C++ identifiers are significant. Names for user-defined types are "decorated" by the compiler to preserve type information. The resultant name, including the type information, cannot be longer than 2048 characters. (See Decorated Names for more information.) Factors that can influence the length of a decorated identifier are:

  • Whether the identifier denotes an object of user-defined type or a type derived from a user-defined type.
  • Whether the identifier denotes a function or a type derived from a function.
  • The number of arguments to a function.

The dollar sign is also a valid identifier in Visual C++.

// dollar_sign_identifier.cpp struct $Y1$ {     void $Test$() {} };  int main() {     $Y1$ $x$;     $x$.$Test$(); } 

https://web.archive.org/web/20100216114436/http://msdn.microsoft.com/en-us/library/565w213d.aspx

Newest version: https://docs.microsoft.com/en-us/cpp/cpp/identifiers-cpp?redirectedfrom=MSDN&view=vs-2019

GCC:

6.42 Dollar Signs in Identifier Names

In GNU C, you may normally use dollar signs in identifier names. This is because many traditional C implementations allow such identifiers. However, dollar signs in identifiers are not supported on a few target machines, typically because the target assembler does not allow them.

http://gcc.gnu.org/onlinedocs/gcc/Dollar-Signs.html#Dollar-Signs

like image 28
phuclv Avatar answered Oct 03 '22 10:10

phuclv