About Macro Functions

Asked 1 years ago, Updated 1 years ago, 37 views

I'm studying macro functions.

If you execute the following code, you will receive an error.
Please tell me the solution.

error message

 error: lvalue required as left operand of assignment
 # define ABS(x)x>=0?x=x*1:x=x*-1;
                                         ^
.\5.c:9:5:note:in expansion of macro 'ABS'
     ABS(x);
     ^~~

source code

#include<stdio.h>

# define ABS(x)x>=0?x=x*1:x=x*-1;

int main() {
    int x, y;
    scanf("%d%d", x, y);
    x + = y;
    ABS(x);
    printf("%d\n", x);
    return 0;
}

c

2022-09-30 21:44

3 Answers

Using macro functions is not directly related to errors.If you write without macros, the ABS(x); part looks like this, but you should still get the same error.

x>=0?x=x*1:x=x*-1;
;

Here, the trinomial operator has a higher priority than the substitution operator, so

(x>=0?(x=x*1):x)=x*-1;

(x>=0?(x=x*1):x) is not appropriate as a substitute, so an error message similar to the one in the question appears.

As a direct correction, you can use parentheses as follows:

x>=0?(x=x*1):(x=x*-1);

Note: C operator priority - cppreference.com

Also, it has nothing to do with this question, but

  • scanf argument
  • ABSMacros have extra semicolons
  • If you use the name
  • ABS, you should focus only on returning absolute values and substitute outside

There are a few things that I am concerned about.


2022-09-30 21:44

In fact, this ABS() passes through the compilation in . and , the operator merge rule (which one should be handled first when the compiler sees it) is
https://ja.cppreference.com/w/c/language/operator_precedence
According to this, the trinomial operator ?: is interpreted before substitution, so the ABS(x); is

(x>=0?x=x*1:x)=x*-1;;

is interpreted as .I'm getting in trouble because the left side of the assignment operator is not a variable.

, starting with https://ja.cppreference.com/w/cpp/language/operator_precedenceNote 2 to

x>=0?(x=x*1):x=x*-1;;

So it can be compiled and works as expected.

In , the answer to this question is out of line, so you should skip the details, but avoid the #define macro as much as possible.This simple-looking ABS() macro has several problems when trying to use it for practical use.Readers should also count how many problems (likely)You should be able to point out at least three.

The way to fix it is likely to be #define in terms of oil.Other people's responses also failed to "use it safely at the practical level" (QAC and PgRelief analysis tools point out quickly).


2022-09-30 21:44

  • I understand that you are studying, so it is not a direct answer, but what you can write in the trinomial operation (condition?expr1:expr2) is an expression.If you want to write an assignment, make it a if statement.※ It's a correction. It's a substitute ceremony.I understood it from someone else's answer.

  • Because macros with
  • arguments are similar to functions, it is uncomfortable for ABS(x) to rewrite the value of x.
    Macros should be represented and substituted when using them.

I understand that you are studying, so it is not a direct answer, but what you can write in the trinomial operation (conditions?expr1:expr2) is an expression.If you want to write an assignment, make it a if statement.※ It's a correction. It's a substitute ceremony.I understood it from someone else's answer.

Macros with arguments are similar to functions, so it is uncomfortable for ABS(x) to rewrite the value of x.
Macros should be represented and substituted when using them.

 x = ABC(x);

When I compiled it, I found a fatal warning message.q q3.c is the source file name

 q3.c:8:17:warning:format specifications type 'int *'but the argument has type 'int' [-Wformat]
        scanf("%d%d", x, y);
               ~~      ^
q3.c: 8:20: warning: format specifications type 'int*' but the argument has type 'int' [-Wformat]
        scanf("%d%d", x, y);
                  ~~      ^


2022-09-30 21:44

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.