C語言中有這個函式
system("Color x")
這會改變控制臺文本的顏色。但是我一直想知道如何通過輸入所需的顏色來使用此功能和用戶輸入,例如:
char color;
printf("Enter color: "); //user enters A/B/C etc.
scanf(" %c", &color);
system("Color %c", color);
uj5u.com熱心網友回復:
該system命令不像命令系列那樣接受引數print(即格式字串后跟帶有變數名稱的變數引數串列)。解決方案是首先使用sprintf. 首先,為目標字串分配記憶體:
char cmd[8]; // E.g. "Color x" has 8 chars when including the null terminator.
然后,使用sprintf組合格式字串和變數,并將結果寫入cmd:
sprintf(cmd, "Color %c", color);
最后,system使用剛剛寫入的字串呼叫:
system(cmd);
這是假設您要使用此方法更改顏色。如需更好的方法,請參見此處。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/516699.html
標籤:C颜色
