JK_PROGRAMMER
JK_PROGRAMMER

Reputation: 21

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

#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.

Upvotes: 1

Views: 883

Answers (1)

Jabberwocky
Jabberwocky

Reputation: 50776

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));

Upvotes: 2

Related Questions