📜  c 打印多行 (1)

📅  最后修改于: 2023-12-03 14:59:39.357000             🧑  作者: Mango

以C语言打印多行的介绍

在C语言中,我们可以使用多种方法来打印多行的文本。以下是几种实现方法。

方法一

使用printf函数打印每一行的文本。

#include <stdio.h>

int main() {
    printf("This is line 1\n");
    printf("This is line 2\n");
    printf("This is line 3\n");
    printf("This is line 4\n");
    printf("This is line 5\n");

    return 0;
}

输出如下:

This is line 1
This is line 2
This is line 3
This is line 4
This is line 5
方法二

使用puts函数打印每一行的文本。

#include <stdio.h>

int main() {
    puts("This is line 1");
    puts("This is line 2");
    puts("This is line 3");
    puts("This is line 4");
    puts("This is line 5");

    return 0;
}

输出如下:

This is line 1
This is line 2
This is line 3
This is line 4
This is line 5
方法三

使用多个printf函数打印多个字符串,将它们组装成一个多行的文本。

#include <stdio.h>

int main() {
    printf("This is line 1\n"
           "This is line 2\n"
           "This is line 3\n"
           "This is line 4\n"
           "This is line 5\n");

    return 0;
}

输出如下:

This is line 1
This is line 2
This is line 3
This is line 4
This is line 5
方法四

使用单个printf函数打印一个包含多行文本的字符串。

#include <stdio.h>

int main() {
    printf("This is line 1\n"
           "This is line 2\n"
           "This is line 3\n"
           "This is line 4\n"
           "This is line 5\n");

    return 0;
}

输出如下:

This is line 1
This is line 2
This is line 3
This is line 4
This is line 5

以上是几种使用C语言打印多行的方法。您可以根据您的需求选择最合适的方法。