c - How to use rand -- C99 version -
i writing c program , want use for :
for(int i=0 ; < ; i++ ) so need use c99 version in order initialize counter of inside " ( ) " c99 version doesn't work rand() function .
in fact on man page of rand() mentions : " function not part of c99 part of posix "
any ideas in order avoid initializing counter before for ..
//update
the output of compile :
warning: implicit declaration of function ‘rand_r’ [-wimplicit-function-declaration] code :
array[i] = rand_r(&seed) % max + min; - > array[i] type char* (i have dynamically allocated space it)
thanks
rand() much part of c99 standard, have make sure include correct header file.
#include <stdlib.h> int main(void) { return rand() % 10; } the stdlib.h header file covered in 7.20 of c99 standard, , rand() covered in 7.20.2.
if man page stating it's not part of c99, it's incorrect. mine (under debian jessie) states:
the functions rand() , srand() conform svr4, 4.3bsd, c89, c99, posix.1-2001.
but, of course, it's standard controlling document.
you may misreading man page, or whoever wrote man page may confused. random() , srandom() functions not part of c99 (they're posix), rand() , srand() definitely are.
or may using rand_r, re-entrant version. posix not c99.
if want use gcc in c99 mode for (int = 0... functionality, still want able call non-c99 functions, compile -std=gnu99 rather -std=c99.
Comments
Post a Comment