Home > Back-end >  About the C language macro definition
About the C language macro definition

Time:04-20

Sample: (not an error)
# define div (a, b) a % b;

Sample 2: (not an error)
# define div (a, b) a % b

Sample 3: (not an error)
T=# define change (a, b) a, a=b, b=t;

Sample 4: (not an error)
T=# define change (a, b) a, a=b, b=t

The sample 5: (error)
# define s (a, b, c) ((a + b + c)/2);
# define area (a, b, c, S) SQRT (S * (S - a) * (S - b) * (S - c));

The sample 6: (not an error)
# define s (a, b, c) ((a + b + c)/2)
# define area (a, b, c, S) SQRT (S * (S - a) * (S - b) * (S - c))

Want to ask about why the sample in the CB five complains, how to determine parameters of macro definition behind add do not add "; "?

CodePudding user response:

You are using is the Angle of the semicolon!

CodePudding user response:

What mistake? In the code and how to use s, area? These is the key, can't say that only the macro definition will be an error or no error, the key is how to use the macro definition is equivalent substitution, in the code to see whether conform to the rules of grammar ~

CodePudding user response:

VC compiler options/EP/P (projects, properties, configuration properties, C/C + +, the preprocessor, pretreatment to the file: is that the preprocessing cancel display line Numbers: is), recompile, view the corresponding after macro expansion. I file, GCC - E http://bbs.csdn.net/topics/391003898

CodePudding user response:

If s (a, b, c) directly involved in the operations in the code behind, do not add a semicolon, such as
S (a, b, c) + 123.//s can't have a plus sign in the

Direct call, when it does not participate in the operation, it can be added, suggest not to add the
S (a, b, c)

CodePudding user response:

reference 2 building self-confidence boy reply:
what mistakes? In the code and how to use s, area? These is the key, can't say that only the macro definition of an error or no error, the key is how to use the macro definition is equivalent substitution, in the code to see whether conform to the rules of grammar ~

You see, this is an error, the other is no problem,

CodePudding user response:

reference 1/f, play big shoot early nuclear response:
is the Angle you use semicolon.
use half-width symbols

CodePudding user response:

I feel two macro definition is continuous statement, so between the two there can be no "; "? When only a macro definition "; "Then dispensable?
Don't know why,,, a great god airborne answer,

CodePudding user response:

Macro is a textual substitution, that must be wrong, you
The equivalent of
Printf ("... "XXXXXX ; ).

CodePudding user response:

Surely not, look at the big upstairs cafe has equivalent substitution for you out,

refer to the eighth floor truth is right or wrong response:
macro is textual substitution, that must be wrong, you
The equivalent of
Printf ("... "XXXXXX ; ).

CodePudding user response:

refer to the eighth floor truth is right or wrong response:
macro is textual substitution, that must be wrong, you
The equivalent of
Printf ("... "XXXXXX ; ).

So all the macro definition end don't need a side "; "Isn't it?
But why, in some cases, can again?
For example:

CodePudding user response:

references to the tenth floor Dream1018 Lin response:
Quote: refer to the eighth floor of the truth is right or wrong response:
macro is textual substitution, that must be wrong, you
The equivalent of
Printf ("... "XXXXXX ; ).

So all the macro definition end don't need a side "; "Isn't it?
But why, in some cases, can again?
For example:
this is equivalent to perform a null statement, add a few didn't also feel
 t=div (x, y);;;;;; 

CodePudding user response:

With the do {... } while (0) enclose the macro, solve the problem of a semicolon,

CodePudding user response:

11 references QZJHJXJ response:
Quote: reference to the tenth floor Dream1018 Lin response:
Quote: refer to the eighth floor truth is right or wrong response:
macro is textual substitution, that must be wrong, you
The equivalent of
Printf ("... "XXXXXX ; ).

So all the macro definition end don't need a side "; "Isn't it?
But why, in some cases, can again?
For example:
this is equivalent to perform a null statement, add a few didn't also feel
 t=div (x, y);;;;;; 

Understand, thank you for your bosses

CodePudding user response:

11 references QZJHJXJ response:
Quote: reference to the tenth floor Dream1018 Lin response:
Quote: refer to the eighth floor truth is right or wrong response:
macro is textual substitution, that must be wrong, you
The equivalent of
Printf ("... "XXXXXX ; ).

So all the macro definition end don't need a side "; "Isn't it?
But why, in some cases, can again?
For example:
this is equivalent to perform a null statement, add a few didn't also feel
 t=div (x, y);;;;;; 

What's on?" ;" On behalf of the end of the statement, but in front of you is call printf function, the function call, the function name at the back of the parenthesis must match, so definitely not ah
  • Related