Why we are avoid Magic Numbers or Constants from Program and use macro? [closed]-Collection of common programming errors

When compiling a C program, the program is first processed by the C preprocessor. It process all the #foo stuff in your program and replaces macros. By the time the actual compiler sees the code, your macros have been replaced with values.

So if you feed your second code snippet to the compiler, the actual C compiler will see the exact same thing as in your first code snippet. This means that using a macro instead of a value does not slow down your program.

The idea about using macros (or constants) is to have names for your numbers that may make sense to a human.

Consider your program supports three bicycles (or whatever, doesn’t matter). You would need to write 3 in a lot of places (like you wrote 255 in your for loop). But a month later, you might not remember the meaning of a 3 any more when you see it. If you instead have:

#define MAX_BICYCLES 3

and then use MAX_BICYCLES everywhere the intention becomes clearer. Better yet, once your app should support, say, 5 bicycles it might be as easy as changing that one macro. You do not have to remember in which places to change a 3 to 5.