This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

atoi doesn't work for all numbers

The function atoi (and sscanf, atof etc), all appear to have the same (unlikely) problem: they don't read the number 5. Yes, I realize that this seems very unlikely.

I am using uVision version 4.21 for the LPC3250

Here are the pertinent lines of code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char * cPtr;
static char buff[512];
int i1 = 0, i2 = 0, i3 = 0;

buff[0] = “6”; buff[1] = “ ”; buff[2] = “5”; buff[3] = “ ”; buff[4] = “7”; buff[5] = “ ”; buff[6] = 0;

cPtr = strtok(buff," ,."); i1 = atoi(cPtr); cPtr = strtok(NULL," ,."); i2 = atoi(cPtr); cPtr = strtok(NULL," ,."); i3 = atoi(cPtr);

The result is:
i1 = 6
i2 = 0
i3 = 7

What I expected was:
i1 = 6
i2 = 5
i3 = 7

I have tried many variants of this program. I also tried the same with sscanf. Always misses the 5, no matter what position the 5 appears in.

Has anyone any ideas why this is happening?

Has anyone seen the same problem?

Parents
  • static char buff[512];
    
    buff[0] = “6”; buff[1] = “ ”; buff[2] = “5”; buff[3] = “ ”; buff[4] = “7”; buff[5] = “ ”; buff[6] = 0;
    

    Care to tell us why you are assigning strings to the individual positions of your character array?

    A character is given with single quotes: '5'.
    A string is given with double quotes: "5".

    By the way - how come your post are using prettified, typographic quotes?

Reply
  • static char buff[512];
    
    buff[0] = “6”; buff[1] = “ ”; buff[2] = “5”; buff[3] = “ ”; buff[4] = “7”; buff[5] = “ ”; buff[6] = 0;
    

    Care to tell us why you are assigning strings to the individual positions of your character array?

    A character is given with single quotes: '5'.
    A string is given with double quotes: "5".

    By the way - how come your post are using prettified, typographic quotes?

Children
No data