1

First thing to say, i'm an absolute noob in programming so it might be a very simple thing and i'm not getting it.

I want to know how much time has passed since the beginning of the day and to do that i used the time() function.

I then want to print it and here is my problem: with the first printf the variable seconds is printed correctly but in the second printf (where I print mills and seconds) it gives me a wrong output.

The code:

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

int main(void) {
  long long int mills, seconds;

  mills = time(NULL);   
  printf("Mills: %i\n", mills );

  seconds = mills / 1000;

  //here the variable is printed correctly
  printf("Seconds: %i\n", seconds );  

  //here mills gets printed correctly but seconds gets printed as 0
  printf("Milliseconds since midnight: %i\nSeconds since midnight: %i\n", mills, seconds);

  return 0;

}

The output:

Mills: 1486143107
Seconds: 1486143
Milliseconds since midnight: 1486143107
Seconds since midnight: 0

Why is the variable printed correctly the first time but not the second time? Shouldn't it be always the same?

Suraj Jain
  • 4,463
  • 28
  • 39

3 Answers3

6

Printing a long long int value through printf requires a %lld (or %lli) format. You are using %i, which is wrong. The behavior is undefined.

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
0

To get the seconds since midnight you need to find the seconds since epoch and mod it by the seconds in a day and then subtract the number of seconds to convert to your local time zone:

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

int main(void)
{
    time_t current_time;
    char* c_time_string;

    /* Obtain current time. */
    current_time = time(NULL);

    if (current_time == ((time_t)-1))
    {
        (void) fprintf(stderr, "Failure to obtain the current time.\n");
        exit(EXIT_FAILURE);
    }

    /* Convert to local time format. */
    c_time_string = ctime(&current_time);

    if (c_time_string == NULL)
    {
        (void) fprintf(stderr, "Failure to convert the current time.\n");
        exit(EXIT_FAILURE);
    }

    /* Print to stdout. ctime() has already added a terminating newline character. */
    (void) printf("Current time is %s", c_time_string);
    (void) printf("Seconds since midnight is %li", current_time%86400 - 18000);
    exit(EXIT_SUCCESS);
}

I subtracted 18000 to make it show seconds since midnight for eastern time zone :)

0

Please Use %lld or %lli for printing long long int , you are using %i which can only be used with int.It causes Undefined Behaviour.

Your Compiler Should Have Warned You

format %i expects argument of type int, but argument 2 has type long long int

If not then please enable [-Wformat=] Warning.

Also Look At This Question cross-platform printing of 64-bit integers with printf

You were unlucky with first printf for that to be working correctly , often things like these get unchecked and cause harm later , it is often much better to enable extra warning and correct your code.

Community
  • 1
  • 1
Suraj Jain
  • 4,463
  • 28
  • 39