我对 C 编程相当陌生,所以我想我应该尝试编写一个简单的程序来
打印两个 int 数字。我提示用户输入这两个数字,然后使用 printf
但是,运行该程序后,我得到的结果非常奇怪。
例如...
Enter first int: 5
Enter second int: 3
First int: 2130567168
Second int: 2686756
我的代码如下...
#include <stdio.h>
int main()
{
int x, y;
printf("Enter first number: ");
scanf("%i", x);
printf("Enter second number: ");
scanf("%i", y);
printf("%i\n%i%\n",x,y);
return 0;
}
最佳答案
这是因为您忘记了 scanf 中的 &
陈述。所以改成这样:
scanf("%i", &y);
scanf("%i", &x);
//^ See here
此外,您的 printf 语句中的 %
太多了。因为 %
代表 format specifiers ,所以如果你想打印符号 %
你必须写两次:
printf("%i\n%i%\n",x,y);
//^ Is a format specifier so if you want to print the symbol, write it 2 times
旁注:
如果您在 scanf 语句中使用说明符 %i
并输入如下内容:035
输出将变为 29
,因为它将被解释为八进制数。因此,如果您不希望这样,可以将说明符更改为 %d
并将 035
变为 35
关于c - 不需要的输出 - 打印出两个数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28622646/