When I started programming in C, the first thing I learned was how to structure a simple algorithm to easily visualize the structure and understand the workings of how to use data and how to store it.

This intended to be a basic introduction for anyone who wants to get started in C. This post introduces basic variables and how to structure a C algorithm.

Structure

Basically, a simple algorithm is structured as follows:

  • Algorithm title (optional): Gives an idea about what the program will do. ** Inclusion of libraries: Useful to facilitate many of the tasks in C. The most common libraries to get started are:
    • stdio.h: Useful to get data and print it on screen.
    • stdbool.h: Adds the boolean variable true or false.
  • Declaration of constants.
  • Main program: Here will go all the “instructions” that the program will carry out.
    • Variable declaration.
    • return 0;: It will indicate to the main program that it has already finished.

Here you can see how it would look like initially:

//*Algorithm SUM*//

//Libraries//
#include <stdio.h>
#include <stdbool.h>

//Constants//

// Main program// 
int main(){
//variables//

//Instructions for the algorithm//

return 0; 
}

In a simple algorithm this is included as a matter of course. The use of // is used to add comments and make the code easier to read.

Variables

Variables are information that we store temporarily in the memory of the program. They are of different types, depending on the data we want to store.

The more you learn, you will see that there are different types of variables and you can even create your own variables. I will explain this in more advanced posts.

Basic variables

The most used basic variables in C are:

  • Integers: As its name indicates, it stores integers (-1, 0, 4, 2500…).
  • Real: This type of variables stores numbers that have decimals (4.56, 0.456…)
  • Characters
  • Boolean

In C they are defined as follows:

/Common variables to use in C/
int -> integer
float -> real
bool -> boolean
char -> character

Variable declaration

Whenever we want to use this type of data for our algorithm, we must initially declare them. Remember the structure at the beginning, where I commented that there was a variable declaration section. That is where they are placed.

Imagine that you want to use two integer variables to make a sum. It is not enough just to put int and leave it as it is. At least you have to give it a name, so that you can identify it when you are going to use it.

For example, if I want the program to store two integers, I will put:

int variable1;
int variable2;

This way, whenever I want to use them, I only have to put the name I have given.

Practical example:

Let’s assume that we are going to create an algorithm that asks the user to type in two numbers and the sum of these numbers will be returned on the screen.

The variables that we are going to have to store will be 3: two variables that store the two numbers that the user will put on the screen and a third one that will be the result of the sum of the first two.

Therefore, the program will be structured as follows:

//*Algorithm SUM*//

//Libraries//
#include <stdio.h>
#include <stdbool.h>

//Constants//

// Main program// 
int main(){
//variables//
int variable1;
int variable2;
int result;

printf("Type 2 numbers separated with Enter\n");
scanf("%d", &variable1);
scanf("%d", &variable2);

result = variable1 + variable2;

printf("Your result number is %d", result);

return 0; 
}

Conclusion

This has been an introductory post to the C language. Surely you have seen things in the code that I have not talked about, but they will be worked on later.

For now, keep in mind that variables are those data that can be modified during the program. If you want to see how the code that we have just seen works, you can find it in the following link.