ctype-conversionc-stringsunsigned-integeratoi

Passing char* to atoi


Here's the scenrio -

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

        int main()
        {
            uint8_t backoff;
            char* value = "300000";
            backoff=atoi(value);
            printf("value = %s\n", value);
            printf("backoff value = %d\n", backoff);

            return (0);
        }

Output is as -

        value = 300000
        backoff value = 224

Can someone please help me understand how this conversion happened ?


Solution

  • Pretty simple. Values of uint8_t are 0 to 255. You need at least uint32_t (uint16_t max 65535). 224 is the bits of the real answer that fit in the 8 bit int.