C: Macro Definition

Source: Internet
Author: User
Tags definition

When you use a macro, enclose each parameter in parentheses and enclose the overall definition of the macro; In general, do not use the self-increment or decrement operator in a macro.

[1] Maximum value

#define MIN (x, Y) (((x) < (y))? (x): (y))

Ask the lesser of the two

#define MAX (x, y) ((x) > (y))? (x): (y))

Ask the larger of the two

#define MAX3 (X,Y,Z) max (Max (x,y), z)

The larger among the three

[2] Shift

#define SHIFT (value, N) (((n) > 0)? ((value) << (n))

: ((value) >>-(n))

Shift operation on value, n > 0 left SHIFT, N < 0 right Shift

[3] Character

#define IS_LOWER_CASE (x) ((x) >= ' a ') && ((x) <= ' z '))

Judge Lowercase

#define IS_UPPER_CASE (x) ((x) >= ' A ') && ((x) <= ' Z '))

Judge Uppercase

#define Is_alphabetic (c) (Is_lower_case (c) | | Is_upper_case (c))

Judging letters

#define IS_DIGIT (x) (((x) >= ' 0 ') && ((x) <= ' 9 '))

Judging numbers

#define Is_special (c) (!is_digit (c) &&!is_alphabetic (c))

Judging special characters

[4] Take absolute value

#define ABSOLUTE_VALUE (x) ((x) < 0)?-(x): (x))

[5] #运算符

#define DPRINT (expr) printf (#expr "=%.1fn", expr)

#参数: Will output the quoted parameter value;

Dprint (1.0/2.0); will output: 1.0/2.0=0.5;

[6]# #运算符

#define XPRINT (Front,back) Front # back

XPRINT (x,1); will output: x1

[7] Judging leap years

#define LEAP_YEAR (x) ((x)% 4 = 0 && (x)% 100!= 0) | | ((x)% 400 = 0))

[8] integer val takes bit of nth bit

#define BIT (Val,n) (((VAL) >> (N)) & 1? 1:0)

This article is from the "Zi Postman" blog, please be sure to keep this source http://zhangjunhd.blog.51cto.com/113473/54402

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.