Welcome to 
Programming & Electronics Tutorials
By TsiamDev




UNDER CONSTRUCTION

Exercises #0

Time to practice!

First, we’ll see one self-explanatory exercise (ex1.c) on common arithmetic operations (like addition, multiplication etc.) between:

  1. Variables
  2. Variables and arithmetic constants
  3. Arithmetic constants

Things that haven’t been discussed yet

“\n” – newline character

As we’ve seen before, “\” is the escape character that changes the function of the following character. Therefore, “\n” means go to the next line.

For example:

When ran, prints out 45 – newline – 4 – newline – 5:

Function definition position

Functions should be defined, above the line of code (loc) where they are called for the first time.

For example, this function definition:

produces a few implicit declaration warnings:

Because these are warnings and not errors, the program still manages to be run, but if more code is added, it may have unexpected behavior. Therefore, warnings should be eliminated when possible.

These warnings go away if just “hoist” our function’s definition above line 5, i.e. before the definition of main:

No warnings!

Certain scripting languages, like JavaScript, do this automatically. It is called “function hoisting” and it is very convenient, as it allows you to define your functions anywhere. But it is a good programming practice to define your functions close to where you call them (if they are defined in the same file that they are executed in). Otherwise, we’ve already seen that the #include statements all go to the top of the file.

Div and mod operations

For arithmetic division, there are two operators. One that returns the result of the division (div operator - < / >) and one that returns the remainder of the division (mod-ulo operator < % >).

Fun fact! In some programming languages, like python for example, the operators are the words div and mod e.g. 2 div 4 and 2 mod 4.

For example, let’s talk about 2 / 4 division. Below you can see what each operator will return:

Floating point numbers

There is a data type that is called “double” (and another called “float”). A variable of type double (or float), stores a floating-point number (e.g., 5.6 or 3.14). The difference between the two, is the number of decimal digits that it can store (i.e., precision issues), but we won’t go into further details about this topic, at least for now.

All you need to know, to solve the exercises, is that you can print a float or double value with “%f” instead of “%d” that we had for integers.

If you want more details on the subject, you can take a look at this: https://www.geeksforgeeks.org/difference-float-double-c-cpp/

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Exercises

Feel free to send me your answers either through github (do you know how to make a pull request?) or on social media if you want to!

  1. Define a function that takes one integer argument, computes the square root of that integer, and then prints it to the console.
  2. Define a function that takes two integer arguments and returns the absolute of their difference, i.e. | a – b |. Then, print the result to the console.
  3. For the function of 2), call the function 3 times (in main). Then, modify the program so that it counts how many times this function has been called (i.e., the machine will be the one that does the counting, not you! 😊) and print it to the console.
  4. Hooray! You’ve built your first counter! 😊

  5. Separate the source code of 3) into two files, the header file, and the source file.
  6. REMINDER:

    Use comments throughout your code, even though the exercises are simple, in order to get acquainted with them!