我對c編程語言很陌生。我寫了這段代碼,我想知道是否有任何方法 寫這個代碼更短,更有效?變量和scanf函數的問題
代碼:
#include <stdio.h>
int main(){
printf("This is my first program.\nPlease put in your name...\n");
char letter[5];
scanf("%c%c%c%c%c%c", &letter[0], &letter[1], &letter[2], &letter[3], &letter[4], &letter[5]);
if(letter[0] == 't' && letter[1] == 'r' && letter[2] == 'a' && letter[3] == 'v' && letter[4] == 'i' && letter[5] == 's'){
printf("Access Granted\nWelcome Travis.\n");
return 0;
}
else{
printf("You are not autorized.\nThis program will exit now...\n");
getchar();
}
}if(letter[0] == 'b' && letter[1] == 'o' && letter[2] == 'b'){
printf("Access Granted\nWelcome Bob.\n");
return 0;
}
對於工作代碼和代碼審查 - 請參閱codereview.stackexchange.com – 2014-11-01 14:50:06
修復後一對大括號:http://coliru.stacked-crooked.com/a/ac471e129d93ece1 – chris 2014-11-01 14:50:06
你讀過[scanf(3)]的文檔嗎(http://man7.org/linux/man-pages/man3/ scanf.3.html)(你應該測試它的結果)?你是否編譯過所有警告和調試信息('gcc -Wall -Wextra -g')?你**使用調試器**('gdb')嗎? – 2014-11-01 15:49:04