2016-02-20 102 views
-1

我正在學習C,我只是想輸出用戶輸入的字符串的第一個字符。不知何故,它不工作?我也沒有錯誤信息。這一定是一個非常簡單的問題,但我不明白。字符串的簡單字符輸出

#include <stdio.h> 

int main(void) 
{ 
    char input[200]; 
    char test; 
    printf("Text input: "); 
    scanf("%s", input); 
    test = input[0]; 
    printf("%s", test); 
    return 0; 
} 
+5

'printf(「%s」,test);'應該是'%c' - 你不能混淆控制字符串。 – artm

+0

感謝您的提示!但它仍然不起作用。程序仍然不會做任何事情。 – Valakor

+0

嘗試在scanf語句之前添加'fflush(stdin);'。 @Valakor –

回答

2

您需要使用%c打印char%s用於空終止的字符串,即字符數組。下面的代碼適合我。

#include <stdio.h> 

int main() { 
    char input[200]; 
    char test; 
    printf("Text input: "); 
    scanf("%s", input); 
    test = input[0]; 
    printf("%c\n", test); 
    return 0; 
} 
+0

工作。謝謝! – Valakor

1

試試這個

#include <stdio.h> 

int main() { 
char input[200]; 
char test; 
printf("Text input: "); 
scanf("%s", input); 
test = input[0]; 
printf("%c\n", test); 
return 0; 
} 

這工作。 U需要使用%c而不是%s來打印字符

+1

工作。謝謝! – Valakor