Best Answer

1.type checking in const that is not a part of #define. 2.scope 3.debugging is possible with const.

const variable can be localized whereas #define variable cannot be done so.

#define simply replaces whatever you have defined by the text you want it to replace.

const variable's value cannot be manipulated during the course of the program.

#define is a text preprocessor command and like all text preprocessor commands (beginning with "#") are handled by textual substitution throughout the code before the compiler sees any of the code.

const is a compiler keyword that identifies a constant declaration. It is handled by the actual compiler.

User Avatar

Wiki User

โˆ™ 2015-07-14 15:59:00
This answer is:
User Avatar
Study guides


20 cards

What are the brain's association areas

What is a field hockey stick made of

How old is she is rebecca stevenson

When during pregnancy should one quit smoking

See all cards


20 cards

What are the brain's association areas

What is a field hockey stick made of

How old is she is rebecca stevenson

When during pregnancy should one quit smoking

See all cards


20 cards

What are the brain's association areas

What is a field hockey stick made of

How old is she is rebecca stevenson

When during pregnancy should one quit smoking

See all cards

Add your answer:

Earn +20 pts
Q: What is the difference in use of define and const variable?
Write your answer...
Related questions

How can we replace the value of const variable using scanf statement?

You cannot change the value of a const variable. That's what const means - it is constant. If you are considering "trickery" using pointers and the scanf function, understand that this is not supported, is highly non portable, and may fail, depending on whether or not the implementation places its const data in read only memory. Besides, most modern compilers will not allow you to place a const variable as a non-const argument to a function. Use the language within its defined boundaries.

You can use a variable to declare an array's size true or false?

True and false in the same time, because even so you can declare array size using notation for variables you have use constwhich makes your variable basically a constant:const int arraySize = 10;In Java, you can use any expression to define the array size, when you create the array. Once you create an Array object, however, you can't redimension it - but you can create a new Array object and destroy the old one.

What is the purpose if declaring object with keyword const in c plus plus?

If you need a variable which cannot be changed then you use const with it, for instance, number of months in the year, number of hours in the day and so on.

Why and when do you use the define directive?

The #define preprocessor directive is severely overused in my opinion. Often, you will see programmers write something like this: # define MAX(a, b) (((a) > (b))? (a) : (b)) However doing so is rather dangerous, because the preprocessor replaces things textually. This means that if you pass in a call to a function, it may happen twice, for example: MAX(++i, j) will be expanded to (((++i) > (j))? (++i) : (j)) which is bad. A much safer (and more common) example is that people will use #define to create a constant variable: #define MYCONST 10 This too can cause problems if another file depends on the constant and certain other conditions are met. A safer alternative is to declare a const variable. The one advantage of using #define for literal constants is that the number will not be stored in memory attached to an object like a const variable will.

What is a ponters in c-programming?

with the help of pointers we able to store the memory location of any variable. In c the pointer variable is use to store the memory location of any variable. The pointer variable is define as a simple variable but in pointer variable use a special "*" character at the left most side of name of pointer variable. If any variable name have * it means it is a pointer variable it hold the memory location of variable.

How do you set global variables to on?

You can use the preprocessor directive #define, or you can describe a variable in the body of main(). With the preprocessor directive you can make the variable accessible even out of your current project.

How do you define a symbol such as plus or - as a variable in python?

Use a character variable. For example: plus = '+' minus = '-' You can now refer to these symbols using the variable names "plus" or "minus".

How do you use define in C?

Just type declare then the variable that you desire to assigned a certain constant value on it. Just type declare then the variable that you desire to assigned a certain constant value on it.

Explain the scope and visibility of variables in c plus plus function?

If you define a variable inside of your function, the variable can be referred and used only inside of that function. It means that you will not able to use the variable in another function (including main). Area of code where your variable can be used after declaration is usually called visibility of the variable.

What is the difference between class and object in c plus plus?

There is no difference; to define an object in C++ you use the 'class' definition

Define drug use and drug abuse?

For me the difference is drug use You can control it Drug abuse is the drug controls you

What is the difference between an independent variable and a constant?

A constant is something that will ALWAYS remain the same in your experiment. For instance, the materials you use and the unit of measurements you use are examples of constants. An independent variable changes with the experiments.

Define When a cell use energy to move material across cell membrane?

if it is against a concentration difference.

What is the difference between auto storage class and register storage class?

keyword register can be used for parameters and local variables, it means two things:- it is often used variable (parameter), so the compiler might want to use a CPU-register to optimize its using.- the programmer swears that he won't ever use the address of that variable (parameter)example:size_t strlen (register const char *s){register const char *q= s-1;while (*++q);return q-s;}

What is const pointer?

In computer science, const-correctness is the form of program correctness that deals with the proper declaration of objects as mutable or immutable. The term is mostly used in a C or C++ context, and takes its name from the const keyword in those languages. The idea of const-ness does not imply that the variable as it is stored in the computer's memory is unwriteable. Rather, const-ness is a compile-time construct that indicates what a programmer may do, not necessarily what he or she can do. In addition, a class method can be declared as const, indicating that calling that method does not change the object. Such const methods can only call other const methods but cannot assign member variables. (In C++, a member variable can be declared as mutable, indicating that a const method can change its value. Mutable member variables can be used for caching and reference counting, where the logical meaning of the object is unchanged, but the object is not physically constant since its bitwise representation may change.) In C++, all data types, including those defined by the user, can be declared const, and all objects should be unless they need to be modified. Such proactive use of const makes values "easier to understand, track, and reason about," and thus, it increases the readability and comprehensibility of code and makes working in teams and maintaining code simpler because it communicates something about a value's intended use. For simple data types, applying the const qualifier is straightforward. It can go on either side of the type for historical reasons (that is, const char foo = 'a'; is equivalent to char const foo = 'a';). On some implementations, using const on both sides of the type (for instance, const char const) generates a warning but not an error. For pointer and reference types, the syntax is slightly more subtle. A pointer object can be declared as a const pointer or a pointer to a const object (or both). A const pointer cannot be reassigned to point to a different object from the one it is initially assigned, but it can be used to modify the object that it points to (called the "pointee"). (Reference variables are thus an alternate syntax for const pointers.) A pointer to a const object, on the other hand, can be reassigned to point to another object of the same type or of a convertible type, but it cannot be used to modify any object. A const pointer to a const object can also be declared and can neither be used to modify the pointee nor be reassigned to point to another object. The following code illustrates these subtleties: void Foo( int * ptr, int const * ptrToConst, int * const constPtr, int const * const constPtrToConst ) { *ptr = 0; // OK: modifies the pointee ptr = 0; // OK: modifies the pointer *ptrToConst = 0; // Error! Cannot modify the pointee ptrToConst = 0; // OK: modifies the pointer *constPtr = 0; // OK: modifies the pointee constPtr = 0; // Error! Cannot modify the pointer *constPtrToConst = 0; // Error! Cannot modify the pointee constPtrToConst = 0; // Error! Cannot modify the pointer To render the syntax for pointers more comprehensible, a rule of thumb is to read the declaration from right to left. Thus, everything before the star can be identified as the pointee type and everything to after are the pointer properties. (For instance, in our example above, constPtrToConst can be read as a const pointer that refers to a const int.) References follow similar rules. A declaration of a const reference is redundant since references can never be made to refer to another object: int i = 42; int const & refToConst = i; // OK int & const constRef = i; // Error the "const" is redundant Even more complicated declarations can result when using multidimensional arrays and references (or pointers) to pointers. Generally speaking, these should be avoided or replaced with higher level structures because they are confusing and prone to error.

Define the Law of Diminishing Returns?

If there are fixed units, increased use of variable input will continue to increase production at a decreasing rate because of congested us of the fixed inputs.

What is the difference between macro and define?

A macro is simply a text-substitution mechanism. You use the #define directive to define both macro symbols and precompiler directives. Every occurrence of a macro symbol within your code is substituted with the macro definition during preprocessing.

Logger function definition in c-language?

1. You can define whatever logger function you want.2. In you unix you can use functionvoid syslog (int priority, const char *message, ...);

What is the difference between declaring a variable and defining a variable?

Declaring a variable instantiates the variable but does not initialise it with a value, whereas defining a variable initialises it with a known value. Whenever we instantiate a variable, memory is set aside for it and we can refer to that memory using the variable's name. However, the memory is left in an uninitialised state -- whatever state that memory had at the time the variable was instantiated will remain the same. We say the value is undefined because if we attempt to use whatever value happens to reside there our program will exhibit undefined behaviour. Once we define a variable, we can safely use it because its value will then be initialised. Although we should always define a variable before we use it, it can sometimes be beneficial to instantiate the variable first and perform the initialisation later, so long as we do not use the variable before that initialisation occurs. For instance, if we want to copy an array from one location in memory to another, we first need to instantiate the memory to hold the copy, but it is not necessary to initialise that memory because we're about to overwrite that memory with values from the original array. Thus we declare the array first and then the copy operation performs the initialisation, thus defining the copy. Once that is done we can safely use the copy. It should be noted that whenever we declare an instance of a class (an object) that has one or more constructors, whichever constructor is invoked will initialise the object's data members. Thus an object of this type can always be said to be defined at the point of instantiation. This is of particular importance where the object has a natural default value. For instance, a string object will default to an empty string, thus the string's default constructor will initialise the memory accordingly. For programmers who are used to using two-stage initialisations (declare first, define later), it can come as a surprise to learn that they are in fact initialising their objects twice -- which is far from efficient. Therefore whenever you declare an object, always initialise at the point of declaration unless the default initialisation (if one is provided) is exactly what you want.

How will you defined variavble in vb script?

To define a variable in VBScript, you use the keyword "Dim" followed by the name of the variable. You can name the variable any string so long as itBegins with a letterDoes not contain a period (.)Is not longer than 255 charactersSo "tom" is a valid variable name, but "tom.d" is not.To assign value to the variable, you use the assignment operator (=)Dim coolWebSite = ""The variable "coolWebSite" will now contain the string ""

Why is x always be a variable?

It is very common to use, but it is not the only variable you can use.

In PHP you can use both single quotes ( ' ' ) and double quotes ( ) for strings?

You can use single quotes $variable = 'string'; You can use double quotes $variable = "string"; You can use the Heredoc method $variable = <<<EOT Very Long String EOT; And you can use the Nowdoc method (after PHP 5.3.0) $variable = <<<'EOT' Very Long String EOT;

How are global variable different from local variable in Visual BASIC?

A global variable is available for use throughout the program. It is normally declared outside any procedure. A local variable is only available for use in the procedure it was declared in. Most variables are local, but there will be occasions when you want different procedures to be able to use the same variable. This is when you use a global variable.

1 JavaScript statements use a single equal sign for what operation?

Single equal signs (=) are used to define a variable. So a = 1 sets a as 1. are used to compare two values.

How do you use manipulated variable in a sentence?

My manipulated variable was affected by the independent variable.