使用 scanf() 具有不同数据类型的多个输入
Multiple inputs with different data types using scanf()
我正在做一些项目,但我被困在一个问题上。 我无法正常使用scanf()
功能。我想扫描具有不同数据类型的多个输入。 但我不知道问题是什么。没有编译错误。
#include "stdio.h"
int main()
{
unsigned char minx[1024], x, y, z;
printf("Enter four ints: ");
scanf( "%s %x %c %i", &minx, &x, &y, &z);
printf("You wrote: %s %x %c %i", minx, x, y, z);
}
提示显示"输入四个整数",但尚未声明任何类型int
的变量。 你有
unsigned char minx[1024], x, y, z;
它为您提供一个包含 1024 个无符号字符的数组和三个单独的无符号字符。
然后你写了
scanf( "%s %x %c %i", &minx, &x, &y, &z);
你说你没有收到任何编译器错误。 如果可能的话,我必须鼓励你得到一个更好的编译器! 我的警告我关于这条线上的各种事情:
format specifies type 'char *' but the argument has type 'unsigned char (*)[1024]'
format specifies type 'unsigned int *' but the argument has type 'unsigned char *'
format specifies type 'int *' but the argument has type 'unsigned char *'
如果要输入字符串、十六进制整数、字符和另一个整数,请使变量类型匹配:
char str[100];
int hex;
char c;
int anotherint;
scanf("%99s %x %c %i", str, &hex, &c, &anotherint);
printf("You wrote: %s %x %c %in", str, hex, c, anotherint);
我用%99s
来确保我不会溢出char str[100]
。
另请注意,在str
scanf
调用之前不需要&
。
相关文章:
- 如何在 CPP 中检查给定输入的数据类型?
- 将空格作为单字符数据类型的输入?
- 函数重载并根据相同的输入返回不同的数据类型
- 有没有办法在函数 c++ 中输入任何数据类型?
- 是否可以用类似C/C++(或任何语言)的语言,从作为用户输入的字符串或文件中创建用户定义的数据类型
- 使用 scanf() 具有不同数据类型的多个输入
- 输入数据类型检查循环未按预期工作 (C++)
- 如果输入变量的数据类型与以前不同,如何使我的循环仍然正常运行?
- C++ 从用户那里获取未知数据类型输入
- C++:获取用户通过cin输入的数据类型
- 如何在无效的数据类型输入后继续循环
- 正确数据类型的输入验证
- C++,如何验证输入的数据是否为正确的数据类型
- C++具有混合分隔符和数据类型的文件输入
- 我们应该使用哪种数据类型来输入一个介于 0<= 数字 <= 10^18 之间的数字
- C++ 使用函数模板查找任何输入数据类型的中间值
- 将未知数据类型的元素输入到向量中并传递给函数模板
- 无限循环,同时在 STACK 中输入不同的数据类型值
- 如何验证数据类型输入
- c++中的混合数据类型输入