Arithmetic Expressions as argument to macro in c

Himanshu Sourav

using macro with preprocessor directive #define I have written the following two codes.

  • first code passes the expression e.g. a+b

_valid_pagesize(a+b)

  • in second code

c=a+b

and then passed this c as an argument to macro.

_valid_pagesize(c)

the second code runs perfectly whereas the first code doesn't. can we not pass expressions as arguments to macro like the way we can in case of a function..? why not..?

the code passing expression as argument :

#include<stdio.h>
#include<stdlib.h>

#define _valid_pagesize(_newsize) (!(_newsize % 0x80000000)?1:      \
        (!(_newsize % 0x40000000)?1:      \
        (!(_newsize % 0x10000000)?1:      \
        (!(_newsize % 0x4000000) ?1:0))))


int main(int argc, char* argv[]){
    uint64_t size[2];
    size[0]=atoi(argv[1]);
    size[1]=atoi(argv[2]);

    if(_valid_pagesize(size[0]+size[1])){
            printf("success\n");
            }
    return 0;
}
user3386109

Rule #1 with macros is to enclose all arguments in parentheses, to avoid exactly the problem that you're seeing.

#define _valid_pagesize(_newsize) (!((_newsize) % 0x80000000)?1:      \
    (!((_newsize) % 0x40000000)?1:      \
    (!((_newsize) % 0x10000000)?1:      \
    (!((_newsize) % 0x4000000) ?1:0))))

When you pass a+b to the macro, it expands to

a + b % 0x40000000

and since % has higher precedence than +, you don't get the result you expect. By enclosing the argument in parentheses, the macro expands to

(a + b) % 0x40000000

which works as expected.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Arithmetic Expressions as argument to macro in c

From Dev

Perform arithmetic within c macro

From Dev

Extract Argument from C Macro

From Dev

Passing NULL as argument to a C macro

From Dev

Pass a token with "::" as argument to C/C++ macro

From Dev

How to pass a macro as an argument in a C function?

From Dev

How to append to the name of an argument in C macro

From Dev

C++ Addition within a Macro Function Argument

From Dev

Token detection within a C preprocessor macro argument

From Dev

Prepend argument to a list and apply another macro in C preprocessor macro

From Dev

Is there a one-macro way to prefix and quote C macro argument

From Dev

C - Variadic macro which expands into set of macro calls on each argument

From Dev

C - Variadic macro which expands into set of macro calls on each argument

From Dev

Arithmetic expressions in for-loop

From Dev

Get different parts of a macro argument in C/C++ preprocessor

From Dev

Safely evaluating arithmetic expressions in R?

From Dev

How to use arithmetic expressions in the terminal?

From Dev

how to pass block as a macro's argument in objective-c?

From Dev

how to pass block as a macro's argument in objective-c?

From Dev

how to wrap a C++ argument-less macro with SWIG?

From Dev

Prepend each variadic macro argument with a name of a function parameter in C++

From Dev

Check if a macro argument is a pointer or not

From Dev

Macro Expansion: Argument with Commas

From Dev

Stringizing argument of macro to be unicode

From Dev

Assembly macro check argument

From Dev

Distributing an argument in a variadic macro

From Dev

Clojure evaluate argument of macro

From Dev

Assembly macro check argument

From Dev

How to use arithmetic operators with SAS macro variables

Related Related

  1. 1

    Arithmetic Expressions as argument to macro in c

  2. 2

    Perform arithmetic within c macro

  3. 3

    Extract Argument from C Macro

  4. 4

    Passing NULL as argument to a C macro

  5. 5

    Pass a token with "::" as argument to C/C++ macro

  6. 6

    How to pass a macro as an argument in a C function?

  7. 7

    How to append to the name of an argument in C macro

  8. 8

    C++ Addition within a Macro Function Argument

  9. 9

    Token detection within a C preprocessor macro argument

  10. 10

    Prepend argument to a list and apply another macro in C preprocessor macro

  11. 11

    Is there a one-macro way to prefix and quote C macro argument

  12. 12

    C - Variadic macro which expands into set of macro calls on each argument

  13. 13

    C - Variadic macro which expands into set of macro calls on each argument

  14. 14

    Arithmetic expressions in for-loop

  15. 15

    Get different parts of a macro argument in C/C++ preprocessor

  16. 16

    Safely evaluating arithmetic expressions in R?

  17. 17

    How to use arithmetic expressions in the terminal?

  18. 18

    how to pass block as a macro's argument in objective-c?

  19. 19

    how to pass block as a macro's argument in objective-c?

  20. 20

    how to wrap a C++ argument-less macro with SWIG?

  21. 21

    Prepend each variadic macro argument with a name of a function parameter in C++

  22. 22

    Check if a macro argument is a pointer or not

  23. 23

    Macro Expansion: Argument with Commas

  24. 24

    Stringizing argument of macro to be unicode

  25. 25

    Assembly macro check argument

  26. 26

    Distributing an argument in a variadic macro

  27. 27

    Clojure evaluate argument of macro

  28. 28

    Assembly macro check argument

  29. 29

    How to use arithmetic operators with SAS macro variables

HotTag

Archive