answersLogoWhite

0


Best Answer

The preprocessor #define directive creates a macro, not a constant. While you can define a literal constant, and call it a constant, this is not the same as an actual constant. Constants (those defined with the const keyword) are really no different to variables; they have a size and type, they can be referenced, pointed at, and they have scope. In fact they only differ from variables in that you cannot alter their value once instantiated (the very definition of a constant). Macros have none of these properties, they are nothing more than text-replacements. And since they are inline expanded before compilation, the compiler never sees them, so you not only lose type-safety and scope, you also lose the help of the debugger. And on top of all that you also cannot point nor refer to a macro, nor can a macro refer to an object. You can only #define literal values, string literals and functions.

Whenever you have the option of using const or #define, go with const every time. It is type-safe, thus you automatically enlist the compiler to help spot any problems that would otherwise be invisible to you via a macro, and therefore difficult to track down. The #define directive should only ever be used when there is no suitable alternative within the C++ language itself, or when a macro helps resolves a complication that would be difficult to accomplish any other way. Keep in mind that macros are not actually part of the language, they are nothing more than simple text-replacements, akin to an automated copy/paste if you like. But when used correctly, such as when assembling top-level constructs from many pieces of boilerplate, they can greatly simplify your code. Just remember that the debugger can't help you debug a macro -- you're completely on your own.

User Avatar

Wiki User

10y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

6y ago

Preprocessor directives (macros) are not type safe. Always express ideas in code, never use a macro in place of a type safe language construct (like const). Remember that the compiler never sees the macro it only sees the code generated from the macro by the preprocessor; the macro itself is stripped out of the source along with user-comments because the compiler cannot interpret symbols that aren't part of the language specification. Macros are intended to modify source code in ways that would be difficult or impossible to achieve with the language specification alone. In C++ there are very few reasons to prefer a macro over a language feature.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How does constant defined by const differ from the constant defined by the preprocessor statement?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

How do the IO facilities in C plus plus differ from those in C?

The C standard library IO facilities are not extensible. For instance, the printf() and scanf() functions cannot handle user-defined types. However, the C++ standard library provides IO streams with insertion and extraction operators (<< and >>) that can be overloaded to support any user-defined type.


How does single phase alternator differ form a dc ganerator?

Single phase alters from positive to negative at 50 (50hz) or 60 (60hz) times per minute. While in dc it does not vary but stays at a constant positive phase.


What are compiler directives?

Compiler directives instruct the preprocessor to perform certain tasks prior to compiling the source code. The language compiler can only compile C code, but macros (lines beginning with a # symbol) are not part of the C language, so these need to be processed before the compiler can do its job. The most common compiler directive is the #include directive, which instructs the preprocessor to import the named file, effectively copy/pasting the content of that file in place of the directive. The content may itself contain directives and these must also be processed accordingly before the content is inserted. Note that the insertion takes place in a temporary file; the source file is not changed by the preprocessor. The temporary file is usually deleted after compilation is complete, however you can configure the compiler to retain these files so you can see what code is produced by the preprocessor and thus what the compiler "sees". The #define and #undefine compiler directives define or undefine macros; symbols which the preprocessor can refer to while processing other directives. Macros can also be defined via the command line. The #ifdef, #ifndef, #elif, #else and #endif compiler directives are used in conditional compilation in conjunction with macro definitions. These tell the preprocessor which parts of the source code to include in or exclude from the temporary file. This is useful when writing debug code that we wish to include in a debug build but exclude from a retail build, or we wish to cater for different architectures and require different libraries and function calls in order to cater for them. A macro symbol can also be given a value. Wherever that symbol appears in the code, the symbol is replaced with the value. This provides a convenient text-replacement system that can help make code easier to read. However, a macro can also be used to define a function (a macro function). These differ from ordinary functions in that they are always inline expanded, however they are not type-safe and the compiler cannot help you debug them (the compiler never sees the macro -- it only sees the inline expanded code produced by the macro). Macro functions are best avoided, however when used with care they can help to resolve complex problems that cannot easily be resolved within the language itself. It's best to think of macro functions as being code generators but, because they are not part of the language, its best to keep their usage as simple as possible. As well as processing compiler directives, the preprocessor also strips out all the user-comments from the source file. Even if a compiler could read a comment it certainly wouldn't understand it, so to keep the compiler implementation as simple and as efficient as possible, all non-C code is completely stripped out. Even the redundant whitespace and newline characters are stripped out. The end result is a compact source file which can be easily compiled -- assuming no errors have been generated by your macros!


How do methods and functions differ?

A method is simply a function that is defined for a class. To invoke it, you usually need to specify an object, or the class name, followed by a dot, followed by the method name.A method is simply a function that is defined for a class. To invoke it, you usually need to specify an object, or the class name, followed by a dot, followed by the method name.A method is simply a function that is defined for a class. To invoke it, you usually need to specify an object, or the class name, followed by a dot, followed by the method name.A method is simply a function that is defined for a class. To invoke it, you usually need to specify an object, or the class name, followed by a dot, followed by the method name.


What is the signature of a function in c plus plus?

A function's signature is defined by the number and type of parameters. Functions with the same signature cannot differ by return type alone. Use of the const keyword also constitutes part of the signature.

Related questions

How does liquid differ from a gas?

A liquid has no defined shape, but has a defined volume. A gas has no defined shape and no defined volume.


Explain with suitable example how switch statement is differ from if statement?

www.assignmentsclub.com


The principle that two stimuli must differ by a constant proportion for their difference to be perceived is known as?

Weber's Law"Ernst Weber noted that for people to really perceive a difference, the stimuli must differ by a constant "proportion" not a constant "amount."


What is character constant in c how does it differ from numeric type constants?

hiii


When the volume is constant how do the masses of 2 objects differ?

When volume is constant, the masses of two objects are in direct proportion to their densities.


How does write statement differ from writeln statement?

I think you mean written statement. What you are asking about write statement you are confusing with written statement, the same with write in statement. A written statement is simply putting your words, or your version of events in writing.


How did colonial governments differ from the british government?

colonists' rights were defined by formal documents. british rights were defined by laws and tradition


How did colonial government differ from the british government?

colonists' rights were defined by formal documents. british rights were defined by laws and tradition


What is the purpose of for statement differ from while statement?

conditin are given in for statement they check one time either it true or false and in while statement they check again and again untell the condition false.


What size should a file be?

Files have no defined size. They all differ based on what is within them.


How does an impulse differ from a stimulus?

a stimulus may be continous, an impulse ha a defined timeline


What was one way that colonial government differ from the British governments?

Colonists' rights were defined by formal documents. British rights were defined by laws and tradition.