Variables are the memory location that provides us with the named storages which can be manipulated by our program. Each and every variable in CPP has to be determined on its type so accordingly the size and layout of its memory could be identified and set of operations that could be applied to the variable with information about the range of values it could receive.
Similarly to the identifiers a variable name could be composed of the number, letters and underscore character. But is restricted to the first letter being either a letter or the underscore character, also uppercase letter and lowercase letter are different from each other as CPP is a case-sensitive language.
As discussed before the basic types of variables or identifiers in CPP are
- bool
- int
- char
- float
- double
Though these are the basic variable types in CPP, CPP also provides with some of the complex Variable types like Pointers, Array, reference, Class, Data structures and much more.
Variable Syntax
To be able to use a named variable, variable need to be defined along with its variable type before its usage, it is a good practice to declare all the variables with their types at the start though CPP allows defining variables anywhere, so you can do as you feel like and declare variables on the go. This defining of the variable is known as the variable definition in CPP.
Example :
Variable Type can be any of the data types available to us and after variable type, there could be either series of variable names or a single variable name
As you could observe a series of variable names are separated by the comma and the whole line is ended by a semicolon (;). int marks, number, salary; would define three variable with name marks, number and salary with the variable type or data type as the integer (int) and accordingly memory space would be provided to them by the system.
Variables could also be initialized at the time of their definition, to initialize a variable (provide a starting initial value) just equate the variable with its initial value with the help of an equal sign (=).
Example:
Variable Declaration in CPP: CPP is a very strongly typed language, thus a variable declaration provides a sense of assurance to the compiler that there exists a variable with giving the name and data type informing compiler the size to reserve in memory for the respective variable. The variable declaration has only compile-time meaning.
Now you all would be likely to have a doubt what is the difference between variable declaration and variable definition, declaration only means to tell the program about the variable i.e., its name its data type and in theory does not allocate size to that variable while in variable definition apart from variable declaration it also allocates memory for that variable, Hence it won’t be wrong to think of definition as the superset of the variable declaration.
When we just simply write int var, the variable with name var is “defined” i.e., it is declared along with its memory being allocated. Now how do we declare a variable such that it is not defined? For this purpose, we use a special keyword called "extern".
Also, note that declaration for a variable could be made numerous numbers of times within the source code while definition for one unique variable could only be made once in the particular block of the code.
This ends with the variable and it data types and all general information about it, variable definition and variable declaration are two of the most important things to be learned while starting to learn code so focus on these topics for a bit a read it through till times you understand it completely as it sets the pace for coming lectures.