Variables in C

Variables in C Language with Examples

In this article, I am going to give a brief introduction to Variables in C Language with Examples. Mainly we will discuss how to represent variables and how to declare variables in the c language. As we already discussed in our previous article, the main things to writing a C Program are variables and methods. The reason is that if two programs want to communicate what we are using is called methods. Generally, we can call it functionality or functions also. And in the process of communication, what the program is sharing is nothing but variables. So, at the end of this article, you will understand the following pointers in detail.

  1. What is a variable?
  2. How do represent variables in C?
  3. How to declare variables?
  4. How to access Variables in C?
Understanding Variables in C Language:

Generally, if we want to execute a program, why we are executing a program means to process the information or process the data. Take an example of a bank application, communication is there. They are executing one program or one transaction. While executing the transaction, they are processing the data like processing the account number, account name, balance, etc.

For every computer application, we must store the information at a particular location. Every machine has memory locations, and every memory location is identified by an address. Just consider in a classroom or in a theater the seating arrangement, just consider the seating arrangement as a memory location.

Every memory location is identified by an address. For a better understanding, please have a look at the below image. As you can see in the below image, 128, 572, 1024, 5098, etc. are one-one memory addresses. We can treat all the addresses are positive integer values.

Understanding Variables in C Language

What is the relation between variable and memory locations?

Generally, if we want to store some information, suppose I want to store a value of 10 in the memory locations. Just consider a classroom, there is no restriction on the students where they can sit. That means the students will go and sit randomly at different locations. In the same way, the value 10 that we want to store will also go and be stored randomly at a particular memory location. For a better understanding, please have a look at the below image.

What is the relation between variable and memory locations?

How to access the data?

Now, I want to access the data, and I just want to print that information. Then how can we print? How we can print the data means we will face the problems. The reason is, in which memory locations the data has been stored that we cannot understand because of a random memory location. So, here accessing becomes very difficult after storing the information. So, what we should do before storing information is first, we need to set the identity to the memory location.

How we can set Identity to Memory Locations?

Before storing the information, first, we need to set the identity to the memory location. The following is the syntax to declare a variable by setting the identity of the memory location in the c language. First, we need to write the data type followed by the identifier.

Syntax: data_type Identifier;

Example: int a; //Here integer is the data type and identifier can be any name and here we set it as a. So, whenever we declare a variable, it gets memory allocated. To one memory location, the identity is set as shown in the below image.

How we can set Identity to Memory Locations?

Here “a” is a named memory location to the location 10344. Later when we are storing an element into a location that is identified by the identifier “a” as follows.

a = 10; //Here, the value is 10 and we are setting this value into a memory location which is identified by “a” as shown in the below image.

Variables in C Language with Examples

For example, in theater, every seat is having some unique number and when you are coming you will sit in a particular seat that is allocated to you. Later if they want to access it, easily they can access it.

What is a Variable in C Language?

A name that is given for any computer memory location is called a variable name. The purpose of the variable is to store some data. The user will access it by the variable name and the compiler will access it by the address. C variable is a named location in a memory where a program can manipulate the data. This location is used to hold the value of the variable. The value of the C variable may get a change in the program. C variable might be belonging to any of the data types like int, float, char, etc.

Note: In C Language, variables are the data objects which are used to store a value. These values can be then manipulated during the execution of the program. You must declare the variable before using it in your program.

Rules for variable declaration in C:
  1. A variable name must begin with a letter or underscore.
  2. Variables are case sensitive
  3. They can be constructed with digits and letters.
  4. No special symbols are allowed other than underscores.
  5. sum, height, _value are some examples of the variable name
Declaring and Initializing C variable:
  • Variables should be declared in the C program before use.
  • Memory space is not allocated for a variable while declaration. It happens only on the variable definitions.
  • Variable initialization means assigning a value to the variable.
How to Declare a Variable in C?

Declaration tells the compiler about the data type and size of the variable. Variable can be declared many times in a program. The variable declaration specifies the name and type of the variable. 

Syntax: data_type variable_name;
Example: int x, y, z;

Here x, y, and z are variable names and ‘int‘ is the data type that specifies that x, y, and z can only store integer type values. In our next article, we will discuss DataTypes in detail.

When we declare a variable in C#, by default it stores some garbage value. Here, garbage value means an unexpected value (it might be zero also). So after the declaration, when we assign some value to the variable. It is called Variable Initialization. Let us see how to initialize a variable in C.

Defining a Variable in C:

When we declare a variable in C#, by default it stores some garbage value. Here, garbage value means an unexpected value (it might be zero also). So after the declaration, when we assign some value to the variable. It is called Variable Initialization.

Syntax: data_type variable_name;
              variable_name = value;
Example: int x, y;
                x = 50, y = 30;   //Here x is defined with a value 50 and y is defined with a value 30

Why do we get garbage value by default in C?

Let us understand this with an example. When we declare an integer variable in C, then 4 bytes of memory will be allocated for it. If there were already 0’s in those 4 bytes then there will be no problem, else if the 4 bytes contain info that we have used earlier then it will be garbage. so we need to clear it. It can be done by initializing the variable with 0.

Variable initialization in C:

Defining a variable with a value at the time of its declaration is known as variable initialization in C. So, we can say that Declaration + Definition = Initialization;

Syntax: data_type variable_name = value;
Example: int x = 0;

Note: It is always a good programming practice to initialize a variable with zero after declaration.

In the next article, I am going to discuss Types of Variables in C Language with examples. Here, in this article, I try to explain Variables in C Language with examples and I hope you enjoy this variable in C Language with Examples article. I would like to have your feedback. Please post your feedback, question, or comments about these Variables in the C Language article.

1 thought on “Variables in C”

Leave a Reply

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