I’ve got code that uses the function ‘time’ and other functions from ‘time.h’, and ‘time’ returns NULL every ‘time’ (haha although not funny, this is expensive on ‘time’ available for me to focus attention towards such) such is re-executed. It is strange in the fact that this only began yesterday. Previous usage of the same function in similar but lacking (I’ve been adding to it) code proved okay. The following is the C89 code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define EXIT_SUCCESS 0
#define EXIT_FAILURE 1
typedef struct tm tm;
int logf(char input_string[])
{
time_t* current_time_since_epoch;
time(current_time_since_epoch);
if (current_time_since_epoch == NULL)
{
printf("'time' returned NULL.\n");
return EXIT_FAILURE;
}
tm* current_time_calandar_time = localtime(current_time_since_epoch);
if (current_time_calandar_time == NULL)
{
printf("'localtime' returned NULL.\n");
return EXIT_FAILURE;
}
char current_time_textual_representation[20];
strftime(current_time_textual_representation, 20, "%d-%m-%Y %H:%M:%S", current_time_calandar_time);
printf("%s\n", current_time_textual_representation);
printf("%s\n", input_string);
return EXIT_SUCCESS;
}
int main(void)
{
int check_logf = logf("Hello.");
if (check_logf == 0) exit(EXIT_SUCCESS);
else
{
printf("'logf' returned EXIT_FAILURE.\n");
exit(EXIT_FAILURE);
}
}
You need to allocate memory for the
time()function to store the result if you want to provide it as a parameter. Either declare the variable on the stack or callmalloc(). You can also retrieve the returned value if you giveNULLas a parameter.More info on the function
time()prototype here