Create your own game
eng   рус

Data types and variables in C language

Previous tutorial: C Language Basic Syntax
Next tutorial: if else switch in C language
Pseudo-game in C v0.2. Tutorial: Data types

For now we can't do much with C language. In this tutorial we'll learn how to store data during work of the program. For that we need to understand what is variable.

We already know what is string literal - that's just the string of text that appeared in some program. The same way works number literals:


Here we sum two number literals. To be more precise: this statement sums two integer literals. C language differs integer numbers and not integer. Not integer number called float (floating point numbers):

1.1 + 8.9; // sum of two float literals

C needs to differ different type of numbers as float numbers and integer numbers are represented differently in memory. Let's look what data types we can use in C programming language:

Data types in C language

Each data type in C language occupies specific memory capacity. Internally all data types are just a numbers (even text). So each data type has it's range.

int - the most useful data type to store integer number. It occupies 4 bytes (32 bits) of memory. And it's value range from -2,147,483,648 to 2,147,483,647. Old computers used 2 bytes for int.

unsigned int - only positive numbers. 4 bytes. Value range from 0 to 4,294,967,295.

short and unsigned short. Integer numbers with lesser than int range. 2 bytes. short is from -32,768 to 32,767 and unsigned short is from 0 to 65,535.

long and unsigned long. Same as int and unsigned int on modern computers.

char and unsigned char. This type occupies 1 byte (8 bits) of memory and can have only 256 values. Range is from -128 to 127 for char and from 0 to 255 for unsigned char. char type is used in C language to store text. We'll see it soon.

Floating-point types

There are 2 floating point data types worth mention now: float (4 bytes) and double (8 bytes). double data type has increased precision. We'll always be using float.


There is a special 'data type' void - it tells that there is no data type. It may sound strange but void is used a lot.

We saw usage of data types in C language already. Let's look at the head of main again:

int main(void)

Now we know what is int and void. Though the real meaning of all this string we'll understand several tutorials later.

Variables in C language

Let's imagine the game situation. Player is the commander of the tank. He can move around the field. When tank moves, it consumes fuel. So we can say that fuel value is changed as time is going. In C language we can model this situation with variable:

float fuel = 500; // initially tank has 500 liters of fuel // here is some code of moving the tank fuel = 400; // changing the quantity of fuel printf("%f\n", fuel); // 400

On the first string we declared and initialized the variable fuel. Then in the commented code we moved the tank (there is no actual movement, just a comment for now), then we changed fuel value. And then we printed the value of fuel.

Variable is some thing that can change the value. All variables must have an identifier - name by which we can address the variable in our program. Also all variables in C language must have data type. In the code above our variable fuel has float data type, so it can represent not only integer values.

Identifiers in C language

Identifier is a name of a variable. Identifiers needed not only for variables but for functions too. So printf and main are identifiers. C language has rules for identifiers. They can contain letters, digits and underscore character _. Identifiers can't start from digit. Also you can't use C language keywords (like int, float, if, while...) for identifiers. And of course there can't be same identifiers in one program (more precisely - in scope, but we don't know what it is yet).

Variable declaration

There are different ways to introduce variable into our program. First is variable declaration:

int a; float b; char c; int d, e, f;

To declare variable you need to point data type of variable and variable name. You can declare several variables in one statement. After you declare variable you can use it but be careful - declared variable doesn't have value yet!

During variable declaration processor will reserve needed amount of memory for it. For int variable it will reserve 4 bytes, for char - 1 byte...

Variable initialization

We can declare variable and put a value in it at the same time. That's called initialization:

int a = 0; float b = 1.5; char c = 'a'; int d, e, f = 1;

In last string only f variable is initialized with value, d and e are just declared. You should not use declared variables, so it's good to initialize variables with some starting value. For numbers it's usually zero. But that's not always possible, so sometimes you'll just declare variable and then in another part of a program you'll put a value in it.

Assignment operator in C

After variable declaration you can change the value of variable. You can do this with the assignment operator =:

int a; a = 5; int b = 5; int c = 1+2; int d = a + b + c; b = d - a; // At this step: // a = 5, b = 8, c = 3, d = 13

Equal sign = is an assignment operator in C language. It takes what is from the right side of it and puts into variable that is from the left side. You need remember that = is an action, it changes the value of the variable that is on the left side.

Pseudo-game: first steps

In the beginning we'll have 4 variables to simulate our tank: x, y - coordinates, fuel and letter - the character that will represent our tank on the field. Coordinates will be of integer type, fuel - float and the tank representation - char:

int x=0, y=0; float fuel = 500; char tank = 'T';


In this tutorial we learnt about data types and variables in C language.


  1. Write the code that draws the game field in the console. Use format specifier to output letter variable.


Nov. 23, 2016, 5:57 p.m.
1 Guest
#include <stdio.h. int myTank; (a,b,c,t) int x=0,y=0; float=500; char="t" if x=0; printf(%d,a,b,c,t); if y=0; printf(%f,a,b,c,t);
Nov. 23, 2016, 6:35 p.m.
2 Guest
exercise2.from surinaame. #include <stdio,h> int myTank; (a,b,c,t) int x=0,y=0; float=500; char="T";/*I can not see single quote one my vista laptop*/ if x=0,y=0; printf("%d\n",x); printf("%f\n",y); I need more theory.