Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why would someone use #define to define constants?

People also ask

How do you know you are being used?

The person imposes on you without consideration for your availability or preferences. For instance, they may move in with you unexpectedly or want to borrow your car at a moment's notice. The person expects you to take care of their needs.

Why do people allow others to use them?

Most people who enable loved ones don't intend to cause harm. In fact, enabling generally begins with the desire to help. Enabling behaviors can often seem like helping behaviors. You may try to help with the best of intentions and enable someone without realizing it.

Why people use you Quora?

Makes them feel powerful that it makes them feel like a king or queen and so superior. because feel can put me down and control me . at time i didn't realise but I am more aware and trust less now I don't believe everyone used me but enough have.

When someone uses you for their own benefit?

exploiter Add to list Share. An exploiter is a user, someone who takes advantage of other people or things for their own gain. Being an exploiter is selfish and unethical. To exploit someone is to use them in a way that's wrong, like an employer who pays low wages but demands long hours.


#define has many different applications, but your question seems to be about one specific application: defining named constants.

In C++ there's rarely a reason to use #define to define named constants.

#define is normally widely used in C code, since C language is significantly different from C++ when it comes to defining constants. In short, const int objects are not constants in C, which means that in C the primary way to define a true constant is to use #define. (Also, for int constants one can use enums).


No one should not!
Actually, One should prefer const int sum = 1; over #define sum 1 for a number of reasons:

Scope Based Mechanism:

#defines don't respect scopes so there is no way to create a class scoped namespace. While const variables can be scoped in classes.


Avoiding Weird magical numbers during compilation errors:

If you are using #define those are replaced by the pre-processor at time of precompilation So if you receive an error during compilation, it will be confusing because the error message wont refer the macro name but the value and it will appear a sudden value, and one would waste lot of time tracking it down in code.


Ease of Debugging:

Also for same reasons mentioned in #2, while debugging #define would provide no help really.


Thus, to avoid the above situations const will be a better choice.


For the example that you just gave, I would normally use a const. Except of course, the #define can be used for conditional compilation elsewhere:

#if SOME_DEFINE == 1
    // Conditional code
#endif

This is something you can't do with a const. If you don't need the value to be accessible from the preprocessor, I'd say use a const unless there's some reason why that's not possible. There's some stuff on this in the C++ FAQ lite, where they rightly point out that just because the preprocessor is "evil", it doesn't mean you'll never need it.


#define is necessary to make things like inclusion guards work, because C++ doesn't have a real module import system.

#define causes a literal textual substitution. The preprocessor understands how to tokenize source code, but doesn't have any idea what any of it actually means. When you write #define sum 1, the preprocessor goes over your code and looks for every instance of the token sum and replaces it with the token 1.

This has a variety of limitations: #define sq(x) x * x will not work right if you use it like sq(3+3); and using #define for a constant does not respect scope in any way, nor does it associate any kind of type with the constant. However, #define can be used (especially in combination with some other special stuff like the # and ## preprocessor operators) to do some magic that is otherwise not possible (except by manually doing what the preprocessor does).


Always try to use "const int", rather than #define.

Use #define, only when your preprocessor code might be read by another tool, and it's easier for it to go with the preprocessor, rather than to parse the language.

Also it's the only way to define something to be checked later by #if/#else/#endif


In simple words

#define sum 1  /*is pre processed*/

Which means, that sum doesn't exist after the preprocessing stage is finished.

const int sum = 1; /*is compiled/linked*/

Which means sum exists till the executable is made out of your program.