Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

#define vs enum in an embedded environment (How do they compile?)

This question has been done to death, and I would agree that enums are the way to go. However, I am curious as to how enums compile in the final code- #defines are just string replacements, but do enums add anything to the compiled binary? Or are they both equivalent at that stage. When writing firmware and memory is very limited, is there any advantage, no matter how small, to using #defines?

Thanks!

EDIT: As requested by the comment below, by embedded, I mean a digital camera.

Thanks for the answers! I am all for enums!

like image 468
Alexander Kondratskiy Avatar asked Jan 09 '11 06:01

Alexander Kondratskiy


1 Answers

Both are constant expressions in the terminology of the standard, so they "should" be evaluated fully at compile-time by any sane compiler. It would take a maliciously pathological compiler to generate different code.

like image 137
R.. GitHub STOP HELPING ICE Avatar answered Oct 12 '22 07:10

R.. GitHub STOP HELPING ICE