C Variables

In this session, we want to talk about the C variables and data in the C language in a more general way. In programming, when you want to send, receive, or do anything with a series of data, you must save it somewhere and know what kind it is. Therefore, the use of variables and familiarity with their types becomes meaningful.

C variables and data types

To process information in a computer, it is necessary to define different types of data in C language. These data can be fixed or variable. Static data does not change during the program, but variable data can change many times during the program. In C language, there are two types of data, simple and compound, which can be seen in the table below. But in this lesson, we will examine simple data.

Simple dataComposite data
characterarray
integerstring
Decimal number (float)structure
pointer

Character in C

When we talk about variables in C language, we first think of the simplest type. One of the types of variables used in programming is character data. In many programming languages, numeric data and character data are different from each other. For example, the number 2 is numerical data, and the letter ‘A’ is character data. In practice, characters are stored as numbers in computer memory, and each character has a numerical code. 

In all encodings, there is an associated numerical symbol for each character, which is called ASCII Code, and its value is from 0 to 255. One byte of memory space is needed to store data of character type. To define a data of character type, “char” can be used in this way :

<variable type> <variable name>;
char x;
signed char temp;

As you can see in the example, we must first specify the type of the variable, which is defined as char for the character data type. Then we choose the name of the variable as desired. Note that variable naming should be done according to C language naming rules. These rules are as follows:

  1. The name of a variable only contains upper and lower-case English letters, numbers, and _.
  2. A variable name cannot start with a number.
  3. A variable name can start with the alphabet or _.
  4. It is allowed to use numbers in the variable name except for the first character.

If we want to store something in this variable, we can write x=’b’, which causes the ASCII code of the letter b to be stored in the x variable. Click here to see the ASCII code of different characters.

It is important to state that the characters are surrounded by a pair of single quotes, so 5 is different from ‘5’ because the first is the number 5 itself, but the second is the ASCII code to display this number, which is equal to 53.

The character data type is unsigned by default. That is, to store one-byte numbers (range 0 to 255), char type can be used. But if we want to store signed numbers in it, we can use the signed descriptor in the form of signed char. In this case, we can cover the range of -128 to 127. 

Integer

In general, positive, negative, and zero integers occupy 16 or 32 bits of memory. Int is used to specify this data type. The definition of a variable of this type is the same as the character data type, but with the difference that int is used instead of char. The size of this variable varies according to the type of machine and compiler, but usually, int data is considered as 2 bytes or 16 bits. 

When defining int variables, short, long, signed, unsigned descriptors or a combination of them may also be used. It is important to explain that using a long descriptor before int will double its size (which is 16 bits of data) to 32 bits. Of course, you should refer to the Help section of each compiler. Because in some compilers, 16 bits are normally considered for int, and in others, 32 bits.

If the normal mode is 16 bits, you can use int or short int, which are the same and using long int doubles the size of the variable. But if in the normal mode of the compiler, the size of int is equal to 32 bits, there is no difference between int or long int, and a 4-byte variable is produced. In this case, you must use the short descriptor to generate a 2-byte variable. Pay attention to the following example:

int x1;
long int x2;
unsigned short int x3;

If we assume that the compiler considers a 16-bit size for int by default, then the variable x1 in the first line is 16-bit, and x2 is 32-bit in the second line. In the third line, x3 will remain the same as 16 bits. Check yourself for the default 32-bit mode.

Another point in the mode of double-byte integer variables is that they can cover the range from 0 to 65535 in unsigned mode. Variables representing integers are described as follows.

unsigned int x1;
signed int x2;
short int x3;
long int x4;
unsigned long int x5;
unsigned short int x6;

Decimal number

In C language, it is possible to store decimal data, in other words, floating point data. The use of this type of data is used for very small and fractional values ​​or large values ​​with high accuracy. To define this type of variable, two keywords called float, and double are used.

Each variable of float type occupies a volume equal to 4 bytes of memory. For this type of variable, descriptors can be used, and positive and negative decimal numbers can be stored. For more information about this type of variable, you can refer to the help of the compiler. Consider the following example:

float temperature;
temperature = 12.87;

In the example above, first, a variable called temperature is defined, which means temperature and is of float type. Then, in the second line, the decimal number 12.87 is stored in this variable. Such a variable requires 4 bytes of storage space.

Quick test: If we had used double instead of float, how many bytes of storage space would be required?

Final word

I hope you have learned good points from this lesson and have familiarized yourself with variables in C language. I recommend that you practice the same things we did in Codeblocks. Whenever you are ready, you can jump to the next lesson.

Was this helpful?
[0]
Scroll to Top
Scroll to Top