当操作大于10位的整数时,为什么C输出乱码结果?

Ubd*_*mad 0 c long-integer

下面给出的代码可以使用(int数据类型)精确到10位数,但是对于超过10位的数字,它失败了,所以我尝试了unsigned long long int.但现在我的输出固定为15,idk为什么?考虑我对C非常新,但我有平庸的python背景!

我正在使用gcc(Ubuntu 5.4.0-6ubuntu1~16.04.1)5.4.0 20160609

#include <stdio.h> //Get number of digits in a int
unsigned long long int get_len();

void main() {
    unsigned long long int num;
    printf("Enter the number:");
    scanf("%d",&num);
    printf("\nThe length of number is is: %d \n",get_len(num));
}

unsigned long long int get_len(unsigned long long int z) {
    unsigned long long int i = 1;
    while (1) {
        if ((z/10) > 1) {
            //printf("Still greater than 1");
            i++;
                        z = z/10;
            continue;}
        else {
            return(i+1);
                        break;}}}
Run Code Online (Sandbox Code Playgroud)

use*_*738 8

您使用了错误的格式说明符.这将是 scanf("%llu",&num);

使用错误的格式指定符scanf是未定义的行为.

除了提到的内容之外,你的长度查找逻辑是错误的,因为单数字数字和多数字数字都会失败.

因为1它将返回数字位数2,对于其他数字也是如此.(就像12它将返回3).

对于较大的数字,您可以选择选择库(大数字处理)或根据需要编写其中一个.

我会扫描这样的数字 if( scanf("%llu",&num) != 1) { /* error */}.更清楚地检查返回值scanf.