C programming variables explained

HacksLand | The computer science playground

Posted by Thilan Dissanayaka on Apr 12, 2020

In computer science you may heard about virtual memory model. In this model computer memory is divided into some segments like stack, heap, data section, bss, text etc. When we write a computer program we can use them to store data. In this document we are going to see how we can use these memory in C.

In C programming one of popular way to store data is using variables. A variable is just a per-allocated memory space to hold some data. These data can be a character, a string, or an integer number. For an example we can store a single character in a "char" variable. We can store a integer in an "int" variable.

Let's get a clear idea about how we can store these data in memory. The computer memory is divided to many small storage rooms. Size of one of these storage room is one byte (Or eight bits). Let's focus on following image

Top of the image you can see a row of bytes. This is how memory is divided into small parts. In the red memory cell we have filled the value ed. Actually ed is the hexadecimal representation of 237. If we translate it to binary it is 11101101

At the bottom of above image you can see a single storage cell with it's eight bits. Assume that this is representation of above red colored byte. It's value should be 237 in decimal and 11101101 in binary. So we stored it on storage cell in binary form. One bit is used for one binary digit.

How many numbers we can store on a single byte? In binary the lowest number we an make with eight digits is 0000000 (zero in decimal). Highest one is 11111111 (255 in decimal). So we can store 256 total numbers (0 to 255). We can expand this storage limit by combining two or more single cells. If we combine two bytes we can store numbers from 0 to

How we can store a character in these memory cells? We have assigned a unique number for every character in the alfabat. For an example the number 65(41 in hexadecimal) is assigned to character "A". 66 for B and 66 is for C. This code allocation is called as ASCII (American Standard Code for Information Interchange). Storing these unique number in a memory is same as store an integer. If you are on a Linux mashing you can use man ascii command to get a full table of ASCII.

Now we are going to see how we can use concepts in C language to store and manipulate data on computer memory.

Variables in C

A variable is an allocated memory space to store some data. In C there are varies variable types. Different operating systems and platforms allocate different number of bytes for variables. We an find the size of a specific variable with sizeof() function.

Let's see how we can declare a variable and store data on it. Syntax for creating new variable is [variable_type] [variable_name]; We can use any supported variable type like int, char etc. Variable's name must follow standard naming rules. following are some examples.

int student_count;

When we use above code. Assembly instructions allocate a space for this variable. After declaring the variable we can store some data on it.

student_count = 10;

In mashing level we copy the value 10 into the allocated memory space. This is called assigning the value "10" into the variable student_count. We can do both of above two steps once like following.

int student_count = 10;

Before use a variable in a C program, we must declare it. If not we get an error while compiling. What if we use a variable without storing a value on it? This is depended on the type of variable. We will talk more about this in "variable scope in C" article.

It is possible to declare two or more variables in a single line as following.

int student_count = 10, teacher_count = 5;

Arithmetic operations on variables

int a = 5;
int b = 2;
int e;

int c = a + b;
int d = a + 1;

a = a + 2;

e = a;

I'm sure you may read and understand what above code do. First we declare two integers with assigned values. Then we do some arithmetic operations on them. Think about int c = a + b;. Hear we first add the value of a and b. Next we declare another integer variable as c. Then we assign previous calculation's result to new variable.

Next code line is int d = a + 1;. Hear we first add the value 1 to a. Next we declare another integer variable as c and assign previous calculation's result to new variable.

What about a = a + 2; ?. Hear we first add the value 2 to a. Next we assign previous calculation's result to the variable a. We did not declare any new variables but modified the value of a

In last code line we can see e = a;. This will simply assign the value of a to variable e

In above examples we used = symbol to represent assign operator. Remember that this is not the equal operator.

Not only the addition we can do with C. In following list we can see other arithmetical operations

int a = 5;
int b = 2;


int c = a + b;
int d = a - b;
int e = a * b;
int f = a / b;
int g = a % b;

You know first four operations are Addition, Subtraction Multiplication and Division. The last one may be new to you. It's called Modulus. This will divide a by b and returns remainder. So in above example value of variable g will be 1.

In C we can use some shorthand operations. think about a = a + 5 . Hear we use a twice. We can short it to a += 5 . In generally we can say it as following. Let's say expression_1 and expression_2 are two expressions

[expression_1] = [expression_1] [operation] [expression_2];

We do some operation on those expressions. We can shortly wrote it as following.

[expression_1] [operation] = [expression_2];

Hear we can see some examples.

a  = a + 5;
a  += 5;


a  = a * b;
a  *= b;


a  = a * b + 6;
a  *= b + 6;

Further more we can write a = a + 1 as a++ or ++a. This is called Unary operator. Both a++ and ++a increments a by one. Also a-- and --a decrement a by one

Are there any deferences between a++ and ++a ? It looks like both of they do same. Let's see following example.

int a = 2, b;

b = a++;

b = ++a;


b = a--;

b = --a;

First we declare two variables as a and b. We assign value 2 to the variable a. In next line we can see Unary operation as b = a++.

Typecasting variables

If we want to temporarily change a variable’s data type we can use typecasting.

Variable Scoping in C

In a C program we can declare a variable inside or outside of a function. In most C programs there is at lest one function. That is main function. Let's see following example.

int a = 10;

int f_alpha(){
	int b = a +  5;
	return b;
}

int f_beta(){
	int a = 4;
	int b = a +  5;
	return b;
}

int f_gamma(){
	int class_count;
	return 0;
}

Hi, I'm Thilan. An engineering student from SriLanka. I love to code with Python, JavaScript PHP and C.

Also read

Aug 12
Error Handling | Python Programming

Errors are absolutely normal when programming. You may get hundred of errors while learning. Not....

Apr 30
XSS overide functions

One of my friend gave me a JavaScript code and asked to trigger an alert() by changing one....

Aug 12
Linux directory managing

As a Linux user you must master Linux terminal. You should be able to handle files and directories....

Comments