首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 更多程式語言 >> 程式設計疑難解答

C 語言變量未聲明與拼寫錯誤導致無法運行

     編輯:程式設計疑難解答

錯誤代碼


int toes;
toes = 10;
acuonts = toes + toes;    // 拼寫錯誤:應為 accounts
squared = toes * toes;    // 未聲明 squared
printf("the toes is %d,the acounts toes is %d,the squared toes is %d,\n", toes, acounts, squared);

問題

  1. `acuonts` 拼寫錯誤 (少 'c'),且未聲明。
  2. `squared` 未聲明類型。
  3. `printf` 中 `acounts` 與聲明不一致。

修正代碼


#include 

int main(void) {
    int toes = 10;
    int accounts = toes + toes;   // 聲明並修正拼寫
    int squared = toes * toes;    // 聲明
    printf("the toes is %d, the accounts toes is %d, the squared toes is %d\n", toes, accounts, squared);
    return 0;
}

編譯器提示

GCC/Clang `-Wall` 會報:
`error: 'acuonts' undeclared`
`error: 'squared' undeclared`
`warning: 'acounts' undeclared (did you mean 'accounts?')`

良好習慣

  • 聲明即初始化:`int accounts = toes + toes;`
  • 使用 IDE 代碼補全避免拼寫錯誤。
  • 編譯開啟最高警告級別 (`-Wall -Wextra` / `/W4`),視警告為錯誤 (`-Werror` / `/WX`)。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved