Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C中的输入与C中具有Struct的下一个输入合并 #包括 #包括 #包括 #包括 类型定义结构学生 { 字符名[256]; 字符id[5]; 字符数[10]; }学生; 学生添加() { 学生; printf(“输入名称:”); scanf(“%s”,学生姓名); printf(“输入id:”); scanf(“%s”,学生id); printf(“输入编号:”); scanf(“%s”,学生编号); 留学生; } int main() { 学生s=添加(); printf(“%-20s%-20s%-20s\n”,s.name,s.id,s.number); }_C - Fatal编程技术网

C中的输入与C中具有Struct的下一个输入合并 #包括 #包括 #包括 #包括 类型定义结构学生 { 字符名[256]; 字符id[5]; 字符数[10]; }学生; 学生添加() { 学生; printf(“输入名称:”); scanf(“%s”,学生姓名); printf(“输入id:”); scanf(“%s”,学生id); printf(“输入编号:”); scanf(“%s”,学生编号); 留学生; } int main() { 学生s=添加(); printf(“%-20s%-20s%-20s\n”,s.name,s.id,s.number); }

C中的输入与C中具有Struct的下一个输入合并 #包括 #包括 #包括 #包括 类型定义结构学生 { 字符名[256]; 字符id[5]; 字符数[10]; }学生; 学生添加() { 学生; printf(“输入名称:”); scanf(“%s”,学生姓名); printf(“输入id:”); scanf(“%s”,学生id); printf(“输入编号:”); scanf(“%s”,学生编号); 留学生; } int main() { 学生s=添加(); printf(“%-20s%-20s%-20s\n”,s.name,s.id,s.number); },c,C,我得到了第二个输入合并和第三个输入合并。有人知道怎么处理吗?请帮帮我。非常感谢你 这些是我的输出。 非常感谢大家。对不起,我的问题不清楚 输入名称:khoi 输入id:abcde 输入号码:1234567890 khoi abcde1234567890 1234567890 在给定结构的这两个变量中,您需要为空终止符多留出一个空间: #include <stdio.h> #include <stdbool.h> #include <string.h> #inc

我得到了第二个输入合并和第三个输入合并。有人知道怎么处理吗?请帮帮我。非常感谢你 这些是我的输出。
非常感谢大家。对不起,我的问题不清楚

输入名称:khoi
输入id:abcde
输入号码:1234567890
khoi abcde1234567890 1234567890


在给定结构的这两个变量中,您需要为空终止符多留出一个空间:

#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <ctype.h>
typedef struct Student
{
    char name[256];
    char id[5];
    char number[10];
} Student;

Student add()
{
    Student student;
    printf("Input name: ");
    scanf("%s", student.name);
    printf("Input id: ");
    scanf("%s", student.id);
    printf("Input number: ");
    scanf("%s", student.number);
    return student;
}

int main()
{
    Student s = add();
    printf("%-20s%-20s%-20s\n", s.name, s.id, s.number);
}
要添加一个,只需在长度中再添加一个:

char id[5];
char number[10];
您将获得所需的输出:

char id[6];
char number[11];


还要注意的是,在
scanf()
中指定宽度不会使程序变得更糟,它接受有限的用户输入,因此减少了程序中出现问题的可能性。

C语言使用所谓的C字符串,其中文本后跟空字节。例如,要存储5个字符串
“hello”
,您将使用6个字节并存储
{h',E',l',l',o','\0'}
,请参阅:

您可以在不使用空终止符的情况下使用固定大小的字符串,但您需要在编写和使用字符串时始终指定长度:

您的
id
字段与
number
串联的原因是
id
的空字符串被保存到
number
的第一个字符,然后
number
被写入该字段

解决方案是使用足够大的字符串,就像您使用名称一样,并按照建议指定宽度。

“这些是我的输出”-确定。您的具体问题是什么?字符串“1234567890”不能存储在大小为10的数组中。您调用了未定义的行为。
“abcde”
也不能存储在5的数组中。没有用于终止符的空间。在没有宽度说明符的情况下,绝对不能在scanf中使用
“%s”
。在这种情况下,使用
scanf(“%9s”,student.number)
。宽度说明符指示scanf使用的字节数,并且必须不大于缓冲区大小的1,以便为空终止符留出空间。
Input name: khoi     
Input id: abcde
Input number: 1234567890
khoi                abcde               1234567890