A typical program uses various values that may change during its execution. For example, a program that performs some operations on the values entered by the user. The values entered by one user may differ from those entered by another user. Hence this makes it necessary to use variables as another user may not use the same values. When a user enters a new value that will be used in the process of operation, can store temporarily in the Random Access Memory of computer and these values in this part of memory vary throughout the execution and hence another term for this came which is known as Variables. So basically, a Variable is a placeholder of the information which can be changed at runtime. And variables allows to Retrieve and Manipulate the stored information.
type variable_name = value; or type variable_names;
char var = 'h'; // Declaring and Initializing character variable int a, b, c; // Declaring variables a, b and c of int type
Characteristics of Variables:
- name : It must be a valid identifier. In above example, var is a valid identifier.
- type : It defines the types of information which is to be stored into the variable. In above example char is a type.
- value : It is the actual data which is to be stored in the variable. In above example ‘h’ is the value.
Rules for Naming Variables
- Variable names can contain the letters ‘a-z’ or ’A-Z’ or digits 0-9 as well as the character ‘_’.
- The name of the variables cannot be started with a digit.
- The name of the variable cannot be any C# keyword say int, float, null, String, etc.
- Valid Variables Names
int age; float _studentname;
- Invalid Variables Names
int if; // "if" is a keyword float 12studentname; // Cannot start with digit
Defining or Declaring a Variable
There are some rules that must be followed while declaring variables :
- specify its type (such as int)
- specify its name (such as age)
- Can provide initial value(such as 17)
int geeks; float interest;
The term initializing means to assign some value to the variable. Basically, the actual use of variables comes under the initialization part. In C# each data type has some default value which is used when there is no explicitly set value for a given variable. Initialization can be done separately or may be with declaration.
int y = 7; // Declaring and initializing the variable at same time int x; // Declaring variable x x = 5; // initializing x with value 5
Two Ways for Initialization:
- Compile time initialization
- Run time initialization
1. Compile Time Initialization
It means to provide the value to the variable during the compilation of the program. If the programmer didn’t provide any value then the compiler will provide some default value to the variables in some cases. Generally, this type of initialization helpful when the programmer wants to provide some default value.
Value of x is 32 Value of y is 0
2. Run Time Initialization
In this, the user has to enter the value and that value is copied to the required variable. In this type of initialization, there is one more possibility in which value is assigned to variable after completion of a function call.
Input : 45 Output : Value of num is 45 Input : 27 Output : Value of num is 27
Value of num is 45
Note: Here the Console.ReadLine() method asks the user to enter the value and later on it puts the same value in the “num” variable. Hence the value will be displayed according to the user input.
- JS++ | Variables and Data Types
- How to avoid Compile Error while defining Variables
- How to drop variables from a dataset in SAS Programming?
- SAS | How to specify a list of Variables?
- Features of C++
- Introduction of Object Oriented Programming
- MATLAB - Plots in Detail
- Amplitude Modulation using MATLAB
- 5 Best Languages for Competitive Programming
- Vectors in Octave GNU
- Strings in Octave GNU
- Difference Between MATLAB and Octave
- How to Learn Python in 21 Days?
- Top Programming Languages for Data Science in 2020
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : shubham_singh