ctype-conversionc-stringsunsigned-integeratoi

unsigned integer and integer pointer in c


I have a code where I pass 300000(value of 5 min in ms) in function.

            BOOL setsomethingfunc(tpUINT8 value) //tpUINT8 is integer pointer
            {
               bufVal=atoi(value); //bufVal is type UINT8
            }

Now when I print bufVal it comes as 224. I'm not able to understand how the value conversion happened. Can someone please explain ? So, Integer pointer has - 300000 and when converted to int it becomes 224.

I've worked on a minimal reproducible example for 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

    1. BOOL setsomethingfunc(UINT8 value) your function should take pointer not the integer. It has to be BOOL setsomethingfunc(UINT8 *value)

    2. Use standard C types. uint8_t for 8 bits unsigned integer and bool from stdbool.h. Many old code (or the code which development started long time ago like Linix kernel) use those non standard types for historical reasons. You should learn the modern C