warning C4244: 'function': conversion from 'time_t' to 'unsigned int', possible loss of data'

时间:2018-12-03 12:53:24

标签: c

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

// Program to generate random numbers, using an array

int main() {
int arr[50];
int i;

srand((unsigned int)time(NULL));
for (i = 0; i < 50; i++)
{
    arr[i] = rand() % 50;
    printf("arr[%d] = %d\t", i, arr[i]);
}
_getch();
return 0;

}

I'm getting this warning:

(12): warning C4244: 'function': conversion from 'time_t' to 'unsigned int', possible loss of data.

1 个答案:

答案 0 :(得分:2)

srand wants an unsigned int as argument, but time returns a time_t which is a larger type than unsigned int on your platform, hence the warning possible loss of data.

In this case the warning can be ignored, because you actually just want to give a seemingly random value to srand.

To get rid of the warning you can cast the value returned by time to unsigned int:

srand((unsigned int)time(NULL));