Reading system time as integers? [duplicate] - c

This question already has answers here:
Get the current time in C [duplicate]
(11 answers)
Closed 5 years ago.
I'm doing a C programming course at uni and one of the tasks is to create an OpenGL analogue clock on the screen. I want to drive the hour, minute, and second hands from the actual time.
How do I read the hour, minute, and seconds from system time? As integers would be best. I've had a look around and can't find anything quite like what I'm after.

You should use localtime, like this:
#include <time.h>
time_t timestamp = time(NULL);
if (timestamp != (time_t)-1) {
struct tm *t = localtime(&timestamp);
// Use t->tm_hour, t->tm_min and t->tm_sec, which are ints
}
See man 3 localtime for more info.

Related

How to create random number values in c [duplicate]

This question already has answers here:
Recommended way to initialize srand?
(15 answers)
srand() — why call it only once?
(7 answers)
Closed 2 years ago.
i am trying to create random data so i can review an algorithm in cache policy. When i use the first code with rand() i get the same numbers every time i run my code(i knew this was coming) so i tried to use srand. when i inserted it i get different numbers every time i run the code but the same 10 numbers at my loop. What am i doing wrong to create random data??? After that i will make with zip ununiformed data.
sorry my bad the loop is in main function:
for(counter=0; counter<10; counter++){
printf("Number %d is %lf\n", counter, rand_val());
}
And i am using this function to create numbers.
double rand_val(){
double rnum;
srand(time(NULL));
rnum=rand();
return(rnum);
}

Why do we use 'NULL'? [duplicate]

This question already has answers here:
What is time(NULL) in C?
(8 answers)
Closed 3 years ago.
Why do we use 'NULL' in code below.
Why can't we multiply the seed by an integer?
Sorry I am new to C++.
CODE
srand(time(NULL));
The time function can write the time to a location provided by a pointer to the function call. This pointer argument can be a null pointer, and then time only returns the current time.
On most systems the time function returns the number of seconds since an epoch, and as such is a pretty unique integer value to use for seeding the random number generator.
The single statement
srand(time(NULL));
is equivalent to
time_t temp_time = time(NULL);
srand(temp_time);
Or if we want to use a non-null pointer
time_t temp_time;
time(&temp_time);
srand(temp_time);

Creating a random array of Bits [duplicate]

This question already has answers here:
Why do I always get the same sequence of random numbers with rand()?
(12 answers)
Closed 7 years ago.
Using rand(), I'm trying to create an array which generates 32 random numbers each time I run the program. However my program below gives me the same 32 random bits each time. Is there anyway I can get it to generate different 32 bits each time I run the program?
for(a=0;a<32;a++)
{
ran[a]= (rand()%2);
}
You need to set up the random seed each time you run the program to something different to do that. What people do is usually this:
#include <time.h> /* time */
...
srand (time(NULL));
...
//Calls to rand();
Then your random seed is different at every start of the program. Check more details about srand here.

How to get the current time in milliseconds in C Programming [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to measure time in milliseconds using ANSI C?
How can I get the Windows system time with millisecond resolution?
We want to calculate the time which a player have taken to finish the game.
But with time.h we could only calculate in seconds. but that is not exact. Is it possible to get the time in milliseconds?
and what is the %? to printf?
There is no portable way to get resolution of less than a second in standard C So best you can do is, use the POSIX function gettimeofday().
quick answer
#include<stdio.h>
#include<time.h>
int main()
{
clock_t t1, t2;
t1 = clock();
int i;
for(i = 0; i < 1000000; i++)
{
int x = 90;
}
t2 = clock();
float diff = ((float)(t2 - t1) / 1000000.0F ) * 1000;
printf("%f",diff);
return 0;
}
If you're on a Unix-like system, use gettimeofday and convert the result from microseconds to milliseconds.

Correctly recording the UNIX time in C (-Wall nightmare) [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
What primitive data type is time_t?
Basically, all I want to do is produce the current UNIX time (result from time(NULL)) and print it to a file I have open.
I've tried code such as:
fprintf(f, "%i", time(NULL));
But I get these annoying compiler warnings:
src/database-createtbl.c:140: warning: int format, time_t arg (arg 3)
I'm trying to compile using -Wall - this really shouldn't be an issue but it's driving me nuts.
Simply cast to a known type:
printf("%ld", (long)time(NULL));
The type returned by time depends on the OS (it's aliased to time_t, but you don't know if this is long or int or something else), so you need to cast it to a known type before passing it to printf, in order for the format to match the argument type.

Resources