Program Ape's---C language details 9

Source: Internet
Author: User

Main content: Macro definition, Max (A, b) macro definition details, size end judgment, (int&) A What do you mean

#if 1 #include <stdio.h>//note the space #define F (x) ((x)-1)//F for the following # define F (x) ((x)-1)//f (x) represents the following # define T1 St  Ruct Type*t1 A, B;  Using the above in defining multiple variables to achieve the purpose, is resolved to a struct type* a, b; A is a pointer to a struct, and B is defined as a struct//classic pen question macro definition Max//#define MAX (A, B) a>b?a:b//This is easy to mistake, the following comparison is not easy wrong, but also can be wrong when #define MAX (A, b) ((a) > (b)? (   A):(B)//written in such a way that there may still be problems, the explanation is as follows//and this macro definition does not consider passing in different types of parameters,/* If an operand is used in two places, it is evaluated two times. * If there are side effects in the expression max (A, b), it is calculated once when the size is compared, and when the value is calculated later, the error example is as follows: */int main (int argc, char** ar    GV) {int x[3] = {2,3,1};    int biggest = x[0];        int i = 1;  while (I < 3) {biggest = Max (biggest, x[i++]);                                    Disassembled for biggest > x[i++]?biggest:x[i++], (biggest=2) < (x[1]=3)//Key point is I have side effects when comparing size, i++ after I have added 1,biggest=x[2]}    The workaround is to determine if there are no side effects in the parameter, or write the Max function printf directly ("Biggest is%d\n", biggest); A Max macro definition that considers passing in different variable types//The correct answer should be: #define MAX (A, B) ({const typeOf (a) _a = A;            Const typeof (b) _b = b; (void) (&_a = = &_b); \ _a > _b? _a: _b;}) return 0;} Here is the method of testing the size end, already using # if #elif need to open the corresponding switch//supplement One (int&) a What does it mean?    (int&) A is * (int*) (&a) #elif 1 #include <stdio.h>int main () {int x = 1;    Char *p = (char *) &x;    if (*p) {printf ("little\n");    } else {printf ("large\n"); } return 0;}    #else #include <stdio.h>int main () {short int x;    Char x1,x2;    x = 0x1122;    X1 = ((char *) &x) [0];    Low Address x2 = ((char *) &x) [1];    High address printf ("x1=%x\n", X1);    printf ("x2=%x\n", x2); return 0;}  #endif

Output:

Macro definition and Size end


Program Ape's---C language details 9

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.