Welcome to
Programming & Electronics Tutorials
By TsiamDev
UNDER CONSTRUCTION
Time to practice!
First, we’ll see one self-explanatory exercise (ex1.c) on common arithmetic operations (like addition, multiplication etc.) between:
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:
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.
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:
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/
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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!
Google “c square root” – you should learn to google efficiently as soon as possible!
Hint #2: there is a library called <math.h> that defines a function called “sqrt(x)” for this purpose
Hint #3: if you haven’t stumbled across this already, here is an example - https://www.tutorialspoint.com/c_standard_library/c_function_sqrt.htm
Hint #1: Google “c absolute”
Hint #2: Here is an example - https://www.tutorialspoint.com/c_standard_library/c_function_abs.htm
Hint #1: Remind yourself about global and local variables!
Hooray! You’ve built your first counter! 😊
Hint #1: Remind yourself about header files!
Use comments throughout your code, even though the exercises are simple, in order to get acquainted with them!