当我使用该time()函数(即,只是随机化种子rand())但不包括头文件时time.h,它适用于C.例如:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i;
srand(time(NULL));
for(i=0;i<10;i++){
printf("\t%d",rand()%10);
}
printf("\n");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当我尝试编译上面的代码时,g++无法编译它,因为time.h不包括在内.但gcc可以.
$gcc ra.c
$./a.out
4 5 2 4 8 7 3 8 9 3
$g++ ra.c
ra.c: In function ‘int main()’:
ra.c:8:20: error: ‘time’ was not declared in this scope
srand(time(NULL));
^
Run Code Online (Sandbox Code Playgroud)
它与gcc的版本有关还是只是C/C++之间的区别?