在編譯的時(shí)候報(bào)錯(cuò):error: ‘for’ loop initial declarations are only allowed in C99 or C11 mode
報(bào)錯(cuò)截圖如下所示
報(bào)錯(cuò)截圖
這是因?yàn)?gcc 在編譯中是基于 C89 標(biāo)準(zhǔn)的安券,這個(gè)標(biāo)準(zhǔn)不允許在 for 循環(huán)內(nèi)聲明變量,而需要在循環(huán)前聲明鹦筹,如下所示
int index;
for (index = 0; index < LENGTH_LIMIT; index ++) {
? ? ----> Do something;
}
而如報(bào)錯(cuò)中 note 提示的那樣址貌,切換成 C99 標(biāo)準(zhǔn)就允許在 for 循環(huán)內(nèi)聲明變量了,如下所示
for (int index = 0; index < LENGTH_LIMIT; index ++) {
? ? ----> Do something;
}
切換標(biāo)準(zhǔn)的方法就是在編譯的時(shí)候遍蟋,增加 -std=c99螟凭,如下所示
gcc func.c -std=c99 -o func