c - 无法使用 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。
解决方案
x = scanf("%d", &x);
这首先从输入中读取值并将其分配给变量 x。然后通过将 x 设置为 scanf() 的返回值来破坏该值,这是成功读取的变量数,在本例中为 1。
推荐阅读
- javascript - 如何使用 JQuery 对表格列进行升序和降序排序?
- vue.js - VueJs - 在 v-select 之后包含一个按钮
- rally - Rally API - 仅获取用户故事的更新
- docker - 使用 docker 安装 swipl
- typescript - 在 Typescript 中使用 allowJs 选项使用 JS 时,如何将对象属性用作可选?
- laravel - Laravel找到具有差异条件的同一列之和的差异
- python - 如何在保持比例的同时使我的玩家图像在碰撞时更大?
- javascript - 我可以将国际象棋游戏/移动导入 React Chessboard 以仅以视觉格式显示移动吗?(实际上没玩)
- laravel - 如何在 Laravel 中创建自定义排除验证规则
- .net-core - .Net Core 5 控制台应用程序,在 Program.Main 中完成时不会为环境加载应用程序设置