MMGameslogo  MMGames
TwitterSharebutton  FacebookSharebutton   
learn through sufferingC Language
learn through sufferingC Language

String Display

absolutely necessary
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?

"HelloWorld program"
HelloWorldは、ほとんどの入門書に登場します。
その意味では、世界一有名なプログラムだと思われます。

Where should I write?
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:

Pseudo-instructions
Pseudo-instructionsとは、プログラムCodeではない命令のことis.
#includeはprintf functionなどの準備をする命令なので、
この命令は機械語に翻訳されず、その前段階で処理されます。


Source code
#include <Description書のFile名>

```
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;
}

The output of this program will be as follows:

Execution results
HelloWorld


Guidelines for Difficult Customers
一般的なC languageの入門書では、1章からここまでのDescriptionをすべて飛ばして、
いきなり上記のプログラムを紹介していることが多いようですし、
#include はおまじないだとしてDescriptionを後回しにするようis.
初めに余計なことをDescriptionすると混乱するとも言われるので
どちらの方針が正しいのかはわかりませんが、
筆者としては、できる限りのDescriptionを行う方針でいきたいと思います。



About This Site

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.

Part 0: Program Overview
  1. What is a program?
Chapter 3: Displaying on the Screen
  1. String Display
  2. newline character
  3. Practice Problem 3
Chapter 4: Displaying and Calculating Numbers
  1. Display of numbers
  2. Basic calculations
  3. Numeric types
  4. Practice Problem 4
Chapter 6: Input from the Keyboard
  1. input function
  2. The fear of input
  3. Practice Problem 6
Chapter 9: Repeating a Fixed Number of Times
  1. Iterative sentence
  2. How Loops Work
  3. Practice Problem 9
Chapter 10: Repeating Without Knowing the Number of Times
  1. Unspecified loop
  2. Input validation
  3. Practice Problem 10
Chapter 13: Handling Multiple Variables at Once
  1. Handling multiple variables collectively.
  2. Arrays
  3. Practice Problem 13
Chapter 19: Dynamic Arrays
  1. Create arrays freely.
  2. Practice Problem 19