Special define usage in C

Source: Internet
Author: User


Today, one of my colleagues told me another use of define, which I did not know before! I am very grateful to him for telling me that he is willing to share it with me. How can I "eat! Haha! So I want to record it and share it with you, and I will not forget it. Okay, now !!


First, let's look at an example:

This is common and can be used by everyone. But can it be used as follows?

Well, let's compile it to see what the situation is. <Strong? Http: www.bkjia.com kf ware vc "target =" _ blank "class =" keylink "> VcD4KPHA + ICA8aW1nIHNyYz0 = "http://www.2cto.com/uploadfile/Collfiles/20140319/20140319092240332.jpg" alt = "\">

Let's take a look at how the macro works! (Macro expansion: $: gcc-E-P tt. c> file. After opening the file, you will see it)


An error was found during compilation and I variable was redefined. Why?

Because the defined macros are expanded in the call place during compilation, variable redefinition occurs when two macro definitions FUN () are called in the same scope.

So is there any way to make this call correct? See the following:

After compilation, the error cannot be found. Why? Let's take a look at the expanded macro:

No error occurs because two different variables, i1, i2, are generated here ,.





Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.