Line wrapping in C language can realize the function of switching to the next line. There are three main methods: 1. Using the escape character \n; 2. Using the puts function; 3. Directly wrapping the line in some cases. The EOL macro can be used to automatically select line breaks when cross-platform.
Newline in C language
In C language, newline can use the escape character\ n
to achieve.
Useprintf
function
printf("这是第一行\n这是第二行");
Useputs
function
puts("这是第一行"); puts("这是第二行");
Direct line wrapping
In some cases, you can also wrap lines directly in the code without using any functions or characters:
这是第一行 这是第二行
Cross-platform considerations
In Windows systems,\n
represents a newline character, while in Unix and Linux systems, it represents a newline and carriage return. For cross-platform compatibility, you can use the macroEOL
, which will automatically select the correct newline character depending on the system:
#if defined(_WIN32) || defined(_WIN64) #define EOL "\r\n" #else #define EOL "\n" #endif printf("这是第一行%s这是第二行", EOL);
The above is the detailed content of How to write newline in c language. For more information, please follow other related articles on the PHP Chinese website!