// A simple program that computes the square root of a number
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main (int argc, char *argv[])
{
if (argc < 2)
{
fprintf(stdout,"Usage: %s number\n",argv[0]);
return 1;
}
double inputValue = atof(argv[1]);
double outputValue = sqrt(inputValue);
fprintf(stdout,"The square root of %g is %g\n",
inputValue, outputValue);
return 0;
}
I received the following errors
Error 1 error C2143: syntax error : missing ‘;’ before ‘type’
Error 2 error C2143: syntax error : missing ‘;’ before ‘type’ Error 3 error C2065: ‘inputValue’ : undeclared identifier
Error 4 error C2065: ‘outputValue’ : undeclared identifier
If you name the file .cpp, it should compile and run fine.
If you name the file .c, however, it will fail.
The reason is that you need to declare all variables at the top of a C function; you cannot declare them at point of use.