Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do i convert seconds into days in C?

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

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading