2016-02-20 103 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