The difference between macro definition and constant definition in C language

Source: Internet
Author: User
Tags constant definition

  They have a common advantage is "change the whole, to avoid the input error" What are the two different? Some.

The main difference is that the macro definition is done prior to compilation, and the const is processed during the compilation phase

Macros define constants that do not occupy memory cells and are defined by const occupy memory units

A macro definition has the same effect as a const constant-----the data is represented by a symbol, but some books say that defining an array constant cannot be used as a const, and it can be tested, and the environment is vs2015

 Constant definition defines the length of an array

const int n=66;

int arr[n];

Some of the books say it's wrong, but after I test it on the vs2015, I can.

  Macro definition defines the length of an array

#define N 66

int arr[n];

Macro definition with Parameters

Format:

#define the macro name (parameter list) to replace the content

#define SUM (A, b) a+v

The program code is as follows:

S=sum (6,8);

Replace A and b in the macro definition with 6 and 8, respectively, with the following code:

S=6+8;

#define没有数据类型, just a simple replacement

#include"stdafx.h"#include<stdlib.h>#defineAdd (a) (a) > (b)? (a):(B)intMain () {printf ("%s", Add ("ABC","BCD")); System ("Pause"); return 0;}
" stdafx.h " #include<stdlib.h>#define Add (a) > (b)? (a):(b)int  main () {    printf ("%d", add (1+2 3+4));    System ("pause");     return 0 ;}

These are all possible.

Therefore, it is generally recommended to use a function that does not recommend macro definitions

The difference between macro definition and constant definition in C language

Related Article

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.