Note that there are some explanatory texts on larger screens.

plurals
  1. PORAND_MAX macro: signed or unsigned?
    text
    copied!<p>I've looked up the C standard (from 1999) and it only says that <code>RAND_MAX</code> should be at least 32767 but says nothing about whether this macro should expand to a signed or an unsigned int. The Single UNIX Specification (<a href="http://pubs.opengroup.org/onlinepubs/7908799/xsh/stdlib.h.html" rel="noreferrer">link 1</a>, <a href="http://pubs.opengroup.org/onlinepubs/7908799/xsh/rand.html" rel="noreferrer">link 2</a>) and Linux man (<a href="http://www.kernel.org/doc/man-pages/online/pages/man3/rand.3.html" rel="noreferrer">link</a>) don't add any clarity.</p> <p>One would think <code>RAND_MAX</code> should be a <code>signed int</code> since that's what <code>rand()</code> returns.</p> <p>However, I've found that some compilers define it as unsigned:</p> <ul> <li>The ancient Turbo C++ 1.01: #define RAND_MAX <b>0x7FFFU</b></li> <li>The not so ancient C++ Builder 5.5: #define RAND_MAX <b>0x7FFFU</b></li> <li>The still alive Open Watcom C/C++ 1.9: #define RAND_MAX <b>32767U</b></li> <li>DJGPP (gcc 3.3.4 for DOS): #define RAND_MAX 2147483647</li> <li>MinGW (gcc 4.6.2 for Windows): #define RAND_MAX 0x7FFF</li> <li>MS Visual Studio 2010 (<a href="http://msdn.microsoft.com/en-us/library/2dfe3bzd%28v=vs.100%29.aspx" rel="noreferrer">link</a>): <i>RAND_MAX is defined as the value 0x7fff</i></li> <li>Tiny C Compiler 0.9.25: #define RAND_MAX 0x7FFF</li> <li>lcc-win32 3.8: #define RAND_MAX 0x7fff</li> <li>Pelles C 6.50: #define RAND_MAX 0x3fffffff <i>OR</i> #define RAND_MAX 0x7fff</li> <li>Digital Mars C/C++ 8.52: #define RAND_MAX 32767</li> </ul> <p>This makes seemingly innocuous code like the below become non-portable and blow up due to signed to unsigned promotion:</p> <pre><code>cos(w * t) + (rand() - RAND_MAX / 2) * 0.1 / (RAND_MAX / 2); </code></pre> <p><code>rand()</code> returns a <code>signed int</code> in the range [0,<code>RAND_MAX</code>].</p> <p>If <code>RAND_MAX</code> is defined as an <code>unsigned int</code>, the value from <code>rand()</code> gets promoted to <code>unsigned int</code> too.</p> <p>And if that's the case, the difference <code>(rand() - RAND_MAX / 2)</code> becomes an unsigned difference of unsigned integers with the value in the ranges [0,<code>RAND_MAX</code>-<code>RAND_MAX</code>/2] &amp; [<code>UINT_MAX</code>+1-<code>RAND_MAX</code>/2,<code>UINT_MAX</code>-1] instead of being a signed difference of signed integers with the value in the range [-<code>RAND_MAX</code>/2,<code>RAND_MAX</code>-<code>RAND_MAX</code>/2].</p> <p>Anyhow, it seems like <code>RAND_MAX</code> should be signed and most(?) compilers define it as such, but is there any authoritative source that says it should be signed? Older standard? K&amp;R? Another UNIX spec?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload