用注释替换源代码后,直接跳过从键盘输入sex的值,执行salary的输入,什么情况?不好编辑,有点难看,谅解

#include
#include
#include

struct worker_List
{
char name[20];
char sex[1];//char sex;
long salary;
};

int main()
{
int len = 0;
struct worker_List *str;
int i = 0;
char name[20];
printf ("please input the number of workers:");
scanf ("%d",&len);
str = (struct worker_List *)malloc(len * sizeof(struct worker_List));
for (i=0; i<len; i++)
{
printf("the %dth worker's information:\n",i+1);
printf("the name is:");
scanf("%s",str[i].name);
printf ("the sex is:");
scanf ("%s",str[i].sex);//此处写%c会直接跳过这个输入语句scanf ("%c",&str[i].sex);
printf ("the salary is: ");
scanf ("%d",&str[i].salary);

}
printf ("please input a name:");
scanf ("%s",name);

for (i=0; i<len; i++)
{
    if (strcmp(name,str[i].name))
    {
      printf ("the sex is %s\n",str[i].sex);// printf ("the sex is %c\n",str[i].sex);
      printf ("the salary is %d",str[i].salary);
    }
}
return 0;   

}

用%c会把上次的回车键取走,所以就感觉跳过了,其实是已经执行过了(取走了回车键)

你那个是字符数组,不是字符串,所有要用 c 接收

%c只能是一个字符;
%s可以是一个字符串。

这么修改看看:
struct worker_List
{
char name[20];
char sex[8];//char sex;
long salary;
};

字符串有个结束符,如果输入F或者M的话,sex至少需要2个字节。

1.哈哈,你的程序之所以没有出问题,是编译器帮忙了,虽然你只给sex变量分配了1个字节,但是编译器还是按照4字节对齐,给你分配了4字节。
2.另外,你程序最后是不是打印最后输入的用户信息,如果是这样,代码就需要改成:
if (strcmp(name,str[i].name) == 0)