In the posted code is missing:
- The inclusion of the
stdio.h
header. - The declaration of the
is_palindrome
function. - The definition of the
is_palindrome
function.
Therefore, if you try to compile it, as it stands, the compiler will output something like
test.c: In function ‘main’:
test.c:6:6: warning: implicit declaration of function ‘is_palindrome’ [-Wimplicit-function-declaration]
6 | r = is_palindrome("level");
| ^~~~~~~~~~~~~
test.c:7:2: warning: implicit declaration of function ‘printf’ [-Wimplicit-function-declaration]
7 | printf("%d\n", r);
| ^~~~~~
test.c:7:2: warning: incompatible implicit declaration of built-in function ‘printf’
test.c:1:1: note: include ‘<stdio.h>’ or provide a declaration of ‘printf’
+++ |+#include <stdio.h>
1 |
/usr/bin/ld: /tmp/ccqqE0qs.o: in function `main':
test.c:(.text+0x19): undefined reference to `is_palindrome'
/usr/bin/ld: test.c:(.text+0x43): undefined reference to `is_palindrome'
/usr/bin/ld: test.c:(.text+0x6d): undefined reference to `is_palindrome'
/usr/bin/ld: test.c:(.text+0x97): undefined reference to `is_palindrome'
collect2: error: ld returned 1 exit status
If you provide (together with
stdio.h
header inclusion) the
is_palindrome
function
declaration
#include <stdio.h>
int is_palindrome(const char * );
int main(void)
{
int r;
r = is_palindrome("level");
printf("%d\n", r);
r = is_palindrome("redder");
printf("%d\n", r);
r = is_palindrome("test");
printf("%d\n", r);
r = is_palindrome("step on no pets");
printf("%d\n", r);
return (0);
}
Then the compiler will output:
/usr/bin/ld: /tmp/ccelevUP.o: in function `main':
test.c:(.text+0x14): undefined reference to `is_palindrome'
/usr/bin/ld: test.c:(.text+0x39): undefined reference to `is_palindrome'
/usr/bin/ld: test.c:(.text+0x5e): undefined reference to `is_palindrome'
/usr/bin/ld: test.c:(.text+0x83): undefined reference to `is_palindrome'
collect2: error: ld returned 1 exit status
These
linker errors are telling you the
defintion of the
is_palindrome
is missing.
Eventually the following piece of code, providing both the
is_palindrome
declaration and
definition will happily compile:
#include <stdio.h>
int is_palindrome(const char * );
int main(void)
{
int r;
r = is_palindrome("level");
printf("%d\n", r);
r = is_palindrome("redder");
printf("%d\n", r);
r = is_palindrome("test");
printf("%d\n", r);
r = is_palindrome("step on no pets");
printf("%d\n", r);
return (0);
}
int is_palindrome(const char * s ) {
return 0; }