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?
The execution results are as follows.
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?
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.
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.
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.
The result of executing this program will be as follows
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
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.
The result of executing this program will be as follows
The second character is shown head-aligned.
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
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
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.