Declaration of variables
Declaration of Variables A declaration is a directive that tells the computer to create a variable. This means declaring a specific memory location for the...
Declaration of Variables A declaration is a directive that tells the computer to create a variable. This means declaring a specific memory location for the...
Declaration of Variables
A declaration is a directive that tells the computer to create a variable. This means declaring a specific memory location for the variable and specifying the data type of the value it will hold.
Example:
int number = 10;
In this example, we declare an integer variable named number and assign the value 10 to it.
Importance of Declaration:
Memory Allocation: Declaration tells the computer where to store the variable's data.
Type Checking: It ensures that the assigned data type matches the declared type.
Variable Initialization: The declaration sets the initial value of the variable.
Scope and Visibility: Declaration defines the scope of the variable (local or global).
Examples:
float weight = 180.5; declares a float variable named weight with a value of 180.5.
int age = 25; declares an integer variable named age with the value 25.
string name = "John"; declares a string variable named name with the value "John".
Key Points:
A variable declaration starts with the keyword int, float, string, etc., depending on the data type.
A variable name must start with a letter or underscore, followed by letters or digits.
The data type is specified inside parentheses after the variable name.
Variables can be declared in different parts of a program, including at the beginning, inside loops, or within function definitions