I'm trying to learn C through the book Modern C by Jens Gustedt. In it is the following example
#include <stdlib.h>
#include <stdio.h>
/* lower and upper iteration limits centered around 1.0 */
static double const eps1m01 = 1.0 - 0x1P-01;
static double const eps1p01 = 1.0 0x1P-01;
static double const eps1m24 = 1.0 - 0x1P-24;
static double const eps1p24 = 1.0 0x1P-24;
int main (int argc, char* argv[argc 1]) {
for (int i = 1; i < argc; i) { // process args
double const a = strtod(argv[i], 0); // arg -> double
double x = 1.0;
for (;;) { // by powers of 2
double prod = a * x;
if (prod < eps1m01) {
x *= 2.0;
}
else if (eps1p01 < prod) {
x *= 0.5;
}
else {
break;
}
}
printf("x: %f \n", x);
for (;;) { // Heron approximation
double prod = a * x;
if ((prod < eps1m24) || (eps1p24 < prod)) {
x *= (2.0 - prod);
}
else {
break;
}
}
printf("heron: a = % .5e, \tx = % .5e, \ta * x = % .12f\n",
a, x, a * x);
}
return EXIT_SUCCESS;
}
I'm using visual studio to follow along and this
char* argv[argc 1]
gives me an error at argc 1, I removed it and it seems to work, but I'd like to know if this is in anyway changing what the program does/ if this is just an antiquated way of writing the argument
CodePudding user response:
int main (int argc, char* argv[argc 1])
declares the argv
parameter as a variable length array (VLA).
Variable length arrays are an optional feature of C. Compilers don't have to support it. MSVC does not support it. Other major compilers do.
This declaration of main
does not conform to the standard at all. The standard allows two forms of main
:
int main(int argc, char* argv[]) // note no size in brackets
int main(void)
Other functions can have VLA arguments but main
cannot.
So it is an error in the book. Remove the size between the brackets and all should be well.