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

newline character

Line break issues
In the previous section, we displayed strings on the screen using the printf function.
You can display strings on the screen as much as you like using the printf function.

"By the way, how will the following program be displayed?"

Source code
#include <stdio.h>

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

The results were as follows:

Execution results
Helloworld

I think you'll notice one significant thing when you see this.
It is not line-broken.

"This way, we're limited to simply displaying them side-by-side, which is very inconvenient."
It may wrap at the right edge of the screen, but...
Is it not possible to force a line break at the point I want?
Escape sequence
It's incredibly inconvenient that I can't make line breaks when displaying text on the screen.
Therefore, C has a feature that allows you to insert a line break at the desired position.

To perform a line break in a C language program, you use an escape sequence.
Escape sequences are special characters used to perform control functions that cannot be displayed on the screen.
One of the escape sequences is \n, which is intended to represent a newline.

キーワード
【Escape sequence】

Control characters used to perform actions that cannot be displayed on the screen.


If you include it within a string that displays the character '\n',
It will wrap and display a new line where there is a \n on the screen.

"Note that overseas, a backslash (\\) is used instead of the yen symbol (¥)."
This is because Japanese computers use characters that differ slightly from the fonts used on computers overseas.
However, no problems arise because both are treated as the exact same character internally.

The following program demonstrates how to create a line break using the escape sequence \n.

Source code
#include <stdio.h>

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

The results of this program's execution are as follows:

Execution results
Hello
world

You can use line breaks wherever you like and as many as you like.
For example, you can rewrite the program as follows, and the result will be the same.

Source code
#include <stdio.h>

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

It might be a little unclear, but there's a \n in the middle.

Often, it's easier to read if you insert a line break after each line.
Going forward, line breaks will be added at the end of each line unless there's a specific reason not to.

There are other escape sequences, but only a limited number are commonly used.
Besides line breaks, \t is often used for indentation.
The following program demonstrates indentation using tabs.

Source code
#include <stdio.h>

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

The results of this program's execution are as follows:

Execution results
Windows Microsoft
MacOS  Apple

The second letter is vertically aligned.


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