c++ - 使用 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 %i\n", str, hex, c, anotherint);
我曾经%99s
确保我没有溢出char str[100]
。
另外,请注意您在通话中不需要&
before 。str
scanf
推荐阅读
- performance - 矛盾的 VTune Amplifier 微架构探索结果
- scala - 如何在火花中将 RDD[Result] 转换为 RDD[Row]?
- javascript - 在 JavaScript 中对多个对象进行排序
- python - 将 keras ImageDataGenerator.flow_from_directory() 与 Talos Scan() 一起使用
- r - 从帮助中获取 R 函数的文档作为字符串
- java - 将任务 ID 从 ActiveMQ 代理记录到客户端应用程序
- javascript - 用 `this` 玩得开心并声明一个全局变量
- android - 调用 notifyDataSetChanged 时 FragmentStatePagerAdapter 不刷新片段
- java - 如何访问来自 ClientBuilder 的 get 请求的响应
- python - 从 json 文件中的字符串中删除标签(\r、\n、<、>)