Big Tuto: Program with the C language

From theory to practice

   

Chapter 6: Using Variables

   

Tutorial presented by: The Ludovyc and Jérémie F. Bellanger (Jay)
The Ludovyc

Publication: June 8, 2014 
Last revision: March 9, 2025


   

   In this chapter, we will now see how to use variables! And so, it will still be a bit theoretical, but let's first correct the challenge exercise!

   

   Challenge 1

   

#include <stdio.h>
#include <stdlib.h>
 
int main(){

    printf("char size: %d\n", sizeof(char));
    printf("short size: %d\n", sizeof(short));
    printf("int size: %d\n", sizeof(int));
    printf("long size: %d\n", sizeof(long));
    printf("float size: %d\n", sizeof(float));
    printf("double size: %d\n", sizeof(double));
 
}

   

   Do copy/paste (ctrl+c, ctrl+v), it will be faster! wink

   

  Challenge 2

   

#include <stdio.h>
#include <stdlib.h>
 
int main(){

 char cs = sizeof(char); //cs for char size
 printf("char size: %d\n", cs);

 char ss = sizeof(short); //ss for short size
 printf("short size: %d\n", ss);

 char is =  sizeof ( int ) ;  //you should have understood ;) 
 printf ( "int size: %d\n" , is ) ;

 char ls = sizeof(long);
 printf("long size: %d\n", ls);

 char fs = sizeof(float);
 printf("float size: %d\n", fs);

 char ds = sizeof(double);
 printf("double size: %d\n", ds);
 
}

   

   This method is easy to implement, but even with copy/paste, it's tedious to code... frown

   

  Challenge 3 

   

#include <stdio.h>
#include <stdlib.h>
 
int main(){

char s = sizeof(char);
printf("char size: %d\n", s);

s = sizeof(short);
printf("short size: %d\n", s);

s = sizeof(int);
printf("int size: %d\n", s);

s = sizeof(long);
printf("long size: %d\n", s);

s = sizeof(float);
printf("float size: %d\n", s);

s = sizeof(double);
printf("double size: %d", s);
 
}

   

   This method takes longer than the first, but you have more control over the program.smiley

Note:

When programming, we have several choices:

  • ease: Program with a lot of code repetition. Less good in terms of speed and optimization, but easier to program...
  • speed: Program with few lines of code, use of simple algorithms. Less good in terms of control and optimization, however.
  • control: Program where you know what is happening line by line, and where most of the variables are clearly defined and stored. Use of simple algorithms. Less good in terms of speed and optimization, however.
  • optimization: Program with the best algorithms consuming the least memory and processor resources, often used for embedded systems, which are not very powerful. Less good in terms of speed. 

      Now we can start this new chapter!smiley

   

   Here is a draft program:

   

#include <stdio.h>
#include <stdlib.h>
 
int main(){

char c = 1;
char c1 = 1;

//Code additions will be made here for your tests

printf("%d", c2);
 
}

   

   This outline will help me demonstrate to you in practice the different possible uses of the variables that we saw in the previous chapter.

   All variables contain numbers. So we can do additions like that:

char c2 = c + 1;

   

   Note that digits or numbers such as 1 or above will be converted to variables of the type that can contain them, here 1 will therefore be a char, which means that we can do this:

char c2 = c + c1;

   

   If we can do additions, we can also, of course, do subtractions like:

char c2 = c - 1;

   

   or even:

char c2 = c - c1;

   

   Then multiplications:

char c2 = c * 1;

   

   Or:

char c2 = c * c1;

   

   And finally divisions: 

char c2 = c / 1;

   

   Or:

char c2 = c / c1;

 

WARNING: Division by 0 is forbidden and will cause an error that will stop any C program!

  

   

    

   The scanf() function

  

   Where printf() is an output function that displays a string and/or variables, scanf() is an input function that allows you to perform an entry.

    Here is an example:

float f;
scanf("%f", &f);

    

   The first line is the definition of a float variable named f.
   On the second line, the first parameter of scanf() looks like that of printf(). The second parameter corresponds to the address of the variable where the entry will be recorded. This is why we put an "&" in front of the "f", but we will come back to that later wink.

   The program pauses during a scanf() while waiting for user input. This will then end when the "Enter" key is pressed.

   

WARNING: If the user makes an entry that turns out to be more memory-intensive than the predefined variable type, this will cause a buffer overflow or overwriting. Behind these barbaric terms lies a serious problem that can happen to your program if you are not careful enough (fortunately, there are other solutions to get around the problemwink). I will explain it to you with an example.

Example

   Here is a fictional memory:

1- 0000 0000
2- 0000 0000

   1 and 2 each represent a memory address for readability.

   Now here is a little program:

   

#include <stdio.h>
#include <stdlib.h>
 
int main(){

unsigned char c;
unsigned char c1 = 1;
 
scanf("%d", &c);
 
}

    

   In this example, we will say that variable c is stored at address 1 and variable c1 at 2.

  So before the scanf(), our memory will look like this:

1- 0000 0000
2- 0000 0001

   During the scanf() now, the user enters 256. As you already know, with an unsigned char we can only count from 0 to 255. So the computer does what the program asks and stores 256 at address 1 which gives:

1- 0000 0000
2- 1000 0001

   And now, the variable c is still 0 while c1 is now 129... The writing of the variable 1 which is too large has therefore been transferred to the next variable, thereby changing its value, and in some cases, the result can be catastrophic! Overwriting is therefore clearly to be avoided and we will see later how to avoid it. But for now, we will therefore use scanf(), with full knowledge of these facts. wink

  

   Exercise Challenge 

  

   Make a program that adds, in a result variable, two variables of type int , entered by the user using two scanf(). Display the result variable using the printf() function. Pay attention to the type of the result variable!

   The correction will be made in the next chapter!cool

   By then, you are now able to make some nice little programs such as calculating the area of ​​a circle, the volume of a cylinder given by the user, the value of an angle in degrees from a radiant, a temperature in Celsius from one in Fahrenheit...

   But we will see in the next chapter how to do much more! See you soon! smiley

 

 

This site uses cookies to enable you to log in. We do not store or sell any personal data. By continuing to use this website, you agree to their use. Thanks!