不同的印刷输出(C程序)

时间:2017-09-16 02:42:30

标签: c

我创建了这个简单的程序,输入两个数字,并打印最大的数字,如果相等,则打印"这些数字相等",非常简单。但是当我想要打印数字时,它会输出不同的数字" 2293616"。

这是代码:

#include <stdio.h>
#include <conio.h>

int main(void)
{
    int a;
    int b;
    printf("Enter TWO Numbers:\n");
    scanf("%d%d", &a, &b);

    if (a > b)
    {
        printf("%d is larger", &a); 
    }

    if (b > a)
    {
        printf("%d is larger", &b); 
    }

    if (a == b)
    {
        printf("These numbers are equal");
    }
    getch();
}

输出结果为:

Enter TWO Numbers:
5 (Input)
10 (Input)
2293616 is larger (Output)

3 个答案:

答案 0 :(得分:1)

您的打印命令错误。它们应该像printf(“%d更大”,a)等因为打印变量a在内存中

答案 1 :(得分:0)

#include <stdio.h>
#include <conio.h>

int main(void)
{
    int a;
    int b;
    printf("Enter TWO Numbers:\n");
    scanf("%d%d", &a, &b);

    if (a > b)
    {
        printf("%d is larger", a); 
    }

    if (b > a)
    {
        printf("%d is larger", b); 
    }

    if (a == b)
    {
        printf("These numbers are equal %d = %d", a, b);
    }
    getch();
}

答案 2 :(得分:0)

'&amp;' symbol返回存储变量的内存地址,因此是随机数。只需删除&amp;创建“printf(”%d更大“,a)”。

仅考虑&amp;的原因scanf()中的符号,是告诉它存储,在变量的位置扫描的内容。有效地制作变量,无论扫描的是什么。