Common macro definition skills in C Language

Source: Internet
Author: User
1. prevent a header file from being repeatedly contained
# Ifndef comdef_h
# Define comdef_h

2. redefine some types to prevent the differences in the number of bytes of the types caused by different platforms and compilers, so as to facilitate migration.
Typedef unsigned long int uint32;/* unsigned 32 bit value */
Typedef unsigned short uint16;/* unsigned 16 bit value */

3. Get a byte or word on the specified address.
# Define mem_ B (x) (* (byte *) (x )))
# Define mem_w (x) (* (word *) (X )))

4. Calculate the maximum and minimum values.
# Define max (x, y) (x)> (y ))? (X): (y ))
# Define min (x, y) (x) Field ))

6. Get the number of bytes occupied by field in a struct.
# Define fsiz (type, field) sizeof (type *) 0)-> field)

7. convert two bytes into a word in LSB format
# Define flipw (Ray) (Word) (Ray) [0]) * 256) + (Ray) [1])

8. convert a word into two bytes in LSB format
# Define flopw (Ray, Val)

(Ray) [0] = (VAL)/256 );

(Ray) [1] = (VAL) & 0xff)

9. Get the address of a variable (word width)
# Define B _ptr (VAR) (byte *) (void *) & (VAR ))

# Define w_ptr (VAR) (word *) (void *) & (VAR ))

10. Obtain the high and low bytes of a word.
# Define word_lo (XXX) (byte) (Word) (XXX) & 255 ))

# Define word_hi (XXX) (byte) (Word) (XXX)> 8 ))

11. Return a multiple of 8 nearest to X.
# Define rnd8 (x) + 7)/8) * 8)

12. convert a letter into uppercase letters.
# Define upcase (C) (c)> = 'A' & (c) = '0' & (c) = '0' & (c) = 'A' & (c) (VAL ))? (VAL) + 1: (VAL ))

16. Return the number of array elements
# Define arr_size (A) (sizeof (a)/sizeof (A [0])

17. Use some macro tracking for debugging
The ANSI standard specifies five predefined macro names. They are:
_ Line __
_ File __
_ Date __
_ Time __
_ Stdc __
C ++ also defines _ cplusplus

If the compiler is not standard, only a few of the above macro names are supported or not supported at all. Remember to compileProgramOther predefined macro names may also be provided.

_ Line _ and _ file _ macro instructions. # The line command can change its value. In short, during compilation, it contains the current number of lines and file names of the program.

The _ date _ macro command contains a string in the form of month/day/year, indicating that the source file is translatedCode.
The _ time _ macro command contains the program Compilation Time. Time is represented by a string in the form of minute: Second
The significance of the _ stdc _ macro command is defined during compilation. Generally, if _ stdc _ has been defined, the compiler will only accept standard C/C ++ code that does not contain any non-standard extensions. If the implementation is standard, macro _ stdc _ contains the decimal constant 1. If it contains any other number, the implementation is non-standard.
The consistent compiler of _ cplusplus and Standard C ++ defines it as a value containing at least six characters. A compiler that is inconsistent with the standard C ++ will use a value of 5 or less.

You can define macros, for example:
When _ debug is defined, the output data and the row of the file
# Ifdef _ debug
# Define debugmsg (MSG, date) printf (MSG); printf ("% d", date, _ line _, _ file _)
# Else
# Define debugmsg (MSG, date)
# Endif

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.