c - Parsing a string separated by Commas using MPLAB Compiler? -
i parsing string separated commas , printing values reason code prints first value if run on mplab c compiler. however, if run code on codeblocks, prints out values.
mplab output
2
codeblocks output
2
100
200
100
code:
char somestr[] ="2,0100,0200,0100"; char *pt; int a; pt = strtok (somestr,","); while (pt != null) { = atoi(pt); printf("%d\n", a); pt = strtok (null, ","); }
i output this
2
0100
0200
0100
so going on ? thanks!
i'm wondering how got 0100
, 0200
(using %d
specifier, should 100
, 200
, and do), running code, output should printed. code seems fine. possible reasons why may not case:
- you have more code invokes undefined behavior , interferes piece of code;
- this not actual code you're compiling;
- the terminal or wherever program prints after having been compiled mplab compiler has bug or didn't notice should scrolled;
- a compiler or standard library bug? (very unlikely...)
edit: want output zero-padded. that, use %04d
format specifier when printf()
ing variable.
Comments
Post a Comment