Page 96 - C-Language
P. 96
gen_min(ld, long double)
gen_min(ull, unsigned long long)
gen_min(i, int)
int main(void)
{
unsigned long long ull1 = 50ULL;
unsigned long long ull2 = 37ULL;
printf("min(%llu, %llu) = %llu\n", ull1, ull2, min(ull1, ull2));
long double ld1 = 3.141592653L;
long double ld2 = 3.141592652L;
printf("min(%.10Lf, %.10Lf) = %.10Lf\n", ld1, ld2, min(ld1, ld2));
int i1 = 3141653;
int i2 = 3141652;
printf("min(%d, %d) = %d\n", i1, i2, min(i1, i2));
return 0;
}
The generic expression could be extended with more types such as double, float, long long,
unsigned long, long, unsigned — and appropriate gen_min macro invocations written.
Undefined reference errors when linking
One of the most common errors in compilation happens during the linking stage. The error looks
similar to this:
$ gcc undefined_reference.c
/tmp/ccoXhwF0.o: In function `main':
undefined_reference.c:(.text+0x15): undefined reference to `foo'
collect2: error: ld returned 1 exit status
$
So let's look at the code that generated this error:
int foo(void);
int main(int argc, char **argv)
{
int foo_val;
foo_val = foo();
return foo_val;
}
We see here a declaration of foo (int foo();) but no definition of it (actual function). So we
provided the compiler with the function header, but there was no such function defined anywhere,
so the compilation stage passes but the linker exits with an Undefined reference error.
To fix this error in our small program we would only have to add a definition for foo:
/* Declaration of foo */
int foo(void);
/* Definition of foo */
int foo(void)
https://riptutorial.com/ 72

