Here's a function called get_temp that returns the temperature in Farenheit or Celcius.
Make a file called temp.c and compile it with gcc temp.c -o temp
Here is temp.c:
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
double get_temp(bool use_farenheit);
int main(int argc,char *argv[])
{
// Display the temperature to the user.
printf("Temperature = %3.3f'C or %3.3f'F\n",get_temp(false),get_temp(true));
return 0;
}
// Returns the temp in Farenheit or Celcius. Returns -1000 if something went wrong.
double get_temp(bool use_farenheit)
{
const int BUFFER_SIZE = 100;
char data[BUFFER_SIZE];
FILE *finput;
size_t bytes_read;
double temp = -1000;
finput = fopen("/sys/class/thermal/thermal_zone0/temp","r");
if (finput != NULL) {
memset(data,0,BUFFER_SIZE);
fread(data,BUFFER_SIZE,1,finput);
temp = atoi(data);
temp /= 1000;
if (use_farenheit) {
temp = temp * 9 / 5 + 32;
}
fclose(finput);
}
return temp;
}