2017-04-13 73 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

相關問題