answersLogoWhite

0

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

User Avatar

Wiki User

7y ago

What else can I help you with?

Related Questions

What is a constant in computer?

In computer terms (especially in programming), a constant is a piece of data that has a set value which cannot be changed. For example, 1 and 3 are constants - they will always equal their respective values. Constants can also be set, so you could make: piValue a constant equal to 3.14.


How are constants and variables important in devoloping java programs?

Without variables, you won't be able to do much programming. Variables is where you store data; such data may change over time. And computer programming is all about manipulating data.


How many constants are there?

The concept of &quot;constants&quot; can vary depending on the context. In mathematics, constants are fixed values that do not change, such as π (pi) or e (Euler's number). In programming, constants refer to values that cannot be altered during the execution of a program. Thus, the number of constants can be infinite in mathematics, while in specific applications like programming, it is limited to those defined within that context.


Compare and contrast variables and constants?

In Computer Programming and Mathematics, variables and constants are ways to refering to a value. For example X=1 and Y=2 The difference being, variables are meant to be arbitary and changable, while constants are meant to be fixed and unchangable. For example, there is no reason for the value of Pi to change. So the value of Pi would best be represented by a constant.


Advantages of computer programming?

The advantages of computer programming is that it pays well since programming is a creative task. Computer programming helps human beings solve problems on a regular basis.


What is the diffirence of computer programming and computer science?

Computer science is a more abstract study of computing techniques; which will include computer programming. Computer programming, as it implies, is the practise of writing programs for computers.


How is writing is somewhat like computer programming?

writing and computer programming are basically the same!


How many pages does The Art of Computer Programming have?

The Art of Computer Programming has 634 pages.


How do you remove the governor on a 1992 dodge stealth rt tt?

It is part of the computer programming and can not be removed.It is part of the computer programming and can not be removed.


Where did IBM get their programming language?

Computer programming language


What is the easiest way to learn computer programming?

computer programming for dummies its a book and it teaches you everything


What do you call a person who is addicted to computer programming?

One thing that you can call a person that is addicted to computer programming is a computer nerd. A computer nerd is always on the computer.