S
Stanley Rice
Dear all
Once again I come across some question about linking. In f1.c, I have
the following function definition.
----------------f1.c------------
#include <stdio.h>
void my_print(float a, int b)
{
printf("%f\t%d\n", a, b);
}
int add(int a, int b)
{
return a + b;
}
In main.c, I call the two function without declaration.
----------------main.c-------------
int main(void)
{
int a = add(1, 2);
printf("%d\n", a);
my_print(2.0, a);
return 0;
}
Notice here, in file main.c, I don't include any headers, say, 'f1.h',
'stdio.h', and don't declare anything deliberately, trying to get what
the result would like to be.
In my machine, with compiler gcc on ubuntu, I got some warnings while
compiling the code, and the binary file runs happily, printing the
following lines:
3
0.000000 1073741824
From the result, I got that
1. the function 'add' and the standard library function 'printf'
performs well, because the first line printed is 3.
2. the function 'my_print' doesn't work, the printed line obviously
shows.
All three function are defined external without declaration in file
main.c. But why the first two works well, but the last one fails? Is
it undefined in ISO C?
Thanks in advance.
Once again I come across some question about linking. In f1.c, I have
the following function definition.
----------------f1.c------------
#include <stdio.h>
void my_print(float a, int b)
{
printf("%f\t%d\n", a, b);
}
int add(int a, int b)
{
return a + b;
}
In main.c, I call the two function without declaration.
----------------main.c-------------
int main(void)
{
int a = add(1, 2);
printf("%d\n", a);
my_print(2.0, a);
return 0;
}
Notice here, in file main.c, I don't include any headers, say, 'f1.h',
'stdio.h', and don't declare anything deliberately, trying to get what
the result would like to be.
In my machine, with compiler gcc on ubuntu, I got some warnings while
compiling the code, and the binary file runs happily, printing the
following lines:
3
0.000000 1073741824
From the result, I got that
1. the function 'add' and the standard library function 'printf'
performs well, because the first line printed is 3.
2. the function 'my_print' doesn't work, the printed line obviously
shows.
All three function are defined external without declaration in file
main.c. But why the first two works well, but the last one fails? Is
it undefined in ISO C?
Thanks in advance.