Live data from Hacker News

How to properly use macros in C

pmihaylov.com

21–30 of 52 posts

Re: How to properly use macros in C

#21
> macros can’t be debugged. When you use a function, that function can be stepped through by the debugger. The macro cannot.

Looks like the debugger could use some improvement.

> A macro is faster than a function.

This is false. Macros may be faster in some cases, but I've seen them being slower. I suspect this is because functions have more type information that can help the optimiser. I've tried it with Monocypher: replacing serialisation/de-serialisation functions (load and store) by equivalent macros slowed the whole thing down.

Re: How to properly use macros in C

#22
post #8

If you're going to write a long lecture to people, it's helpful to do some Googling to fact-check your own understanding. The claim that macros do not exist in other languages is incorrect. Every sensible assembly language compiler has macros to help the programmer with boilerplate. The claim that multiline macros are a problem is correct. Suggesting that a workaround is to use a naming convention for such macros is…

The goal of this article is not to provide some genuine insight to the topic, but to serve as a publicity stunt for the largest code academy in Bulgaria, dubiously called "Software University". On numerous other occasions their lecturers and trainers have made ridiculously misleading claims about trivial programming stuff. One of their lecturers insisted that the second return in "return; return;" was probably needed…

>One of their lecturers insisted that the second return in "return; return;"

That guy has a great career ahead in comedy!

Re: How to properly use macros in C

#23
post #8

If you're going to write a long lecture to people, it's helpful to do some Googling to fact-check your own understanding. The claim that macros do not exist in other languages is incorrect. Every sensible assembly language compiler has macros to help the programmer with boilerplate. The claim that multiline macros are a problem is correct. Suggesting that a workaround is to use a naming convention for such macros is…

The goal of this article is not to provide some genuine insight to the topic, but to serve as a publicity stunt for the largest code academy in Bulgaria, dubiously called "Software University". On numerous other occasions their lecturers and trainers have made ridiculously misleading claims about trivial programming stuff. One of their lecturers insisted that the second return in "return; return;" was probably needed…

> just in case the first one did not work

I repeat all statements in my code for exactly this reason. Sometimes I even compile the same code twice. You can never be too careful.

Re: How to properly use macros in C

#25
post #8

If you're going to write a long lecture to people, it's helpful to do some Googling to fact-check your own understanding. The claim that macros do not exist in other languages is incorrect. Every sensible assembly language compiler has macros to help the programmer with boilerplate. The claim that multiline macros are a problem is correct. Suggesting that a workaround is to use a naming convention for such macros is…

The goal of this article is not to provide some genuine insight to the topic, but to serve as a publicity stunt for the largest code academy in Bulgaria, dubiously called "Software University". On numerous other occasions their lecturers and trainers have made ridiculously misleading claims about trivial programming stuff. One of their lecturers insisted that the second return in "return; return;" was probably needed…

Well, I'm sorry, but I agree with the lecturer. I've run into this problem myself.

In my case I had some cleanup code to run at the end of the function in an error case, so I used "goto fail;" instead of "return;". And then just to be sure it always worked, I put in the "goto fail;" twice!

Re: How to properly use macros in C

#27
post #13

This isn't wrong but I've got issues with some of the examples, especially the lack of a solution for #2 and #4. #2 is referred to as an unsafe macro because of the multiple evaluation of its arguments. In some cases this is fine but it may need to be documented that the macro is unsafe for future users. It is also entirely possible to convert these macros into safe ones if you're willing/able to use GNU C extensions…

> #2 is referred to as an unsafe macro because of the multiple evaluation of its arguments. In some cases this is fine but it may need to be documented that the macro is unsafe for future users. In the early days of C programming, a naming convention was adopted to warn about this. If you wrote a macro that might have multiple evaluation of its arguments, you named it in UPPERCASE as a warning: #define MIN( a, b ) (…

Although I get that the original intent was different. I actually find making all constants uppercase a useful practice. When reading the code, it's easy to see what is a local variable and what is not, or at least not supposed to be.

Re: How to properly use macros in C

#28

Earlier quoted context omitted.

The goal of this article is not to provide some genuine insight to the topic, but to serve as a publicity stunt for the largest code academy in Bulgaria, dubiously called "Software University". On numerous other occasions their lecturers and trainers have made ridiculously misleading claims about trivial programming stuff. One of their lecturers insisted that the second return in "return; return;" was probably needed…

Well, I'm sorry, but I agree with the lecturer. I've run into this problem myself. In my case I had some cleanup code to run at the end of the function in an error case, so I used "goto fail;" instead of "return;". And then just to be sure it always worked, I put in the "goto fail;" twice!

Spotted the Apple developer!

Re: How to properly use macros in C

#29

> macros can’t be debugged. When you use a function, that function can be stepped through by the debugger. The macro cannot. Looks like the debugger could use some improvement. > A macro is faster than a function. This is false. Macros may be faster in some cases, but I've seen them being slower . I suspect this is because functions have more type information that can help the optimiser. I've tried it with Monocypher…

[deleted]

Re: How to properly use macros in C

#30
> Some compilers offer debug strings in macros, which cannot be used in functions: __FILE__, __LINE__, __func__.

But... Of course they can. They are just symbols defined by the compiler, and have nothing to do with being in a macro expansion or not. Heck, they are probably implemented as macro's themselves inside the preprocessor.

Post reply on HN