Macros and functions

Source: Internet
Author: User

/***1. Macros are simply replaced when the compiler is compiling the source code, without any logical detection, that is, simple code copying. 2. When a macro is defined, the type of the parameter is not considered. 3. The use of a parameter macro causes a block of code with the same effect to have multiple copies in the destination file, which increases the size of the target file. 4. Parameter macros run faster than functions because they do not require a parameter stack/stack operation. 5. The parameter macro should be more careful when defining, extra parentheses. 6. The function only exists in one place in the target file, which saves the program space. 7. Function calls will involve the transfer of parameters, the stack/stack operation, the speed is relatively slow. 8. The parameter of the function has the problem of passing the value and passing the address (pointer), the parameter macro does not exist. */#include<cstdio>typedefstructpoint3d{floatx; floaty; floatZ;} Point3D;//function Mode//void Point3d_print (const point3d* PD)//{//printf ("%g,%g,%g", Pd->x, Pd->y, pd->z);//%g Select%e or%f printf ("%.2g", 0.0123) according to the specific values, and the ====> 0.012 effect is equivalent to the number of significant digits removed after 0//}//macro Way, for simple functions you can consider using macros, because faster#definePoint3d_print (PD) \printf ("%g,%g,%g", Pd->x, Pd->y, pd->z);intMain () {Point3D PD= {1.01f,1.0f,1.0f}; Point3D* p = &PD; Point3d_print (p);//This is not a direct use of &PD, you must define a pointer, such as p, this macro a bit of a pit barGetChar (); return 0;}

Macros and functions

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.