Change the Value of a Fundamental Object in C or Make it Constant
Part 6 of Complete C Course
Foreword: How to change the Value of a Fundamental Object in C or Make it Constant
By: Chrysanthus Date Published: 31 May 2024
Changing the Value of an Object
#include <stdio.h>
int main(int argc, char *argv[])
{
int myInt = 88;
myInt = 99;
printf("%i\n", myInt);
return 0;
}
99 is printed at the terminal window, as the output. myInt was initialized to 88, and later on changed to 99. Remember, after declaration or initialization, the identifier is not preceded by the type indicator, even when the value is being changed.
Note: after initialization, an object is very complete, and that is definition.
Making a Copy of an Object Value for Another Identifier
A copy of an object value can be made in another memory location. Consider the following code segment:
int myInt = 8;
int yourInt = myInt;
printf("%i\n", myInt);
printf("%i\n", yourInt);
The output is:
8
8
Now, myInt and yourInt are two different identifiers holding the same value. The content of the location of myInt was copied to the location of the object of yourInt. An object is not copied; it is the content (value) that is copied. This is because an object refers to both an identifiable location and its content. Different locations are unique, but their contents are not necessarily unique. The different unique locations should have different unique identifiers.
Declaration and Initialization in two Different Statements
If an object is to be declared, as well as initialized, in two different statements, then all that can be done inside a function definition, such as in the main() function, as in the following program:
#include <stdio.h>
int main(int argc, char *argv[])
{
float flt;
flt = 2.5;
return 0;
}
If the declaration and initialization in two statements, is not done inside any function (that is, done outside of all functions), then the program will not compile. The following program does not compile, and issues an error message:
#include <stdio.h>
float flt;
flt = 2.5;
int main(int argc, char *argv[])
{
return 0;
}
However, if the declaration without initialization is not done inside any function definition, and the initialization is done inside a function definition, then that is alright; the program will compile. The following program compiles successfully:
#include <stdio.h>
float flt;
int main(int argc, char *argv[])
{
flt = 2.5;
return 0;
}
Now, if the declaration and initialization is to be done in one statement, then the one statement can be done outside of all function definitions, or inside one function definition. The following program compiles successfully:
#include <stdio.h>
float flt = 2.5;
int main(int argc, char *argv[])
{
char ch = 'A';
return 0;
}
The two statements are independent.
Constant
const int myInt = 55;
Under this condition, the value 55, which is in the region of memory identified by myInt, can never be changed. Note: when using the reserved word, const, the declaration must have initialization in one statement. Test the following program that illustrates the use of the reserved word, const:
#include <stdio.h>
int main(int argc, char *argv[])
{
const int myInt = 55;
printf("%i\n", myInt);
return 0;
}
The output is 55. An object, for whose identifier the value is constant, as in the above code, is referred to as a read-only variable. Now, test the following code, where an attempt is made to modify the object's value; and note that the program does not compile, and an error message is outputted:
#include <stdio.h>
int main(int argc, char *argv[])
{
const int myInt = 55;
myInt = 45;
printf("%i\n", myInt);
return 0;
}
Read the error message and start getting use to appreciating error messages.
The declaration in this const case, cannot be separated into two statements, consisting of declaration as one statement and initialization (definition) as another statement. The following program does not compile and issues an error message:
#include <stdio.h>
int main(int argc, char *argv[])
{
const int myInt;
myInt = 55;
printf("%i\n", myInt);
return 0;
}
Case Sensitivity
C is said to be case sensitive. This means for example, that the identifier, myInt is not the same as MyInt, and is not the same as myint, and is not the same as MYINT, and so on.
Conclusion
This tutorial has explained the different ways in which the value of a fundamental object can be changed or cannot be changed.