I tried to using two different functions from sting.h header file (without including it) strlen() and strtok(). Strlen executed successfully without any error (but some warnings), strtok failed at runtime. Why is it that strlen() function worknig gine but not strtok() if I don't include the header file? I suppose there is something in the linking process that I don't understand. Please clarify for such behavior. However, the program terminates successfully if I print a as '%c' instead of '%s' (strtok returns a garbage value).
CODE:
int main()
{
char string[] = "This is a String";
printf("\nLength of sting is %d\n",strlen(string));
}
WARNINGS: hello.c: In function ‘main’: hello.c:4:2: warning: incompatible implicit declaration of built-in function ‘printf’ [enabled by default] hello.c:4:37: warning: incompatible implicit declaration of built-in function ‘strlen’ [enabled by default] hello.c:4:2: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘long unsigned int’ [-Wformat]
OUTPUT: $ ./a.out
Length of sting is 16
CODE:
int main()
{
char string[] = "This is a String";
printf("\nLength of sting is %d\n",strlen(string));
char *a = strtok(string," ");
printf("%s",a);
}
WARNINGS: $ gcc hello.c hello.c: In function ‘main’: hello.c:4:2: warning: incompatible implicit declaration of built-in function ‘printf’ [enabled by default] hello.c:4:37: warning: incompatible implicit declaration of built-in function ‘strlen’ [enabled by default] hello.c:4:2: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘long unsigned int’ [-Wformat] hello.c:5:12: warning: initialization makes pointer from integer without a cast [enabled by default]
OUTPUT: $ ./a.out
Length of sting is 16
Segmentation fault (core dumped)