我的程序不会计算9位数后的数字 [英] My programme doesnot count digits after 9 digits

查看:102
本文介绍了我的程序不会计算9位数后的数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

//使用while计算整数中的位数并执行



//Calculates number of digits in an integer using while and do

#include <stdio.h>

int main(void)

{
    int digits  = 0, num;

    printf("Enter a nonnegative integer: ");
    scanf("%d", &num);

    do{
        num /= 10;
        digits++;
      }
    while (num > 0);

    printf("Number of digits in your integer is %d\n", digits);

    return 0;
}





我的尝试:



当我输入11位数的intiger时,它显示1位数



What I have tried:

When i enter 11 digit intiger it shows 1 digit

推荐答案

我的猜测是因为你要将值转换为int。

My guess is that is because you are converting the value to an int.
An int variable stores the actual binary value of the integer it is assigned. In the declaration of int x = 564, the variable x stores the binary value of the integer number 564. An int variable can store 4 bytes of data, which means the value of an int can hold range between -2,147,483,648 and +2,147,483,647



如果您输入1111111111,您实际上将计为10,而不是9


if you entered "1111111111" you would actually count to 10, not 9


引用:

当我输入11位数时,它显示1位数字

When i enter 11 digit intiger it shows 1 digit



使用调试器,您将看到您的代码正在做什么,注意变量价值。



您的代码行为不符合您的预期,您不明白为什么!



有一个几乎通用的解决方案:逐步在调试器上运行代码,检查变量。

调试器在这里向您展示您的代码正在做什么,您的任务是与它进行比较应该这样做。

调试器中没有魔法,它不知道你应该做什么,它做什么没有找到错误,它只是通过向您展示正在发生的事情来帮助您。当代码没有达到预期的效果时,你就接近了一个错误。

要查看你的代码在做什么:只需设置断点并查看代码是否正常运行,调试器允许你执行第1行第1行,并在执行时检查变量。

调试器 - 维基百科,免费的百科全书 [ ^ ]



掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]

使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]

1.11 - 调试程序(步进和断点)|学习C ++ [ ^ ]

调试器仅显示您的代码正在执行的操作,并且您的任务是与应该执行的操作进行比较。


With the debugger, you will see what your code is doing, pay attention to variables values.

Your code do not behave the way you expect, and you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.
Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]
The debugger is here to only show you what your code is doing and your task is to compare with what it should do.


这篇关于我的程序不会计算9位数后的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆