Difference Between Local And Global Variable, What Is A Variable
Hello guys, welcome back to my blog. In this article, I will discuss the difference between global and local variables, what is variable, rules for defining a variable, how to define a variable, etc.
If you require an article on some other topics then comment us below in the comment box. You can also catch me @ Instagram – Chetan Shidling.
Also, read:
- Difference Between CNN And RNN Architecture In Deep Learning
- Difference Between Machine Learning Artificial Intelligence Deep Learning
Difference Between Local And Global Variable
What is a variable?
A variable is a title given to a memory location where some data can be stored. Applying the variable name, the data can be stored in a memory location and can be accessed or handled very easily. The points to learn when we use variables are:
- Every variable should be associated with type, size, and value.
- Whenever a new value is stored into a variable, it replaces the past value.
- Reading variables from memory does not change them.
- The value stored in the variable may change during the execution of the program.
Rules of defining a variableThe following rules are followed while we use a variable.
- The first character in the variable should be a letter (from A to Z or a to z ) or an underscore.
- The first character can be followed by any number of letters or digits (0 to 9) or underscore.
- No extra symbols are allowed other than letters, digits (0 to 9), or underscores.
- The length of an identifier can be up to a maximum of 31 characters.
- C keywords should not be used as variable names.
The valid and invalid variables are shown below:
if | invalid | It is a keyword. |
$sum | invalid | $ sign should not be there. |
sum= | invalid | =sign should not be there. |
a | valid | – |
principle_amount | valid | – |
_ _a_ _ _ _b | valid | – |
3_factorial | invalid | should not start with a digit. |
for1 | valid | – |
for | invalid | It is a keyword. |
Sum, 1 | invalid | The comma should not be there. |
sum-of-digits | invalid | Minus sign should not be there. |
sum of digits | invalid | No spaces are allowed. |
– – | valid | Underscores are valid |
Sum1 | valid | – |
How to define a variable
Defining a variable is a method of telling the compiler to preserve the memory space for the data based on the type of variables. The compiler keeps the space in memory which is large sufficient to store different types of data and connect there reserved memory locations with variable names. The programmers can obtain the data or manage the data using their variables.
The syntax to define variables is shown below:
Syntax
where,
01. x1, x2, ….. xn are variable names. All the variables should be separated by commas and must end with semicolon.
02. “type” is type of variable x1, x2,…. xn. The type of variables may be int, float, char, double, etc.
The various points to be remembered when defining the variables are:
01. All variables of the same data type can be defined in single line and ending with semicolon as shown below.
int x, y, z;
02. All the variables of the same data type can be defined separately in multiple lines for better readability and each ending with semicolon as shown below.
int a;
int x;
int w;
Example:
int x, y, z;
Using the above statement, the compiler keeps two bytes of memory space for all of the variables. That is because all variables x, y, and z are defined as integer variables using the keyword int, and size of the integer is 4 bytes. The memory locations are not initialized and hence they carry garbage values as shown below.
Variable initialization
The variables are nothing but memory locations which are not initialized when they are defined. Hence, variables normally contain garbage values (meaningless values) and hence they have to be initialized with valid data. The method of giving the initial values for the variables before they processed is called initialization of variables. The variables initialization can be done using three different ways:
- Initializing one variable
- Initializing more than one variable
- Initializing at appropriate place
Difference Between Local And Global Variable
01. The global variables are declared outside all the functions but not inside the program whereas the local variables are declared inside the function.
02. Global variables are alive and holds the values as long as the program is being executed. Local variables are alive when control enters into the function during execution and are destroyed when the control comes out of the function.
03. The global variables are accessible throughout the program. The local variables are accessible only in the function inside which they have been created.
04. Global variables are allocated memory on the permanent storage. Local variables are allocated memory on the stack.
05. Global variables are always initialized to zero. Local variables are not initialized automatically in some compilers.
06. Addresses are assigned in the order in which the global variables are present in the program but in increasing order. Addresses are assigned in the order in which the local variables are present in the program but in decreasing order.
I hope this article may help you all a lot. Thank you for reading. if you have any doubts related to the article “difference between local and global variable”, then comment below.
Also, read:
- 100+ C Programming Projects With Source Code, Coding Projects Ideas
- 1000+ Interview Questions On Java, Java Interview Questions, Freshers
- App Developers, Skills, Job Profiles, Scope, Companies, Salary
- Applications Of Artificial Intelligence (AI) In Renewable Energy
- Applications Of Artificial Intelligence, AI Applications, What Is AI
- Applications Of Data Structures And Algorithms In The Real World
- Array Operations In Data Structure And Algorithms Using C Programming
- Artificial Intelligence Scope, Companies, Salary, Roles, Jobs
- AWS Lambda, Working, Cost, Advantages, Disadvantages
- AWS Technical Interview Questions, Top 200+ AWS Questions
- Battery Management Systems Using Artificial Intelligence
- Best Engineering Branch For Future
- Best Programming Languages For Electrical and Electronics Engineers
- Big Data, Evolution Of Big Data, Benefits Of Big Data, Opportunities
- Bit Operation In C Programming With Example & Applications
- Blockchain Projects For Computer Science Engineers
- Blockchain Technology, History, Working, Applications, Advantages
- Brain Computer Interfaces Technology, Beyond AI, ML, IoT, Blockchain
- C Language Interview Questions On Programs With Output
- C Program On Arrays With Output For Placement Exams