Genius usage of C macro

Source: Internet
Author: User
Let's see how foreigners use macros. This example is taken from expert C programming. A small bitwise pattern is used to map an icon or graph (glyph) to an image generated on the screen. One digit represents a pixel in the image. If a single bit is set, the pixel it represents is "bright. If a single bit is cleared, the pixels it represents are "dark. Therefore, a series of integer values can be used for image encoding. Tools like iconedit are used for plotting. They output an ASCII file containing a series of integer numbers, which can be used by a window. Program . The problem is that the icon in the program is only a string of hexadecimal numbers. In C, a typical 16x16 black/white image may be as follows: static unsigned short stopwatch [] = {
0x07c6,
0x1ff7,
0x383b,
0x600c,
0x600c,
0xc006,
0xc006,
0xdf06,
0xc106,
0xc106,
0x610c,
0x610c,
0x3838,
0x1ff0,
0x07c0,
Zero x 0000
};

As we can see, these C-language constants do not provide any clue about the actual image. Here is an amazing # define-defined elegant set that allows programs to create constants to make them look like images on the screen. # Define x) * 2 + 1
# DEFINE _) * 2
# Define s (0/* for building glyphs 16 bits wide */

After they are defined, the program will automatically create their hexadecimal mode as long as they draw the required icons or graphics. Using these macro definitions, the self-describing capability of the program is greatly enhanced. The above example can be changed to: static unsigned short stopwatch [] =
{
S _ x _,
S _ x,
S _ x,
S _ x __,
S _ x __,
S x _,
S x _,
S x _,
S x _,
S x _,
S _ x __,
S _ x __,
S _ x ___,
S _ x ____,
S _ x ______,
S ________________
};

ObviouslyCodeIn contrast, it means more obvious. The standard C language has octal, decimal, and hexadecimal constants, but there is no binary constant. Otherwise, it is a simpler method for drawing the image mode.

If you capture the top-right corner of the book and see this page oblique, you may guess that this is a small stopwatch image named "cursor busy" for the popular window system. I learned this technique from Usenet Comp. Lang. C News group a few years ago.

Do not forget to clear these macro definitions after the drawing is complete. Otherwise, this may cause unpredictable consequences for your subsequent code.

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.