我正在編寫一個C程序,它可以接受來自命令行的字符輸入。如果用戶在命令行輸入字符,程序應該打印這些字符的ascii值。我遇到了以下問題:1)編寫printf語句; 2)如果用戶沒有從命令行發送任何內容,則跳過打印輸入。以下是我已經寫了:C:打印元素* argv
int main(int argc, char *argv){
char thisChar; //Holds the character value of the current character.
int ascii; //Holds the ascii value of the current character.
int x = 1; //Boolean value to test if user input 0, our while loop break condition.
int i = 0; //Counter for the following for loop
if(argc > 0){
for(i; i<argc; i++){
thisChar = argv[i];
printf("%c\nAscii: %d\n", thisChar, thisChar);//prints the character value of thisChar followed by its ascii value.
}
printf("Done.");
}
}
當我把它從這樣的命令行:
./ascii F G h
輸出是:
�
k
�
�
Done.
是我printf的問題聲明?爲什麼if條件評估爲真,即使我沒有輸入?
咦? 'int main(int argc,char * argv)' – this