在程序中打印无效字符

kap*_*pil -1 c struct

我写了这个程序

#include<stdio.h>
struct student
{
    char name[22];
    char rollno[10];
    unsigned long long int phno;

};
int main()
{
    int i,j;
    char c[1];
    struct student cse[10],*p;
    p=cse;
    for(i=0;i<3;i++)
    {
            printf("enter student name\n");
            gets(cse[i].name);
            printf("enter student roll number \n");
            gets(cse[i].rollno);
            printf("enter student phone number \n");
            scanf("%llu",&cse[i].phno);
            gets(c); //to catch the '\n' left unprocessed by scanf
    }
    for(i=0;i<3;i++);
    printf("the following is the information about CSE B student\n");
    printf("%-6s%-24s%-14s%-14s \n","S.no","student Name","Roll no","phone no.");
    for(i=0;i<3;i++)
    {
            printf("%-6d%-24s%-20s%-14llu \n",i+1,(*p).name,(*p).rollno,(*p).phno);
            ++p;
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

输出是

the following is the information about CSE B student
S.no  student Name            Roll no       phone no.      
1     kapil                   1234567890??I      1234567890     
2     kumar                   9876543210??L     9876543210     
3     sharma                  5123467890??a1     5123467980     
Run Code Online (Sandbox Code Playgroud)

Roll no coloumns中有一些不需要的和不可理解的字符,这些无效字符的打印原因是什么

P.P*_*.P. 5

该阵列rollno仅存储10个字符,但您输入10个或更多字符.如果你的滚动数是10位数,你需要至少11个字符作为字符串打印,一个额外的终止空字节.这是技术上未定义的行为,使您的程序无效.

请注意,gets()自C11以来已经弃用,您应该使用它fgets().