user1263375
user1263375

Reputation: 663

Printing uptime and idealtime in linux using /proc

I am new to Linux. I made this program for printing uptime and ideal time of my Linux machine. But everytime i run this, it shows idealtime = 0. Can i ever get idealtime=0 ?

#include<stdio.h>

int main() 


{

int a,b;
FILE *fp; 
fp=fopen("/proc/uptime","r");
fscanf(fp,"%d%d",&a,&b);
printf("\n\nUptime =%d \nIdealtime =%d",a,b);
fclose(fp);

return 0;
}

Upvotes: 0

Views: 1217

Answers (1)

Some programmer dude
Some programmer dude

Reputation: 409176

It's because the values are not integers. Try this:

float a, b;
FILE *fp = fopen("/proc/uptime", "r");
fscanf(fp, "%f %f", &a, &b);
printf("Uptime = %d\nIdealtime = %d\n", (int) a, (int) b);

Upvotes: 1

Related Questions