-2
在以下c代碼中,scanf不起作用。 當程序運行時,它執行到第二個printf行,並跳過scanf(「%c」,& sex);並直接執行下一個printf()。 爲什麼會這樣發生?我在不同的c編譯器上運行這個代碼,但輸出是一樣的。運行時錯誤的Scanf();
#include<stdio.h>
void main()
{
char mar,sex;
int age,flag=0;
printf("Married [Y/N]:");
scanf("%c",&mar);
printf("Sex [M/F] :");
scanf("%c",&sex); //**This not working**
printf("Age :"); //**execution directly jumped here**
scanf("%d",&age);
if(mar=='y')
flag=1;
else if(sex=='m'&& age>=30)
flag=1;
else if(sex=='f'&& age>=25)
flag=1;
else
{
}
if(flag)
{
printf("Congratulations!!!! You are Egligible..");
else
printf("Sorry... You are not egligible..");
getch();
}
//輸出
Married [Y/N]:y
Sex [M/F] :Age :23
Congratulations!!!! You are Egligible..