C-mpler (13 Part Series)
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)=x2, then f(2) will give us 4 as an output)
#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:
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 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".