【问题标题】:Reading character and integer separated by spacer in C在C中读取由空格分隔的字符和整数
【发布时间】:2021-02-17 15:04:54
【问题描述】:

我想问一下是否有一种方法可以读取(stdin)输入的字母(空格)数字在哪里,并将字母保存为 char 并将数字保存为 int。所以,基本上,我需要对“get”的“scanf”说他们应该在输入第一个间隔后停止阅读。我正在寻找比获取字符串和逐个字符读取更简单的方法。

非常感谢

参考。输入:

H 1234

参考。工作:

char a='H' int b=1234

【问题讨论】:

  • char a; int b; scanf("%c %d", &a, &b); 怎么样?

标签: c input scanf gets


【解决方案1】:

你可以使用scanf 喜欢:

char input[] = "H 1234";
char c;
int i;

sscanf(input, "%c %d", &c, &i);

// Prints out the character and the number
printf("Charcter: %c, integer: %d", c, i);

return 0;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-28
    相关资源
    最近更新 更多