CS Electrical And Electronics
@cselectricalandelectronics
All PostsDifference BetweenProgrammingQuestions And Answers

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:

  1. Difference Between CNN And RNN Architecture In Deep Learning
  2. 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:

  1. Every variable should be associated with type, size, and value.
  2. Whenever a new value is stored into a variable, it replaces the past value.
  3. Reading variables from memory does not change them.
  4. 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.

  1. The first character in the variable should be a letter (from A to Z or a to z ) or an underscore.
  2. The first character can be followed by any number of letters or digits (0 to 9) or underscore.
  3. No extra symbols are allowed other than letters, digits (0 to 9), or underscores.
  4. The length of an identifier can be up to a maximum of 31 characters.
  5. C keywords should not be used as variable names.

The valid and invalid variables are shown below:

ifinvalidIt is a keyword.
$suminvalid$ sign should not be there.
sum=invalid=sign should not be there.
avalid
principle_amountvalid
_ _a_ _ _ _bvalid
3_factorialinvalidshould not start with a digit.
for1valid
forinvalidIt is a keyword.
Sum, 1invalidThe comma should not be there.
sum-of-digitsinvalidMinus sign should not be there.
sum of digits invalidNo spaces are allowed.
– –validUnderscores are valid
Sum1valid

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

What is a variable?

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 declaration

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:

  1. Initializing one variable
  2. Initializing more than one variable
  3. Initializing at appropriate place

Difference Between Local And Global Variable

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:

Author Profile

CS Electrical And ElectronicsChetu
Interest's ~ Engineering | Entrepreneurship | Politics | History | Travelling | Content Writing | Technology | Cooking
Share Now

CS Electrical And Electronics

Interest's ~ Engineering | Entrepreneurship | Politics | History | Travelling | Content Writing | Technology | Cooking

Leave a Reply

Your email address will not be published. Required fields are marked *