1

I need a special symbol for certain things in my code (variables, functions ,etc). I noticed that '$' is a legal symbol, but was just wondering if it is recommended to use it (it won't conflict with something else later?).

I'm using VStudio, and I know that for certain things VS uses $ (like for snippets etc. although I'm not planning on using them).

Tez
  • 517
  • 1
  • 7
  • 16
  • `variables, functions ,etc` - it's not allowed to have `$` in function names, variable names, etc. – Kiril Kirov Apr 17 '13 at 06:42
  • Oh so it must be a Visual Studio thing because my code works/compiles with $ symbols. – Tez Apr 17 '13 at 06:44
  • 3
    @KirilKirov: That's compiler dependent. They are allowed to support more than the basic character set, and `$` is accepted by at least GCC and MSVC. Of course it's not portable, but the question is specifically about MSVC, where it's usually impossible to write portable code anyway. – Mike Seymour Apr 17 '13 at 06:44
  • @MikeSeymour - didn't know that, thanks. Point taken. – Kiril Kirov Apr 17 '13 at 06:46
  • 3
    Why would you want to do this? – Alex Chamberlain Apr 17 '13 at 06:49
  • Regarding the possible duplicate: I know the other question is not specific to Visual Studio. But I am not sure if you question is either (you seem to give Visual Studio as an example only). Also, the answers in the other question cover the Visual Studio exception. – jogojapan Apr 17 '13 at 07:17
  • For the closers: the duplicate question is old and has C++03-answers only. Things changed in C++11! – Arne Mertz Apr 17 '13 at 08:27
  • 1
    For C++11: Maybe and no. **Maybe:** According to §2.11, identifiers may consist of *digits* and *identifier-nondigits*, starting with one of the latter. *identifier-nondigits* are the usual `a-z`, `A-Z` and underscore, *universal-character-names* (e.g. \uBEAF, \UC0FFEE32), and *other implementation-defined characters*. So it is implementation defined if using `$` in an identifier is allowed. VC10 and up supports that, maybe earlier versions, too. It even supports identifiers like `こんばんわ`. **No**: don't do that. Make identifiers as readable and portable as possible. `$` is not portable. – Arne Mertz Apr 17 '13 at 08:33
  • I can't believe that I didn't know that GCC and MSVC allowed dollar signs in identifiers. – Michael Burr Apr 17 '13 at 08:36

1 Answers1

2

Microsoft VC++ compiler allow to use $ sign as well. read this: http://msdn.microsoft.com/en-us/library/565w213d.aspx. So if you works in MS environment, No issues, You can use it as other supported such as A-Z, a-z, 0-9, _

Edit: But one thing, If some other programmer may see your code in future and may confuse (because some programming languages such as PHP uses $ as a key symbol). And also it is recommended to use meaning full name for identifiers (Write programs for peoples first, computer second - Steve McConnell). So Batter if you reconsider to use $ sign if it makes your names ugly. And it also may cause to portability issue (only matters,if you target multiple compilers)

Nayana Adassuriya
  • 23,596
  • 30
  • 104
  • 147