代码在C中打印意外输出 [英] Code prints unexpected output in C

查看:71
本文介绍了代码在C中打印意外输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C中编写了一个非常小的代码,它基本上从用户那里获取输入,保存在变量中并将其输出到用户给屏幕输入的相同输入。

但是,当我输入9879041552时,它打印出非常奇怪的数字。虽然,有些数字工作正常,但数字限制为4位数。

造成这种意外行为的原因是什么?



  #include   
#include

int main()
{
int mobileNumber;
printf( 输入您的手机号码:);
scanf( %d,& mobileNumber);
printf( 您的手机号码是:%d,mobileNumber);

return 0 ;
}





我的尝试:



我是编程初学者。我尝试在网上搜索,但它并没有告诉我为什么会发生这种情况。

解决方案

这个数字大于一个整数(有4个字节),它可以容纳2 ^ 31-1和一点符号。



用于电话号码的char数组通常命名为字符串。



谷歌的一些教程。 ; - )

I wrote a very small code in C which basically takes input from user, save in variable and print it out the same input given by user to screen.
But, When I entered "9879041552" it printed out really strange number. Although, some numbers are working fine, the numbers which are limited to 4 digits.
What's causing this unexpected behavior?

#include 
#include 

int main()
{
    int mobileNumber;
    printf("Enter your mobile number: ");
    scanf("%d", &mobileNumber);
    printf("Your mobile number is: %d", mobileNumber);
    
return 0;
}



What I have tried:

I am beginner to programming. I tried searching online but it doesn't give me any reason why this happened.

解决方案

The number is bigger than an integer (with 4 bytes) which holds up to 2^31-1 and a bit for the sign.

Use for phone numbers a char array often named string.

Google for some tutorials. ;-)


这篇关于代码在C中打印意外输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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