我的输出有一些奇怪的符号显示

我不得不为我的课程编码。 编码是关于要求用户键入他们的姓名,年龄和身份。 然后程序应该根据他们名字中的前6个字母,他们的年龄和他们学生ID中的前两个字母的密码。 问题是输出中有一个未识别的符号(╠╠╠╠╠╠╠╠╠╠╠╠╠╠)。 谁能告诉我它为什么存在>? 然后它应该计算并显示密码的长度。 这是代码:

#include  #include  void main(void) { char name[20], id[9], age[3], passcode[10]; int x; puts("Enter your name:\n"); gets(name); puts("\nEnter your student id:\n"); gets(id); puts("\nEnter your age:\n"); gets(age); x = strlen(passcode); strncpy(passcode, name, 6); strncat(passcode, id, 2); printf("The passcode is:%s \n",passcode); printf("The passcode has %d characters..\n",x); } 

它看起来像:

 Enter your name: johnny Enter your student id: dc87671 Enter your age: 20 The passcode is:johnny╠╠╠╠╠╠╠╠╠╠╠╠╠╠20dc The passcode has 22 characters.. Press any key to continue . . . 

密码有22个字符

然而,您为密码分配了10个字符的缓冲区

 passcode[10] 

你不是null终止密码。 来自strncpy的注释

如果source长于num,则不会在目标末尾隐式附加空字符。 因此,在这种情况下,目标不应被视为空终止的C字符串(因此读取它会溢出)。

还要注意该行

 x = strlen(passcode); 

在初始化之前对代码进行操作。 因此,它将包含随机位,使得x的值没有很好地定义。 您目前不使用x,因此它不会直接影响手头的问题。