2017-04-13 70 views
-2

我试图让在C简单的代码,但我不能使用相同的变量:如何用C使用相同的变量进行不同的值

#include <stdio.h> 
#include <stdlib.h> 


int main(){ 

    char a; 

    printf("Type something\n"); 
    scanf("%c", &a); 
    printf("%c", a); 

    printf("\nType something else\n"); 
    scanf("%c", &a); 

    printf("something else -> %c", a); 
    return 0; 
} 

任何提示吗?

+1

当你说,你“不能使用相同的变量”究竟是什么问题呢?据我所知,这应该可行,假设你在每一个'printf'语句中加入''\ n“'。 – NoseKnowsAll

+1

你是什么意思_我不能使用相同的变量_?有编译器错误吗?你的输入和输出是什么? – Tas

+0

here printf(“别的 - >%c”,a);我没有得到任何东西,就像我不能再次使用变量(对我的英语感到抱歉) – NoobCoder

回答

0

scanf将字符读你输入的字符,例如:


你运行该程序,然后键入ab当它要求你输入,输出将是:

Type something 
ab 
a 
Type something else 
something else -> bPress any key to continue . . . 

注意最后一行中的b


如果键入a然后按回车,输出将是:

Type something 
a 
a 
Type something else 
something else ->  // Note: there is a new line character here 
Press any key to continue . . . 
+0

谢谢,我理解并使用Jonathan Leffler所说的修复! ^^ – NoobCoder

相关问题