Command line input error

Ok, so I made this program to help me with my homework, and because I wanted to improve my experience in C. Everything compiles fine when I do "gcc file.c -lm", but when I run it with the number on the command line as an argument, my program returns only 70.0000.

#include <stdio.h> #include <math.h> #include <stdlib.h> double temp(double hour){ double t = (3.14/12)*hour; double c = cos((double)t); double temp = 13 * c + 57; return temp; } int main ( int argc, char *argv[]){ double temperature = temp((double)atol(argv[0])); printf("%f\n", temperature); } 
+6
source share
2 answers

argv[0] is probably your program name. I want to expect argv[1] . This tutorial is for quick and easy explanation.

Also, is there a reason you use atol(3) and casting on double instead of just using atof(3) , which returns a double directly?

+7
source

Remember that argv are the arguments used to run a program that looks something like this:

 /path/to/my/exec value 

So, when you access the first element of this array, argv[0] , you get access to the following:

 /path/to/my/exec 

What you really need is the second element of the array, argv[1] , which should contain the following:

 value 
+3
source

Source: https://habr.com/ru/post/907448/


All Articles