The difference between macros and functions in C language

Source: Internet
Author: User


One, the code length: the macro is completely replaced, inserted into the program to make the code longer;

The function code is only present in one place and is used when it is called.


Second, the implementation speed: macro faster, because in the pre-compilation phase has been completed;

The function is slower because of the need to call and return (stack/stack) extra time.


Third, operator priority: macros sometimes need less parentheses to cause the calculation logic error;

The function finally returns only the value, and the result is easy to predict.


Four, the parameter evaluation: the macro every time to calculate;

The function only needs to be evaluated once, and subsequent calls do not result in multiple evaluation errors.


Five, the parameter type: the macro does not care about the type, as long as the parameter operation is valid;

function types are consistent, and different types require different functions.


The difference between macros and functions in C language

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.