Home > Back-end >  How do i convert seconds into days in C?
How do i convert seconds into days in C?

Time:01-29

I'm a bit lost, how can I convert the output of sys_inf.uptime (seconds) into days?

#include <stdio.h>
#include <sys/sysinfo.h>

int main(void) {
  
  struct sysinfo sys_inf; // for system information
  sysinfo(&sys_inf); // to get system information

  int days = sys_inf.uptime;
  float totalDays = days / 86400;

  printf("Latest reboot was : %.2ld seconds ago (%.2f)", sys_inf.uptime, totalDays);

  return 0;
}

CodePudding user response:

You're doing integer division here:

int days = sys_inf.uptime;
float totalDays = days / 86400;

Add .0 to make it a floating point division:

int days = sys_inf.uptime;
float totalDays = days / 86400.0;
  •  Tags:  
  • Related