Creating an atoi function

后端 未结 5 1888
感情败类
感情败类 2021-01-19 04:03

I\'m attempting to create my own atoi function. With the following I\'m getting a return value of 0. Whatever I change the number variable within the function is what I get

5条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-19 04:23

    As I thought, the problem is in your call.

    Change your main to.

    int main()
    {
        char number[MAXSIZE];
        int num;
    
        printf("Please enter a number:\n");
        scanf("%s", number);
        num = atoi_me(number);
        printf("%d", num);
        return 0;
    }
    

    Other than this it's not a good idea to use scanf - http://c-faq.com/stdio/scanfprobs.html . In this case you should use fgets.

提交回复
热议问题