我被要求在我的大學做一份海鮮菜單作爲一項任務。我會在這裏提供的相關代碼:%c與%s的實際區別
int main()
{
int a,d;
float c ;
char b,s,S,m,M,l,L;
printf ("\n\t\t\t\tSeafood Menu\n");
printf ("----------------------------------------------------------------------------------\n");
printf ("\t\t\t\t\t\t Dish Size\n");
printf ("Item Number\t Seafood Dish\t Small\t Medium Large\t\n");
printf ("----------------------------------------------------------------------------------\n");
printf ("\t 1\t Fried \t20.00 \t\t 40.00 \t\t 55.00\n");
//Continue with a bunch of menu here
printf (" Enter item number :");
scanf ("%d",&a);
printf (" Enter dish size (S/M/L) :");
scanf ("%s",&b);
if ((a==1)&&((b=='s')||(b=='S')))
{c=20.00;}
//continue with a bunch of condition to choose the price per dish stated in the menu
printf (" Enter dish quantity :");
scanf ("%d",&d);
printf (" Price per dish size :RM%.2f\n\n\n",c);
return 0;
}
當我試圖在這個轉換格式標識符%C,它只是停止接受輸入特定scanf函數。
printf ("Enter dish size (S/M/L):");
scanf ("%s",b);
我想附圖像,但似乎不允許我這樣做想留兩個鏈接:
Normal, using %s:和abnormal, using %c
我很好奇,爲什麼它不「當我使用%c
時工作,而%s
工作?我所進入的只有性格。請賜教。
這兩種變體都會調用未定義的行爲。請閱讀'scanf'的文檔以及一個字符和一系列字符的區別_。一個C字符串。 – Olaf
在您方便的C參考手冊中應明確說明'%c'和'%s'的行爲。如果您沒有方便的C參考手冊,我推薦Harbison&Steele的[C:A參考手冊](http://careferencemanual.com)。 '%c'從輸入流中讀取* single *字符並將其存儲到單個'char'對象。 '%s'從輸入流中讀取字符值的*序列*,直到它看到空格或EOF並將它們存儲到char的*數組*中作爲0結尾的字符串。 –