Data types and variables in C language
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):
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 typesThere 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.
voidThere 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:
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:
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).
There are different ways to introduce variable into our program. First is variable declaration:
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...
We can declare variable and put a value in it at the same time. That's called initialization:
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 =:
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:
In this tutorial we learnt about data types and variables in C language.
- Write the code that draws the game field in the console. Use format specifier to output letter variable.