Run Code  | API  | Code Wall  | Misc  | Feedback  | Login  | Theme  | Privacy  | Patreon 

Variable declarations in while loop conditions are also C++-only.

// Due to the declaration in the while-condition, this must be compiled as C++.
// C99 supports declarations in the initializer of a for loop, but not in if
// and while. See ISO/IEC 9899:1999 6.8.5 Iteration statements. (A freely 
// available DRAFT: http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf.)

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main(void)
{
    enum { maxval = 9 };
    
    printf("Here's a handful of \"random\" numbers from 1 to %d:\n", maxval);
    
    srand((unsigned)time(NULL));
    while (const int n = rand() % (maxval + 1)) printf("%d\n", n);
    puts("Done.");
}
 run  | edit  | history  | help 0