Calling strtok(tmp, " ")
would cause undefined behavior, since it would attempt to modify the string literal that tmp
points to -- but since the operand of sizeof
is not evaluated (with one exception that doesn't apply here), that's not an issue.
The real problem is that you're trying to print size_t
values with a "%ld"
format, which requires an unsigned long
argument.
If your implementation supports it, the correct format for a size_t
argument is "%zu"
(added in C99):
printf("size of char * = %zu and size of strtok return val = %zu\n",
sizeof(char *), sizeof(strtok(tmp," ")));
Otherwise, explicitly convert the arguments to the appropriate size. I'd use "%lu"
, since size_t
is an unsigned type.
printf("size of char * = %lu and size of strtok return val = %lu\n",
(unsigned long)sizeof(char *), (unsigned long)sizeof(strtok(tmp," ")));
Here's a complete self-contained program that should produce the expected results on any C89 or later implementation:
#include <stdio.h>
#include <string.h>
int main(void) {
char * tmp = "How are you?";
printf("size of char * = %lu and size of strtok return val = %lu\n",
(unsigned long)sizeof(char *),
(unsigned long)sizeof(strtok(tmp," ")));
return 0;
}
EDIT :
The OP's comment on the other answer indicates that the string.h
header was the problem; apparently he had
#include "string.h"
rather than
#include <string.h>
I'm going to leave this answer here because it describes another problem that needs to be fixed in the OP's code, though not the one that caused the observed symptom.
and the compiler picked up the wrong string.h
header file.
<string.h>
? Then the compiler might be in traditional mood and assume return typeint
for functions it doesn't know. – Bootlacegcc-4.5.real (Ubuntu/Linaro 4.5.2-8ubuntu4) 4.5.2
. Crazy. – Vigilante<string.h>
, 8 and 4 without. – Bootlace