Comparison between macros, functions, and constants

Source: Internet
Author: User

Differences between macros and functions:
(1) memory Stack: When the function is called, the address is put into the stack. After the function is executed, the address of the called function is returned from the stack to the program. However, macro expansion directly replaces the macro name with the replacement ID.
(2) time effect: calling a function requires on-site protection and recovery. It takes a lot of time, but it does not extend the target program. macro development runs during compilation without occupying the running time, but it will make the target program become longer.
(3) parameter transfer: When a function with parameters is called, copy the values of the real parameters first, and then pass the copied values to the form parameters. Expand the macro with parameters and replace the real parameters directly.
(4) parameter type: the parameter type must be specified for a parameter function parameter. The parameter in a parameter macro does not have a type, and the real parameter can be of any type.

 

Use enumeration and use # define to define the difference between constants and const keywords (from the advantages and disadvantages)
# Define command

Advantage: it is stated that it is used multiple times at a time and occupies less memory because no variable space needs to be allocated in the memory.
Disadvantage: The only drawback is that most debugging programs cannot check constants declared by # define.

Enumeration
Advantages:
Easy to maintain
Easy to read
Easy to debug
Disadvantage: occupying more memory: Because the enum keyword needs to be used to allocate memory for the stored Variables

Const keyword
Const defines that a symbolic constant has a type. In a local constant, it is limited to the function body and has a semicolon.

Differences between local variables and global variables

(1) variable type: local variables can be auto, register, and static declared automatic type variables, while local variables can only be static declared static class variables.
(2) Life Cycle: local variables are visible in their own scopes, while global variables are visible throughout the life cycle of the program.
(3) memory allocation method: local variables are dynamically allocated in the thread stack. If they are not initialized, they are determined by the value stored in the current memory. global variables are statically allocated, if no initialization value exists, the default value is 0;

 

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.