【问题标题】:c structure: Input requires number line by line by hitting "enter" after each numberc结构:输入需要在每个数字后按“enter”逐行输入
【发布时间】:2016-12-22 21:52:29
【问题描述】:

如何在每行不写一个数字的情况下添加 10 位数字。

struct test {
  unsigned short telephone[10];
  //Some Code Here ... 
}

for (j = 0; j < 10; j++) {
  scanf("%d", & seats[position - 1].telephone[j]);
}

【问题讨论】:

标签: c scanf conversion-specifier


【解决方案1】:

有两件事要提,

  • 尊重数据类型
  • 利用 scanf 的最大字段宽度

 scanf("%1hu", &seats[position-1].telephone[j]);
     //  ^

仅从输入中读取 1 个元素。

注意:h 是一个 长度修饰符,描述为

h

指定以下 diouxXn 转换说明符适用 到 type pointershort intunsigned short int 的参数。

也相关,来自C11,第 §7.21.6.2/p9 章,(强调我的

一个 输入项定义为输入字符的最长序列,不超过 任何指定的字段宽度,并且是匹配输入序列或匹配输入序列的前缀。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-26
    • 1970-01-01
    • 2012-07-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多