Chapter 4: Input & Output

Chapter 4: Input & Output

Input is data that a computer receives, and output is data that a computer sends out. This can be very simple, such as the buttons on a keyboard or joypad, to more complex things such as signals which are received from radio waves. For now, let’s keep it simple and just use the keyboard your computer is currently using.

As with the ASCII, each character on your keyboard has a designated arrangement of 1s and 0s. This is then allocated a specific key, but how do we store those keys in our program? How do we get feedback from the user?

scanf() is a native C function, you can think of it as printf()s brother. It takes data from the input device and then stores it at a given address. We define an address by combining an address of operator ‘&’ with a named area of data, a ‘variable’.

Exercise One:

Write the following code into code-blocks:

#include <stdio.h>
int main()
    int firstNumber   ;
    int secondNumber  ;

    printf("Please give me two numbers to add \n")  ;

    printf("\nWhat the first number\n")             ;
    scanf("%d", &firstNumber)                       ;

    printf("\nWhat the second number\n")             ;
    scanf("%d", &secondNumber)                      ;

    printf("%d + %d = %d",firstNumber, secondNumber, firstNumber + secondNumber ) ;


As you can see here, we use this address of operator ‘&’, as the second parameter in the scanf() function, but beyond that, the syntax is virtually the same as printf(), especially with its use of format specifiers.

In this calculator, similar to the one in the video, we are adding variables, and doing a little math on them. In C, there are a small number of arithmetic operators.

Arithmetic Operators

The last one, the % sign is a little confusing, but surprisingly useful for creating limits.

So this in action, the expression “5 mod 2” would evaluate to 1 because 5 divided by 2 has a quotient of 2, as you can fit 2 into 5 twice to make 4, with a remainder of 1.

Simply put: as this is an odd number we need to find the number which 2 can divide into equally to acquire a whole number, which is 4, leaving a remainder of 1 to equal the total, 5.

4 / 2 = 2

5 – 4 = 1 (this 1 is left over, it is the remainder, so 5 mod 2 is equal to 1).

While “16 mod 4” would evaluate to 0 because the division of 16 by 4 has a quotient of 4 and leaves a remainder of 0.

Simply put: 4 X 4 = 16, so there is no data left over, meaning 16 mod 4 = 0 as 4 goes into 16, in 4 nice chunks.

Its a little confusing, but say we wanted to use a random generator below some value, we know that any number % 4, will have a maximum remainder of 3.

For Example

4 % 4 = 4 -> ( 4 + 2 = 0)

5 % 4 = 1 -> ( 4 + 1 = 5)

6 % 4 = 2 -> ( 4 + 2 = 6)

7 % 4 = 3 -> ( 4 + 3 = 7)

8% 4 = 0 -> ( 4 + 4 = 8)

And we can never have a number larger than 4 as the output. We have a simple limit.

Exercise Two

Rewrite the code from the previous exercise, but substitute in the other math operators in place of the addition operator. Next, test the numbers and observe your results. You should always be curious to experiment with your code!

Once you have done this, try to make this little calculator more accurate, by getting it to work with a float variable type rather than an int variable type.

Hint: make sure to adapt your format specifiers...

So long as you have been keeping up with actively writing the code alongside me and my invidious, as well as in these tutorials, hopefully, you should be starting to feel a little more confident.

By then end of this everything posted in this episode you should understand, so if you have struggled with any concepts, perhaps refresh yourself and keep practicing! The % operator is more complicated, so if you are struggling with that one, just keep trying to go over it! You’re doing great!

This course is provided for free, as we at Devoke believe all education should be. However, if you enjoy the course, we are always grateful for support and donations to Patreon to ensure we can continue providing resources for free.

Please Login to read or post comments