Variables, arrays and data types (3)
A variable is a way to store a value in the computer memory, and to retrieve it from its identifier. Every variable has a type, and you have to specify it when you declare one.
<type> <identifier>;
<type> <identifier> = <value>;
In this example, we create a variable to store the age of Elon Musk today (51yo).
int elon_musk_age = 51;
We choose the int type because we store an integer number.
You can change the value of the variable in this way :
<identifier> = <value>;
elon_musk_age = 678; // summerlife
Data types
char
: For a character valueshort
: For a tiny integer value, but we don't really use itint
: For a normal integer value, used by default for numberslong
: For a long number, whenint
is not enough to store itfloat
: For a normal decimal value, used by default for decimal numbersdouble
: For a long decimal number, whenfloat
is not enough to store itlong double
: For a longer decimal number thandouble
could store
A value has a certain size in the computer memory, and it depends on the chosen data type and the platform.
Data type | Size (Bytes) | Range |
char | 1 | -128 to 127 |
unsigned char | 1 | 0 to 255 |
short | 2 | -32,768 to 32,767 |
unsigned short | 2 | 0 to 65,335 |
int | 2 or 4 | -32,768 to 32,767 or -2,147,483,648 to 2,147,483,647 |
unsigned int | 2 or 4 | 0 to 65,535 or 0 to 4,294,967,295 |
long | 4 or 8 | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
unsigned long | 8 | 0 to 18,446,744,073,709,551,615 |
float | 4 | 1.2E-38 to 3.4E+38 (6 decimal places) |
double | 8 | 2.3E-308 to 1.7E+308 (15 decimal places) |
long double | 10 | 3.4E-4932 to 1.1E+4932 (19 decimal places) |
Sometimes, you may see
long int
instead oflong
. It's the same thing, you can choose which one you wanna use
Prefixes for data types
There are two prefixes for data types : unsigned
and signed
. By default when you declare a type it's like if you were using the signed
prefix. It's implied, except for the char
type.
signed
: For a negative or positive valueunsigned
: For a positive value only (also includes "0")
unsigned int weird = -5;
This example doesn't work because we are storing a negative value into a variable with a strictly positive type. Maybe it will compile, but you will get some runtime problems.
Standard library and types
This header <limits.h>
contains all the max and min sizes for data types
To get the size in memory of a variable, you can use sizeof(<variable>)
:
#include <stdio.h>
int main(void) {
printf("Size in memory for an integer : %li\n", sizeof(int));
int a = 5;
printf("Size in memory for 'a' : %li", sizeof(a));
return 0;
}
Size in memory for an integer : 4
Size in memory for 'a' : 4
Arrays
We can create lists of values, called "arrays"
<type> <identifier>[<length>];
<type> <identifier>[<length>] = {<values>};
Example :
int values[3] = {78, 2, 560};
Then to get a value from an array :
<array>[<index>]
Example :
int values[3] = {78, 7, 560};
int value_from_array = values[1]; // 7 assigned
What ? Why is it 7 and not 78 assigned to the variable ? We got the element "1"
Yes, but no. In C and in a lot of languages, we start counting from 0. So [0]
is the first element, [1]
is the second one, [2]
is the third one... For reasons coming from Assembly, C designers decided to work in the same way.
You also can change a value from an array :
values[0] = 100;
// now, `values` is `{100, 7, 560}`
Array of characters
There is no "string" in C like we could find in another language. But you can create an array of characters to do the same thing :
char my_name[7] = {'A', 'n', 't', 'o', 'n', 'i', 'n'};
That's so annoying to write each letter one by one in an array !
No problem, we can do the same by :
char my_name[7] = "Antonin";
Variables and arrays with no value
It's possible to declare an array or a simple variable without setting a value :
int unknown;
int unknonw_array[5];
But be careful because weird things can happen. In common cases, the variable is directly filled by the compiler with "0". But you should assign a zero value by yourself instead of letting the compiler doing it :
int unknown = 0;
int unknown_array[5] = {};
Printing variables
We have seen the printf
function. But did you know that you can print variables ? The "f" in "printf" stands for "formatting", to format values into the string.
#include <stdio.h>
int main(void) {
char name[4] = "Elon";
int elon_musk_age = 51;
printf("%s has %i years old\n", elon, elon_musk_age);
printf("PI = %f\n", 3.14);
return 0;
}
Elon has 51 years old
PI = 3.140000
Identifier naming rules
There are strict rules to respect before naming a variable.
You cannot use special characters. This example tries to do it, but it doesn't compile :
int main(int argc, char** argv) {
int à = 5;
return 0;
}
main.c: In function ‘main’:
main.c:12:9: error: stray ‘\303’ in program
12 | int �� = 5;
| ^
main.c:12:10: error: stray ‘\240’ in program
12 | int �� = 5;
| ^
An identifier cannot be the same as a reserved token from the language. This is the list of forbidden keywords for identifiers :
Language keywords |
auto break case char const continue default do double else enum extern float for goto if int long register return short signed sizeof static struct switch typedef union unsigned void volatile while |
You should also avoid using the same identifiers for functions and variables :
int foo(void) {
return 0;
}
int main(int argc, char** argv) {
int foo = 5;
foo();
return 0;
}
Because you will get this sort of errors :
main.c: In function ‘main’:
main.c:17:5: error: called object ‘foo’ is not a function or function pointer
17 | foo();
| ^~~
main.c:16:9: note: declared here
16 | int foo = 5;
| ^~~
At this point, the compiler knows foo
as a variable, not a function
You cannot start the identifier with a number, but you can put some numbers in the identifier after the first letter. "9hello" isn't valid, but "h9ello" is.
Exercises
- Tell us your name by passing by a variable
Fix the following code :
#include <stdio.h> int main(void) { printf("PI = %s\n", 3.14); return 0; }
- Name the variable :
int x = 5; printf("The kid is %i years old", x);
Solutions
Tell us your name by passing by a variable
For the name "John" :
#include <stdio.h> int main(void) { char name[4] = "John"; printf("Hey you ! I'm %s\n", name); return 0; }
Fixing code
You have to change
%s
to%f
in the string because we are passing a floating value, not a string#include <stdio.h> int main(void) { printf("PI = %f\n", 3.14); return 0; }
Name the variable
Rename
x
bykid_age