簡體   English   中英

MPLab / C prg /變量

[英]MPLab / C prg / Variables

我已經安裝了MPLab V8.43,並且剛剛安裝了用於編程的C18編譯器。 每當我嘗試構建小的測試代碼時,它都會在第一個變量聲明時停止。 它說有一種語法。

unsigned char counter;

在我看來並沒有錯...即使是作為未簽名的char計數器[1]也是如此; 而且它仍然向我拋出語法錯誤。 是什么賦予了? 有任何想法嗎?

必須在塊的頂部(在這種情況下為函數)的頂部聲明局部變量。這符合C89標准。

這些是可以接受的:

void functionname(void)
{
    unsigned char counter;

    /* rest of code */
}

void functionname(void)
{
    /* code */

    for (unsigned char counter = 0; counter<30; counter++)
    {
    }

}

這是不可接受的:

void functionname(void)
{
    /* code */

    unsigned char counter = 0; 

    /* more code */

}

由於您具有帶有char數據類型的計數器變量。 但是它不是數組或字符串。

  so you can't access it by counter[1].

您可以在main中定義局部變量,但是應該對其進行定義,以使它們不會跟隨變量賦值塊或代碼執行塊。

這是MPLAB C18中的有效變量聲明/定義:

void main ()
{
    /* Declare or Define all Local variables */
    unsigned char counter;   
    unsigned char count = 5;

    /* Assignment Block or the code Execution Block starts */ 
    conter++;
    count++; 
}

但是,這是無效的,並且會導致“語法錯誤”:

void main ()
{
    /* Declare or Define all Local variables */
    unsigned char count = 5;

    /* Assignment Block or the code Execution Block starts */ 
    count++; 

    /* What??? Another variable Declaration / Definition block */ 
    unsigned char counter;     /* Hmmm! Error: syntax error */ 
}

希望有幫助!

暫無
暫無

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

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