Now, I will explain how to display strings on the screen.
It's because necessary to provide various explanations afterward.
Actually, making a program perform various processes is surprisingly easy.
It's surprisingly difficult to make it understandable to humans.
Because, at their core, computer processing is simply the flow of electrical signals.
Fortunately, that can now be readily achieved.
Computers have displays.
So, all you need to do is display the processing contents.
Without the ability to see what's being processed, programming learning cannot succeed.
Because I don't know what the program did and what happened to it.
Therefore, for future programming studies,
I really need to learn how to display text on the screen.
Essentially, displaying text on the screen is a preparation for learning.
printf function
To display a string in C, you use the printf function.
The printf function is used as follows:
Source code
printf("文字列");
For example, if you want to display something like HelloWorld, you would do the following.
Source code
printf("HelloWorld");
If you write this sentence into a program, it will display "HelloWorld" on the screen.
By the way, where exactly should I write this sentence?
As explained in the preceding paragraph, the printf function displays characters on the screen.
However, as mentioned at the beginning, C programs start with the main function.
"That means that `printf` function alone is not enough; you absolutely need a `main` function."
Let's recall the program with the main function that we created initially.
Source code
int main(void)
{
return 0;
}
While I'm explaining that C starts with a main function,
I didn't explain the order in which the program runs within that function.
Within a function, it simply proceeds sequentially through the statements in the preceding sentence .
And when a return statement is reached, the function execution ends there.
For example, suppose we had a program like the following,
Source code
int main(void)
{
文1;
文2;
return 0;
文3;
}
The program runs in the order sentence 1 -> sentence 2.
Before reaching statement 3, the function ends with a return statement, so statement 3 is ignored.
Given this, I know where the printf function should be written.
So, you would write it like this.
Source code
int main(void)
{
printf("HelloWorld");
return 0;
}
Instruction import
"In the previous section, we should have completed the program using the printf function."
Actually, this program is not ready, so it won't display any characters on the screen when run.
一部のお節介を焼くCompilerでは動いてしまいますが・・・
In fact, the printf function is not a feature of the C language itself.
To put it another way, the C compiler doesn't know anything about a function called printf.
Therefore, simply writing the printf function alone won't make it work.
To make it work, you need to have the compiler read the manual for the printf function.
C has special instructions for passing arguments.
It is a #include(include) pseudo-instruction.
The #include pseudo-instruction can be used as follows:
```
The printf function's documentation is in a file called stdio.h.
"That means you'll be able to use the printf function by adding the following program."
Source code
#include <stdio.h>
Where should I write this order?
Since you're handing over the program's instructions, it needs to be before the program is run.
It's too late to hand over the instructions once the program has already run.
Considering this, it would be best to write it at the very beginning (before the main function).
"That is, a program that displays a string on the screen using the printf function would look like this."
Source code
#include <stdio.h>
int main(void)
{
printf("HelloWorld");
return 0;
}
"Note that #include is a pseudo-directive, so it can be placed outside of functions."
Sorry to keep you waiting.
Thank you all for your patience.
Finally, I've completed a program that displays text on the screen.
Source code
#include <stdio.h>
int main(void)
{
printf("HelloWorld");
return 0;
}
Learning C language through suffering (Kushi C) is
This is the definitive introduction to the C language.
It systematically explains the basic functions of the C language.
The quality is equal to or higher than commercially available books.