-1

I am trying to use system("date") in printf but I would like it on the same line and I do not know how to format it properly. Any suggestions.

printf("Temperature %.2f date %d \n", val, system("date"));
hellow
  • 12,430
  • 7
  • 56
  • 79

1 Answers1

2

The system function returns the exit status of the given external command, which if successful will typically be 0. The specific command being run is outside of your process so you don't have too much control over what it outputs.

Rather than call an external command, there are functions you can call to get the information you want.

You need to first use the time function, which gives you the time in seconds since the UNIX epoch of 1970-01-01 00:00:00 UTC. Then pass this to the ctime function which gives you a text representation of the time:

time_t t = time(NULL);
printf("date is %s", ctime(&t));
dbush
  • 205,898
  • 23
  • 218
  • 273