c - 当我使用scanf()时,我想知道这两者有什么不同
问题描述
我不知道如何用语言来解释,所以我先给你一个问题。所以如果你之前有同样的问题,请原谅我。
#include <stdio.h>
int main()
{
int a, b;
scanf("%d %d", &a, &b);
printf("%d %d", a, b);
return 0;
}
#include <stdio.h>
int main()
{
int a, b;
scanf("%d%d", &a, &b);
printf("%d %d", a, b);
return 0;
}
我一直想知道我在编码时scanf("%d %d", &a, &b);
和scanf("%d%d", &a, &b);
编码时的区别。所以我的问题是,这两个代码在功能上是否相同?
解决方案
两段代码没有区别。无论哪种方式都有效,您可以使用您喜欢的任何一种。
但是,如果考虑 %c 即 char 数据类型说明符,那么事情就会变得有趣。为了理解差异,请考虑以下程序:
int main()
{
char x,y; //declaring two variable of char data type
printf("Part 1");
scanf("%c%c",&x,&y); //no space between the specifiers
printf("%c %c",x,y);
printf("Part 2");
scanf("%c %c",&x,&y); //single white space between the specifiers.
printf("%c %c",x,y);
return 0;
}
在第 1 部分中,变量 x 存储字符“A”,变量 y 存储“”(空白)。在这种情况下,空间被视为输入,因此忽略了实际输入。在第 2 部分中,变量 x 存储“A”而 y 存储“B”,因为明确提到输入中需要空格。
希望这可以帮助 :)
推荐阅读
- amazon-web-services - 如何使用多个 lambda 函数的代码创建单个存储库?
- android - 如何在 Android 中插入两张图像并创建一张图像?
- javascript - 故事书不渲染组件
- php - php 会话 cookie 不起作用 - ngnix.conf - 位置块 - 安全标头 nginx
- mongodb - 在Mongo中查找不在两个对象之间的对象
- javascript - 不显示特定通知 React Native
- python - webscraping python - 发生异常:InvalidURL 无法解析:
- c# - MemoryCache 过期时间比设置时间长
- opencv - 改进与一个图像的多个模板的模板匹配/在图像上查找字符
- ruby-on-rails - 将 json 数据和文件发布到 Ruby on Rails API