On 2007-09-11, Wolfgang Granzer <[email protected]> wrote:
> "strtol" always return 0 instead of the converted integer. The output
> of "printf" of the following simple example is "12 0"
>
> long temp;
> char test[3];
>
> test[0] = '1';
> test[1] = '2';
> test[2] = 0;
> temp = strtol(test, &endptr, 10);
>
> printf("%s %ld ",test,temp);
>
> If I use "atoi" instead of "strtol", everything works fine and the
> output of "printf" is "12 12 ".
>
> Any ideas?
You forgot to declare strtol() so the compiler thinks it's
returning an int instead of a long. Add the following line
to the top of your program:
#include <stdlib.h>
More importantly, always compile with -Wall, and fix all of the
warnings. Personally, I use
-Werror -Wall -W -Wpointer-arith -Wcast-align -Wshadow -Wundef -Wwrite-strings
--
Grant Edwards grante Yow! I'm changing the
at CHANNEL ... But all I get
visi.com is commercials for "RONCO
MIRACLE BAMBOO STEAMERS"!