MMGames Introduction to C C Language Development Environment C language now Useful Apps Contact Us
MMGames

Automatic version identification

SpineViewer

It's easy to tell by looking at it.

Response Time Checker

I can leave my computer on and do it.

Mouse cleaning time

I can leave my computer on and do it.

Mouse cleaning time

C language learned by suffering
C language learned by suffering

newline character

line break problem
In the previous section, the printf function was used to display a string on the screen.
You can use the printf function to display any number of strings on the screen.

By the way, how would the following program look like when executed?

source code
 #include <stdio.h>

int main(void)
{
    printf("Hello");
    printf("world");
    return 0;
}

The execution results are as follows.

Execution Result
Helloworld

I think you will notice one critical thing when you look at this.
That is, there are no line breaks.

This leaves us with no choice but to display them side by side, which is very inconvenient.
If you display it all the way to the right edge of the screen, it might break lines in that position, but
Is it not possible to make a line break at the position where I want to make a line break?
escape sequence
It is too inconvenient to display text on the screen if line breaks are not possible.
Therefore, the C language provides a function to make a line break at any desired position.

To create a new line in a C program, use escape sequences.
Escape sequences are special characters used to control that cannot be displayed on the screen.
One of the escape sequences is \n, which is supposed to represent a line break.

Keywords.
Escape Sequence

Special characters used to control that cannot be displayed on the screen.


If you write this \n in the string that you want to display
On the screen, the line will be broken at the location where the \n is located.

Note that overseas, the backslash symbol is used instead of the \ (backslash) symbol.
This is due to some differences in fonts between Japanese and foreign computers.
However, since both are treated internally as exactly the same characters, no problem occurs.

The following program is an example of using the escape sequence \n to break a line.

Source Code
 #include <stdio.h>

int main(void)
{
    printf("Hello\n");
    printf("world\n");
    return 0;
}

The result of executing this program will be as follows

Execution Result
Hello!
world

You can use any number of these line break characters in any position you wish.
For example, if the previous program were rewritten as follows, the result would be the same

Source code
 #include <stdio.h>

int main(void)
{
    printf("Hello\nworld\n");
    return 0;
}

It may be a little confusing, but there is a \n in the center.

In many cases, it is easier to see a line break each time a line is displayed, so
From now on, we will always start a new line at the end of a line unless there is a particular reason not to.

There are various other escape sequences, but their use is limited.
Other than newline characters, the most commonly used is \t, which inserts a tab for headroom.
The following program is an example of head-aligning with ɑt.

source code
 #include <stdio.h>

int main(void)
{
    printf("Windows\tMicrosoft\n");
    printf("MacOS\tApple\n");
    return 0;
}

The result of executing this program will be as follows

Execution Result
Windows Microsoft
MacOS Apple

The second character is shown head-aligned.


About this Site

The C language (bitter C), which is learned by suffering, is
This is the definitive C language introductory site.
It systematically explains the basic functions of the C language and
It is as complete as or better than any book on the market.

Part 0: Program Overview
  1. What is the program?
Chapter 2: How to write a program
  1. Writing Rules
  2. Writing conventions
  3. Exercise 2
Chapter 3: Display on Screen
  1. String display
  2. newline character
  3. Exercise 3
Chapter 4: Numeric Display and Calculation
  1. Numeric Display
  2. Basic Calculations
  3. Type of value
  4. Exercise 4
Chapter 5: Numerical Memory and Calculation
  1. Memorize values
  2. Variable Type
  3. Type conversion
  4. Numeric justification
  5. Exercise 5
Chapter 6: Input from the keyboard
  1. Functions for input
  2. Fear of Input
  3. Exercise 6
Chapter 9: Repetition with a fixed number of times
  1. Sentences that repeat themselves
  2. Loop Operation Mechanism
  3. Exercise 9
Chapter 10: Unknown number of repetitions
  1. Loop of unknown frequency
  2. input check
  3. Exercise 10
Chapter 13: Handling Multiple Variables at Once
  1. Multiple variables are handled together.
  2. How to use arrays
  3. Exercise 13
Chapter 19: Dynamic Arrays
  1. Create arrays at will
  2. Exercise 19
Chapter 20: Multiple Source Files
  1. Minimal division
  2. The Stone of Division
  3. Exercise 20

Comment
COMMENT

Open the 💬 comment submission box