What is Variable in c
Variables
Q) What is a variable?
Q) Define variable.
- Variable means an entity whose value seat vary (i.e.change) during the specific execution of a program. A perfectly speaking variable is a named memory location.” yes, the variable is nothing but another name given to memory location. We can use variable to hold any type of data.
Image Source ~ Crafted With ©Ishwaranand – 2020 ~ Image by ©Ishwaranand |
Variable declaration
Q) explain the variable declaration.
Q) write the syntax for the declaration of the variable.
- The declaration means telling the compiler about the name and type of the data hold by the variable, that we want to use in our program. The declaration refers to the process of informing the compiler about the variable used in the program. Before using any variable the variable needs to be declared.
Following is the general form of the declaration of variable:
- data_type variable_name;
Here,
- data_type – Type of the data hold by the variable.
- variable_name – Name of the variable. It can be any valid identifier.
For example:
int a;
- This statement is called a variable declaration. The statement tells the compiler that “a” is the name of the variable and its data type as int(integer) i.e. it can contain any integer value (-32768 to +32767)
float f;
- Here, f s the variable of type float.
Variable initialization
Q) explain the initialization of a variable.
Q)write the syntax for the initialization of a variable.
- Assigning the to variable at the time of creation or declaration is called variable initialization or simply initialization.
- Initialization provides the value to the variable. if the variable is not initialized, then it will contain unpredictable or non-useful(garbage) values.
Following is the form for the initialization of the variable:
- data_type variable_name= value;
Here,
- data_type – Type of the data hold by the variable.
- variable_name – Name of the variable. it can be any valid identifier.
- Value – The initial value that we want in a variable.
For example:
Â
int i = 10;
- The above statement is called initialization. This statement declares ‘i’ as the int (integer) variable and will be assigned the value ’10’
Float pi = 3.1415;
- The above statement will create a variable called “pi” of type float with “3.1415” as the initial value.
The rule for constructing variable names
Q) write the rules for constructing for constructors.
- Some variable name is any combination of 1 to 31 alphabets, digits or underscores. Any compilers allow variable names whose distance could be up to 247 characters. still, it would be safer to stick to the rule of 31 characters.
- A variable name can contain any alphabet, digit or underscore.
- The first characters in the variable name must be an alphabet or underscore/
- A variable name may contain a maximum of 31 characters.
- No commas or blanks are allowed within a variable name.
- No special symbol other than an underscore ( as in gross_sal) can be used in a variable name.
Last updated on Sunday - May 21st, 2023