Aug 14, 2020

C programming variables explained

In computer science, you may have heard about the virtual memory model. In this model, computer memory is divided into some segments like the 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 this memory in C.

In C programming one of the popular ways to store data is using variables. A variable is just a pre-allocated memory space to hold some data. These data can be a character, a string, or an integer number. For example, we can store a single character in a "char" variable. We can store an 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 into many small storage rooms. The size of one of these storage rooms is one byte (Or we can tell it is eight bits). Let's focus on the following image. 

memory row

Here you can see a row of bytes. This is how memory is divided into small parts. Actually there are thousands of such memory cells in a computer. This number is depended on your RAM and system architecture.

At right side you can see a specified memory cell in yellow color. In the bottom there is a number as 0x80483f4. This is called the address of this specific memory cell. Actually 0x80483f4 is the hexadecimal representation of 134513652. (We use 0x before a number to show it is in hexadecimal form). That mean if we are counting from the beginning this cell is the 134513652 one. (Since we have taken a small part of entire memory this is our first cell).

So the next cell to that one is 134513653 cell. In hexadecimal its number is 80483f5. So we can say the address of next memory cell is 0x80483f5.

Now think about following image. It shows a row of memory cells.

A row of bytes

 In the blue memory cell is filled with the value ed. Actually, ed is the hexadecimal representation of 237. If we translate it to binary it is 11101101.

On the bottom, we have taken that specific memory cell. Every memory byte can be further divided into eight bits. Each bit holds a single digit. Just one or zero. In this way, a single byte can hold a number. One bit is used to represent one binary digit.

How many numbers we can store on a single byte? In binary, the lowest number we make with eight digits is 0000000 (zero in decimal). The highest one is 11111111 (255 in decimal). So we can store 256 total numbers (0 to 255).

0 to 255 range

How we can calculate this number? If there are n number of bits, we can use the following equation to find the maximum number we can represent.

2^n - 1

 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 65535. With four bytes we can store numbers from 0 to 4294967295.

How we can store a character in these memory cells? We have assigned a unique number for every character in the alphabet. For 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 ASCII (American Standard Code for Information Interchange). Storing these unique numbers in memory is the 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. So to store a single ASCII character, we need a single byte.

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 various variable types. Different operating systems and platforms allocate different numbers of bytes for variables. We 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. The following are some examples.

int student_count;

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

student_count = 10;

In the 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 the above two steps once like the following.

int student_count = 10;

Before using 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 value on it? This is depended on the type of variable. We will talk more about this in the "variable scope in C" article.

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

int student_count = 10, teacher_count = 5;

Naming rules for a variable

When we naming a new variable there are some rules to be followed.

  • You can only use alphanumeric characters, numbers, and the underscore in a name. (No blanks or other special characters are allowed)
  • You cant start a variable name with a number. (1st_number is not valid for a variable)
  • C is a case sensitive language. So all variable names are case sensitive. (var1 and VAR1 are treated as different variables)

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 the above code does. First, we declare two integers with assigned values. Then we do some arithmetic operations on them. Think about int c = a + b;. Here we first add the value of a and b. Next, we declare another integer variable as c. Then we assign previous calculation results to a new variable.

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

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

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

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

Not only the addition we can do with C. In the 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 the 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 the remainder. So in the above example value of variable g will be 1.

In C we can use some shorthand operations. think about a = a + 5 . Here we use a twice. We can shorten it to a += 5. In general, 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 write it as follows.

[expression_1] [operation] = [expression_2];

Here we can see some examples.

a  = a + 5;
a  += 5;


a  = a * b;
a  *= b;


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

Furthermore we can write a = a + 1 as a++ or ++a. This is called the 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 them do the same. Let's see the 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 the 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 least one function. That is the main function. Let's see the 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;
}

Apr 16
Wordpress nulled theme checker

We all love free stuff. So many people try to install premium themes and plugins on there WordPress....

Apr 09
C programming array tutorial

In a previous article we discussed how we can declare and use variables in C. We know a variable is....

May 04
C programming strings

In our "Manipulating data with C" article we saw how we can store data in memory. We used the....

Replying to 's comment Cancel reply