0

I'm facing an issue while using macros in C.

I have a macro (this macro is just a macro I've create to reproduce the issue, please do not comment the fact that this macro is useless) :

#define my_macro(x) \
({ \                                                 // This is line 5
    typeof(x) _x = x; \
    typeof(x) res = x; \
\
    if (_x < 0) \
        _x = _x * (-1); \
    for (typeof(x) _i = 0; _i < _x; _i++) \
        res = res * x; \
    res; \
})

That I use in my program as so :

int main (int argc, char **argv) 
{
    printf("%d\n", my_macro(atoi(argv[1])));         // This is line 19
    return (0);
}

When I compile my program using gcc as so : gcc test.c -Wpedantic, this leads to a warning :

warning: ISO C forbids braced-groups within expressions [-Wpedantic]
    5 |     ({ \
      |     ^
note: in expansion of macro main_test’
   19 |     printf("%d\n", my_macro(atoi(argv[1])));
      |                    ^~~~~~~~~

In comments of this post they talk about adding __extension__ to remove the warning. But even if the warning is removed, this does not make the macro ISO Compliant I think.

I've also found here that using a do {} while (0); could fix the warn (I did not try that myself since it's not the solution I would like to use). But I feel like this is more a bypass than a real answer.

So I'm wondering how I could fix this. If the answer to use __extension__ and am missing information and I would really like to have your explanations on this. Also I did not manage to find explanations on what exactly are braced-groups. If anyone could please explain them in comments I would be happy to read this and understand my mistake.

Overall any help would be appreciate, Thanks by advance

Dzious
  • 169
  • 2
  • 14
  • [What a braced group is.](https://stackoverflow.com/questions/1238016) – Robert Harvey Jan 24 '21 at 19:59
  • So what prevents you from using an ordinary function instead of a macro? – Robert Harvey Jan 24 '21 at 20:00
  • Can you wrap your braced group in a function? – Robert Harvey Jan 24 '21 at 20:02
  • @RobertHarvey the macros I use in my project use typeof() which is, to my known, not usable outside macros. Also macros i use are part of a generic linked_list library i've created. if you want to have a look, here is the link : https://github.com/Guerout-Arnaud/C_Lib/tree/Linked_List – Dzious Jan 24 '21 at 20:10
  • 1
    What you're trying to do is not possible in standard C. GCC added braced groups as an extension to specifically enable this type of programming. – Barmar Jan 24 '21 at 20:14
  • `typeof(x)` is not in ISO C either – M.M Jan 24 '21 at 20:18
  • 1
    `do..while(0)` is no substitute as it cannot "return" a value, whereas the GCC statement-expression can be used where a value is required. The latter is basically an inline function definition with automatically deduced return type – M.M Jan 24 '21 at 20:19
  • In addition, [typeof is specific to GCC](https://stackoverflow.com/a/12082050/102937) (it is a non-standard compiler extension), so I'm inclined to assume that you're going to be using a GCC compiler and this doesn't matter. – Robert Harvey Jan 24 '21 at 20:20
  • Alright @Barmar Then this means that the `__extension__` "trick" is the only way to solve this problem ? (by reading a bit more at [these explanations](https://developer.ibm.com/tutorials/l-gcc-hacks/) from [this post](https://stackoverflow.com/a/18885626/10898093) the braced-group seems to be a kernel/gcc "hack" – Dzious Jan 24 '21 at 20:20
  • 1
    I don't think it's an actual problem. The warning is there to let you know that it's non-standard C, but since you're already using `typeof()`, it's already non-standard C. – Robert Harvey Jan 24 '21 at 20:22
  • `__extension__` is just to suppress the warning about using a GCC extension, it overrides `-Wpedantic`. – Barmar Jan 24 '21 at 20:22
  • It's a GCC extension that was created because the kernel programmers wanted to write macros like this. – Barmar Jan 24 '21 at 20:22
  • Alright, I've got it now and i'll try to use `__extension__`. Huge thanks to all of you who helped me understand this. :D – Dzious Jan 24 '21 at 20:24
  • If it's any consolation, there's proposals in flight for C23 that attempt to provide standard equivalents to these GCC extensions. – StoryTeller - Unslander Monica Jan 24 '21 at 20:30

0 Answers0