answersLogoWhite

0


Best Answer

The main two I can think of:

  1. It gives more meaning to the constant. eg using PI instead of 3.14159 makes it easier to understand what's going on when calculating things like a = PI * r * r;
  2. Should the constant for the program need to be changed, only one occurrence needs to be changed (in a header file) instead of all usages.
User Avatar

Wiki User

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

Wiki User

11y ago

Symbolic constants offer two benefits. The first benefit is that it is easier to remember the symbolic constant than its underlying value (such as remembering Math.PI instead of 3.14159). The second benefit is that if a value changes, you need only find the constant in its source. As an example, if you write an algorithm that operates on 256 bits (but can be variable), and you discover later that 512 bits would be optimal, you can simply change the value of the constant instead of looking for every instance of 256 and replacing it with 512 (where you might miss an instance of it, or even worse, change a 256 that was actually unrelated to the algorithm).

This answer is:
User Avatar

User Avatar

Wiki User

7y ago

When we use literal constants, we invariably use them repeatedly in our code. However, sometimes we need to change the value of a constant at design time, thus we must search all our code and replace each and every occurrence, which may lead to inconsistencies in our code if we miss any. We could use a search and replace utility, however we might use the same constant value for different purposes and may not wish to change every occurrence, so we'd need to manually inspect each replacement. This creates a maintenance burden that we don't get when we use symbolic constants.

By assigning literal constants to a symbolic constants, we can identify constants by name rather than by value, which (with well-chosen symbolic names) helps make our code more readable. And if we need to change the value of certain symbols, we can do so in just one place; all code that uses that symbol will be updated immediately, thus ensuring consistency is maintained.

This answer is:
User Avatar

User Avatar

Wiki User

9y ago

C has no concept of a constant variable so we must use symbolic constants instead. However, symbolic constants are not type safe:

#define PI 3.14

#define HI "Hello world"

void main() {

int pi = PI; // warning: conversion from double to int!

char c = HI; // error: string to character

return 0;

}

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What are the advantages of using a symbolic constant rather than a literal constant in c plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What are constants in computer programming?

A constant may refer to a literal constant, a constant variable. A literal constant is a value that we use to initialise or assign to a variable, literally, such as: int x {42}; // initialise x = 0; // assign Here, the values 42 and 0 are literal constants. A constant variable is a variable which will not change value after initialisation: const int x {42}; // initialise x = 69; // error - cannot assign to a constant variable Constant variables are useful when we wish to use the same constant value repeatedly within a scope. We could also use a literal constant rather than a constant variable, however naming our constants makes it easier to refer to the value consistently, particularly during code maintenance where we may wish to review the value. With a constant variable we need only change the initialiser, but with literal constants we must change every occurrence of the literal and that can lead to inconsistencies. Consider the following: int x[100]; int y[100]; for (int i=0; i<100; ++i) { cout << x[i] << endl; } // ... for (int i=0; i<100; ++i) { cout << y[i] << endl; } Here we've used the literal constant 100 four times. At a future time we may decide array x really needs 200 elements rather than 100, but we have to be careful which literals we change because array y makes use of that same literal constant. Using constant variables helps keep those usages separate: const int xmax {100}; const int ymax {100}; int x[xmax]; int y[ymax]; for (int i=0; i<xmax; ++i) { cout << x[i] << endl; } // ... for (int i=0; i<ymax; ++i) { cout << y[i] << endl; } Now we can safely change the xmax initialiser without affecting any usage of the ymax constant: const int xmax {200}; const int ymax {100}; int x[xmax]; int y[ymax]; for (int i=0; i<xmax; ++i) { cout << x[i] << endl; } // ... for (int i=0; i<ymax; ++i) { cout << y[i] << endl; } Note that a constant variable cannot be initialised by a non-constant variable: int x {42}; const int y {x}; // error - x is non-constant


Can you declare constructor as constant if yes why?

Constructors are implicitly constant members of a class. They do not modify objects, rather they initialize them. So constant and not constant objects can invoke them: const MyClass object; // invokes MyClass::MyClass() constructor


What are advantages of telecomunication in an organization?

It save people a lot of time rather then walking


What are symbolic constants and how they are useful in developing programs?

symbolic constants are named constants like : final double PI = 3.14 ; They are constants because of the 'final' keywords, so they canNOT be reassigned a new value after being declared as final And they are symbolic , because they have a name A NON symbolic constant is like the value of '2' in expression int foo = 2 * 3 Answer given By Sushil Mittal..........


What advantages does 2 by 6 studs have over 2 by 4 studs?

You can get more insulation when using 2x6 studs rather than 2x4 studs

Related questions

What is the meaning of constant variables?

Constant variables are variables that, once initialized, do not change in value. They are effectively the same as literal constants, except that you can use symbolic names to make it easier to understand the meaning of the program. For instance, in a trigonometry program, it is easier to use the identifier PI than it is to use the literal 3.1415926535897932. Also, if you have a constant that might be adjustable, such as a factor in an equation, naming it allows you to change its value by changing only one line of code, rather than seeking out all of the lines of code that refer to the factor. Constant variables are also candidates for being placed in read-only memory, potentially making the program more secure.


What advantage are there in defining an array size in term of symbolic constant rather than a fixed integer value?

It may be possible to generalise results to other integer values.


What does figuratively speaking mean?

Figuratively speaking refers to using language in a symbolic or metaphorical way, rather than in a literal sense. It is a way of expressing ideas or concepts indirectly for emphasis or imaginative effect.


Does the pillar of salt exist today?

The pillar of salt referred to in the Bible is a historical event, and there is no physical evidence to suggest that it still exists today. It is considered to be a symbolic story rather than a literal event.


Did leviathan really exist?

The Leviathan is a mythological sea creature mentioned in various religious texts and folklore. There is no scientific evidence to suggest that a real-life Leviathan exists. It is believed to be a symbolic representation of chaos or a powerful force rather than a literal creature.


Where do Protestants get that transubstantion is wrong?

Protestants believe that transubstantiation is not biblically supported and that the Eucharist is a symbolic remembrance rather than a literal transformation of bread and wine into the body and blood of Christ. They often interpret the Last Supper accounts symbolically rather than literally, leading to a different understanding of the sacrament.


Why is it important to keep all variables but one constant in a scientific experiment?

Because changing more than one variable will change the entire experiment. Changing one variable allows you to see how things could have turned out under different circumstances.If you keep all the variables different, you won't be able to determine what variable lead to which outcome. So just make sure you keep ONE variable different (the one you are testing) because this is crucial to your whole experiment.


The advantages of the literal rule?

follows parliament supremacy and makes sure all judges do is APPLY the law rather than make itintended to demand precision with drafting so that anyone that can read English can understand the law


What are constants in computer programming?

A constant may refer to a literal constant, a constant variable. A literal constant is a value that we use to initialise or assign to a variable, literally, such as: int x {42}; // initialise x = 0; // assign Here, the values 42 and 0 are literal constants. A constant variable is a variable which will not change value after initialisation: const int x {42}; // initialise x = 69; // error - cannot assign to a constant variable Constant variables are useful when we wish to use the same constant value repeatedly within a scope. We could also use a literal constant rather than a constant variable, however naming our constants makes it easier to refer to the value consistently, particularly during code maintenance where we may wish to review the value. With a constant variable we need only change the initialiser, but with literal constants we must change every occurrence of the literal and that can lead to inconsistencies. Consider the following: int x[100]; int y[100]; for (int i=0; i<100; ++i) { cout << x[i] << endl; } // ... for (int i=0; i<100; ++i) { cout << y[i] << endl; } Here we've used the literal constant 100 four times. At a future time we may decide array x really needs 200 elements rather than 100, but we have to be careful which literals we change because array y makes use of that same literal constant. Using constant variables helps keep those usages separate: const int xmax {100}; const int ymax {100}; int x[xmax]; int y[ymax]; for (int i=0; i<xmax; ++i) { cout << x[i] << endl; } // ... for (int i=0; i<ymax; ++i) { cout << y[i] << endl; } Now we can safely change the xmax initialiser without affecting any usage of the ymax constant: const int xmax {200}; const int ymax {100}; int x[xmax]; int y[ymax]; for (int i=0; i<xmax; ++i) { cout << x[i] << endl; } // ... for (int i=0; i<ymax; ++i) { cout << y[i] << endl; } Note that a constant variable cannot be initialised by a non-constant variable: int x {42}; const int y {x}; // error - x is non-constant


What is an example of an object moving at constant speed and constant velocity?

Photon or rather light


What is know that something that you learn to do by practicing rather than from reading a manual?

symbolic knowledge


What is an expression of a meaning that contradicts the literal meaning?

An expression of a meaning that contradicts the literal meaning is called an idiom. Idioms are phrases that have a figurative rather than literal meaning, often making them difficult to understand when translated directly.