Calling strtok(tmp, " ") will lead to undefined behavior because it will try to change the string literal tmp points to, but since the sizeof operand is not evaluated (with one exception, apply here), this is not a problem.
The real problem is that you are trying to print the size_t values ββwith the format "%ld" , which requires an unsigned long argument.
If your implementation supports it, the correct format for the 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 would 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 stand-alone program that should produce the expected results with any implementation of C89 or later:
#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: An OP comment on another answer indicates that the string.h header was a problem; apparently he
#include "string.h"
but not
#include <string.h>
I am going to leave this answer here because it describes another problem that needs to be fixed in the OP code, but not the one that caused the observed symptom. and the compiler took the wrong header file string.h .
Keith thompson
source share