簡體   English   中英

為什么#undef 不適用於我的 function?

[英]Why is #undef not working for my function?

我在一開始就定義了一些東西:

#define noprint

然后我在我的函數中返回,如果它被定義:

void print()
{
    #ifdef noprint
        return;
    #else
        //do stuff
    #endif
}

然后在主function中:

main()
{
    #undef noprint
    print();
}

它仍然不起作用。 怎么來的?

宏不是變量。 它們是一個簡單的文本替換工具。 如果定義或取消定義宏,則(取消)定義對宏之前的源沒有影響。 function 定義在定義后不會更改。

例子:

#define noprint
// noprint is defined after the line above

void print()
{
    #ifdef noprint // this is true because noprint is defined
        return;
    #else
        //do stuff
    #endif
}

main()
{
    #undef noprint
// noprint is no longer after the line above
    print();
}

預處理完成后,生成的源代碼如下所示:

void print()
{
    return;
}

main()
{
    print();
}

PS 你必須給所有函數一個返回類型。 main的返回類型必須是int

暫無
暫無

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

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