answersLogoWhite

0

In computer programming, (1) When a number is needed several times in a program, it is good practice to give that number a name. (2) When a name refers to a number that will never change during a run of a program, it is good practice to declare it as a constant before using it.

In most programming languages, we do not define the number of array items as a constant. In many programming languages, it is easy to add items to an array, and an array keeps track of the number of items it holds, which a program can access using something like "array.length" or "array.shape". However, in C and C++ programming, the programmer must define the size of the array at the time it is defined, and the array does not keep track of the number of items it contains. To not define your array would leave the program open to unchecked growth. "Capping" the array with an upper value ensures that, if something goes wrong, you will not crash the application or system. Also, capping the array will make debugging potentially easier. Capping requires using that number in several places, and so (1) tells us it is good practice to give that number a name. Since it is not possible(*) to change the size of an array in C or C++, the number that holds the number of items in the array will never change, and so (2) tells us it is good practice to declare it as a constant. (*) There are a few tricks one can do with malloc() and realloc() that have the same effect as resizing an array, although technically all those tricks merely create a new fixed-size array.

User Avatar

Wiki User

16y 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.


What is the constant in C programming language?

Anything declared as a constant.


What are the range of character constant and how are thay decided?

Please specify context. If you're referring to computer programming, you'll need to specify the language.


What Circuits considered constant?

What is considered "constant current"


What does CONSANT mean?

There is a typo in your question. Did you mean "constant"? If so, a constant is a value that remains the same and does not change throughout an equation, function, or program. It is used in mathematics, computer programming, and various other fields to represent unchanging values.


What is the significance of declaring a constant unsigned integer?

What is the significance of declaring a constant unsigned integer?


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

The const keyword transforms a variable into a constant. This means the constant cannot be altered via that constant's identifier. The const keyword can also be applied to a class instance method, such that the method will not alter a class instance's immutable members. Note that the const keyword is merely a programming aid that provides assurances a constant will not be altered inadvertently. However, it is still possible to alter the constant by using a non-constant pointer to the constant. However you have to make a conscious effort to override constant behaviour.


What is integer in programming?

it is a type of numeric constant which doesn't contain any fractional part


Is thank you is a NOT a character constant?

No, "thank you" is not a character constant. In programming, a character constant is a single character enclosed in single quotes, such as 'a' or '5'. "Thank you" is a string constant because it consists of multiple characters enclosed in double quotes.


What is velocity of a considered to be constant?

horizontal


What is the time complexity of an if statement in a programming language?

The time complexity of an if statement in a programming language is O(1), which means it takes constant time to execute regardless of the input size.


What is the meaning of 2L in C programming?

2L is the literal constant for a long (int) type with the value 2.