簡體   English   中英

為什么我不能在以for循環中的條件為例時動態聲明變量?

[英]why I can't declare variables on the fly while making the conditions in the for loop as an example?

我制作了這段代碼,使用他們的 ascii 代碼遍歷字母表

#include <stdio.h>

int main() {
    for ( int alphabet = (int) char A = 'A'; alphabet <= (int) char Z = 'Z'; alphabet++) {
        printf("The number of the Alphabet %c is %d\n ",(char) alphabet , alphabet  );
    }
}

但是在編譯它時只是說它應該在char Achar B之前有一個表達式,我真的不明白這是什么意思,所以任何幫助將不勝感激 xD

您不需要定義像(int) char A = 'A'這樣的標識符來表示字符。

只需執行以下操作:

for (int alphabet = 'A'; alphabet <= 'Z'; alphabet++)
    printf("The number of the Alphabet %c is %d\n", alphabet, alphabet);

請注意,您不需要在printf()中使用(char) ,它會自動轉換為 integer 中的字符。 此外,您不需要在循環和條件中對單一語法使用花括號,建議這樣做以避免混淆它們的結束范圍。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM