answersLogoWhite

0


Best Answer

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

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

Wiki User

9y ago

Constants are value that do not change. For instance, PI and the speed of light are both constants.

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

using self modifying code.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What are constants in computer programming?
Write your answer...
Submit
Still have questions?
magnify glass
imp
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.


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.


How is computer programming used in computer programming?

Parallax is used in computer programming by using a computer and a USB connection. They have been popular in robotics because of their easiness to program.


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 many pages does The Art of Computer Programming have?

The Art of Computer Programming has 634 pages.


How is writing is somewhat like computer programming?

writing and computer programming are basically the same!


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 has the author Edsger Wybe Dijkstra written?

Edsger Wybe Dijkstra has written: 'Selected writings on computing' -- subject(s): Computer programming, Computers, Electronic data processing 'EWD 316' -- subject(s): Computer programming 'A method of programming' -- subject(s): Computer programming 'A discipline of programming' -- subject(s): Computer programming