首页 > 解决方案 > 无法使用 scanf 打印商

问题描述

我在本练习中的目标是创建一个函数名称MultiTwo(),它将用户插入的两个整数相乘,然后打印商。MultiTwo必须在里面调用main()

这是我的尝试:

#include <stdio.h>

int MultiTwo(int x, int y, int result);

int main() {
    int x, y, result;

    printf("Insert an integer: \n");
    x = scanf("%d", &x);
    printf("Insert a second integer: \n");
    y = scanf("%d", &y);
    result = x * y;

    printf("The quotient of the two inserted integers is: %d", result);

    return 0;   
}

我插入两个整数,尽管插入了整数,但我总是得到的结果是 1。

标签: c

解决方案


x = scanf("%d", &x);

这首先从输入中读取值并将其分配给变量 x。然后通过将 x 设置为 scanf() 的返回值来破坏该值,这是成功读取的变量数,在本例中为 1。


推荐阅读