Antonin Hérault
Antonin Hérault

Antonin Hérault

Variables, arrays and data types (3)

Antonin Hérault's photo
Antonin Hérault
·Jul 11, 2022·

6 min read

Subscribe to my newsletter and never miss my upcoming articles

Play this article

Table of contents

  • Data types
  • Arrays
  • Variables and arrays with no value
  • Printing variables
  • Identifier naming rules
  • Exercises

A variable is a way to store a value in the computer memory, and 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 at 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 by this way :

<identifier> = <value>;
elon_musk_age = 678; // summerlife

Data types

  • char : For a character value
  • short : For a tiny integer value, but we don't really use it
  • int : For a normal integer value, used by default for numbers
  • long : For a long number, when int is not enough to store it
  • float : For a normal decimal value, used by default for decimal numbers
  • double : For a long decimal number, when float is not enough to store it
  • long double : For a longer decimal number than double could store

A value has a certain size in the computer memory, and it depends on the chosen data type and the platform.

Data typeSize (Bytes)Range
char1-128 to 127
unsigned char10 to 255
short2-32,768 to 32,767
unsigned short20 to 65,335
int2 or 4-32,768 to 32,767 or -2,147,483,648 to 2,147,483,647
unsigned int2 or 40 to 65,535 or 0 to 4,294,967,295
long4 or 8-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
unsigned long80 to 18,446,744,073,709,551,615
float41.2E-38 to 3.4E+38 (6 decimal places)
double82.3E-308 to 1.7E+308 (15 decimal places)
long double103.4E-4932 to 1.1E+4932 (19 decimal places)

You may sometimes see long int instead of long. 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 value
  • unsigned : For a positive value only (also includes "0")
unsigned int weird = -5;

This example doesn't work because we are storing a negative value to a variable with an positive-only type. Maybe it will compile, but you will get some runtime problems.

Standard library and types

This header <limits.h> contains all 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 it's 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 by 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 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 also should 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 kind 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

  1. Tell us your name by passing by a variable
  2. Fix the following code :

     #include <stdio.h>
    
     int main(void) {
         printf("PI = %s\n", 3.14);
         return 0;
     }
    
  3. Name the variable :
     int x = 5;
     printf("The kid is %i years old", x);
    

Solutions

  1. 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;
     }
    
  2. 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;
     }
    
  3. Name the variable

    Rename x by kid_age