3

Strangely, the following C++ program compiles on Sun Studio 10 without producing a warning for an undefined variable:

int main()
{
  return sun;
}

The value of sun seems to be 1. Where does this variable come from and what is it for?

lytenyn
  • 819
  • 5
  • 21
  • 7
    It's for counting suns, and there is only the one of them, hence it's 1. Have you tried `return stars;`? You'll need something bigger than an int though :) – Binary Worrier Apr 11 '11 at 07:43

3 Answers3

4

It's almost certainly a predefined macro. Formally, the C and C++ standards reserve names starting with an underscore and a capital letter, or containing two underscores, for this, but practically, compilers had such symbols defined before the standard, and continue to support them, at least in their non-compliant modes which is the default mode for all of the compilers I know. I can remember having problems with `linux' at one time, but not when I invoked g++ with -std=c++89.

James Kanze
  • 150,581
  • 18
  • 184
  • 329
2

It must be one of the automatic macros created by the compiler.

Try the same thing, replace sun by gnu and use a gcc compiler on Linux. You'll get a similar result.

With gcc, you can get all the predefined macros with: echo "" | gcc -E - -dM.

Didier Trosset
  • 36,376
  • 13
  • 83
  • 122
  • Using GCC on Solaris here did produce an error as expected. I always thought these macros were of the form `__GNUC__` or `__SUNPRO_CC` to avoid polluting the namespace. – lytenyn Apr 11 '11 at 07:46
  • Thank you, the second hint helped, I can see now that gcc sets the macros `sun`, `unix` and `sparc` to 1. It is a bit annoying, though, I think. Anybody has a reference where these come from? – lytenyn Apr 11 '11 at 07:49
  • Found it myself: http://gcc.gnu.org/onlinedocs/cpp/System_002dspecific-Predefined-Macros.html#System_002dspecific-Predefined-Macros – lytenyn Apr 11 '11 at 07:50
1

sun is defined for historical backwards compatibility from before the convention to start with an underscore was adopted. For Studio, it's documented in the cc(1) and CC(1) man pages under the -D flag:

   -Dname[=def]

       Defines  a  macro  symbol  name  to  the preprocessor.  Doing so is
       equivalent to including a #define directive at the beginning of the
       source.  You can use multiple -D options.

       The following values are predefined.

       SPARC and x86 platforms:


         __ARRAYNEW
         __BUILTIN_VA_ARG_INCR
         __DATE__
         __FILE__
         __LINE__
         __STDC__ = 0
         __SUNPRO_CC = 0x5130
         __SUNPRO_CC_COMPAT = 5 or G
         __TIME__
         __cplusplus
         __has_attribute
         __sun
         __unix
         _BOOL if type bool is enabled (see "-features=[no%]bool")
         _WCHAR_T
         sun
         unix
         __SVR4 (Oracle Solaris)
         __SunOS_5_10  (Oracle Solaris)
         __SunOS_5_11  (Oracle Solaris)
...

Various standards compliance options can disable it, as can the +p flag to CC.

alanc
  • 4,102
  • 21
  • 24