Some special usage of C language (# define)

Source: Internet
Author: User

I haven't written a BLOG for a long time. ^_^

If you don't know what to write, write something that is easy to ignore.


I. Special string macros

[Cpp]
# Define A (x) T _ # x
# Define B (x) # @ x
# Define C (x) # x

Assume that x = 1, the macro definition above will be interpreted as follows:
A (1) ------> T_1
B (1) ------> '1'
C (1) ------> "1"


These functions are mainly used in string processing functions and parameter naming. They are not complex, but few are known.


Ii. Shielding useless parameter warnings

[Cpp]
# Define UNUSED_PARAM (p) (void) p)
This is used to block invalid Parameters
For example

[Cpp] view plaincopy
Void a (int x1, int x2)
{
// Do nothing
}

A warning is displayed. x1 and x2 are invalid parameters.
However, it doesn't matter if you write it like this.

[Cpp]
Void a (int x1, int x2)
{
UNUSED_PARAM (x1 );
UNUSED_PARAM (x2 );
}

3: I can't think of it. I want to hold a place and wait until I wake up.


I will not write more common macro usage, such as the header file security macro, version definition macro, flexible use indeed get twice the result with half the effort.

Author: vonger

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.