Intro to I/O
Tags: c, beginners
Published: 2019-06-12 13:01:59

So, we already learned how to create variables and basic ways to manipulate their values (mathematics operations). But what if we don't want to create a variable and _initialize_ it (insert the first value to it) by yourself? What if we want our program/application to be more interactive and fun and let the users/clients to make this choice?
Moreover, how do we show them information about what is happening? For now, there is nothing on the user's screen but a black console.

C developers asked exactly these two questions before (I guess), and fortunately, therefore we have the functions: ___printf___ and ___scanf___.
- __printf("[text]", [variable/s])__: this function gets a text (to be accurate, a string, but for now we call it "text") and variables and prints an output to the console, for the users.
- __scanf("[text]", &[variable/s])__: this function does the opposite, it gets input from the user and inserts it to the variables.

(If you don't know what a "function" means it's __OK__ for now. We will cover this subject soon. If you insist, think about what you learned in high school: function is a thing which gets input and returns an output. For example: if f(x)=x^2, then f(2) will give us 4 as an output)

For example:
```cpp
#include <stdio.h>
int grade;
printf("Please enter the student's grade before the factor:\n");
scanf("%d", &grade);
grade += 10;
printf("The student's grade after the factor is: %d\n", grade);
```
The console, in this case, will be:
```cpp
Please enter the student's grade before the factor:
90
The student's grade after the factor is: 100
```
As you can see, our program asks the user to insert input and then wait for one. Once it gets the input (in the example above the input we typed was "90"), it inserts it to the integer variable _grade_, increases its value by 10 (a pretty nice factor I wish for all of you) and then prints the new grade (in our case, "100").

"Good", you may say, "but what are all the "%d" and "\n" nonsense?"
The answer is that it that actually isn't as difficult as you might think:
- __%[type]__: in our case, "%d", is just a placeholder for the variable value which comes after the comma. Each variable type (remember?) has %[letter] for himself. In our case, for integer, it's "%d" because it's a shortcut to "decimal". (For char is "%c" and for float is "%f").
- __\n__: to keep it simple, that means "new line". In other words, if you want the next text you print to be in a new line, just put "\n" at the end of your previous printed text. You can also print a few lines at once with a single _prinf_. However, you should always use "\n", not just for this purpose, because "\n" also tells the computer to print your output __right now__. You have to understand, the printing operation is a really expensive and difficult operation for the computer (compared to mathematic operations, for example) and therefore the computer "prefers" collecting all the outputs in a buffer (a small storage place) and flushes it out only once the program explicitly demands it or when the buffer is "full enough". In order to avoid this frustrating situation, just use: "\n". It forces the computer to flush its buffer immediately. To be honest, you might not notice that in most of the modern [IDEs](https://en.m.wikipedia.org/wiki/Integrated_development_environment) because they simulate that for you in their console. However, it's a good habit for the "real life" to put a "\n" at the end of each _printf_.

Yes, I know that there is a '&' before the variable name in the _scanf_, and it's __OK__, it's not a typo (to be more accurate: you have to put this sign before the variable in _scanf_). This will be explained when we get to talk about __Memory addresses__.

Finally, the wise reader could already understand the meaning of the title. I/O stands for: "Input & Output".

####__Always remember, it's much C-mpler than you thought!__

Regards,
Gilad