Comparison of macros and functions (c + +)

Source: Internet
Author: User

Macros and functions and comparisons in C + +:

1, frequent use and perform simple calculations can use macros, and other more cumbersome, complex programs can be implemented with functions.

2, the macro definition is independent of the type, and the function needs to pay attention to the type of parameters.

3, the name of the macro definition is all uppercase, and the function name is the use of letters and "_" to name.

4, macro parameters have certain side effects, you need to note: "()" use, and the function does not exist these problems.

5. In C + +, macros are generally not used (macro functions--inline functions, solid---const, enum).

6. In C, macros cannot be debugged, and there is no type security check, but in C + +.

#include <stdio.h> #define M 9#define CAT (x, y) x# #Y # define printf (Format,value) printf ("The VALUE of" #VALUE ":" FORMAT "", VALUE), int main () {PRINTF ("%d\n", M);p rintf ("%d\n", CAT (123,321));p rintf ("File:%s\t line:%d\t date:%s\t Time :%s\n ", __file__,__line__,__date__,__time__); return 0;}


This article is from the "Flower Open Shore" blog, please be sure to keep this source http://zxtong.blog.51cto.com/10697148/1752737

Comparison of macros and functions (c + +)

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.