当前位置:首页 C++ > 正文

c/c++中cout、cout.put、cin、cin.get、cin.getline、getline、puts、putchar、printf、gets、getchar、scanf详细讲解

作者:野牛程序员:2023-07-24 14:23:41 C++阅读 3049

当进行输入和输出时,程序通常按照以下顺序执行:

  1. 输入数据阶段:

    • 使用 scanf() 函数来读取格式化的数据,如整数、浮点数、字符等。它使用格式化字符串指定输入的数据类型,并通过地址传递将数据存储到相应的变量中。

    • 使用 getchar() 函数来读取单个字符。它从标准输入读取一个字符并返回其 ASCII 值。

    • 使用 gets() 函数来读取一行字符串。然而,强烈不建议使用 gets(),因为它没有边界检查,容易导致缓冲区溢出。应该使用更安全的 fgets() 或者 C++ 中的 getline() 函数来代替。

示例使用 scanf()getchar()

#include <stdio.h>

int main() {
    int num;
    char ch;
    char str[100];

    printf("Enter an integer: ");
    scanf("%d", &num);

    printf("Enter a character: ");
    getchar(); // Consume the newline character left by previous scanf
    ch = getchar();

    printf("Enter a sentence: ");
    gets(str);

    printf("You entered: %d, %c, %s\\n", num, ch, str);
    return 0;
}

输入:

Enter an integer: 42
Enter a character: A
Enter a sentence: Hello World!

输出:

You entered: 42, A, Hello World!
  1. 输出数据阶段:

    • 使用 printf() 函数来格式化输出数据。它使用格式化字符串指定输出的格式,并通过参数列表传递要输出的数据。

    • 使用 putchar() 函数来输出一个单个字符。

    • 使用 puts() 函数来输出以 null 结尾的字符串,并自动换行。

    • 在C++中,使用 cout 对象来进行输出。您可以使用 << 运算符来向 cout 发送数据,并使用 cout.put() 来输出单个字符。

示例使用 printf()putchar()

#include <stdio.h>

int main() {
    int num = 42;
    char ch = 'A';
    char str[] = "Hello World!";

    printf("The number is: %d\\n", num);

    putchar(ch);
    putchar('\\n');

    puts(str);

    return 0;
}

输出:

The number is: 42
A
Hello World!

示例使用 C++ 中的 coutcout.put()

#include <iostream>

int main() {
    int num = 42;
    char ch = 'A';
    char str[] = "Hello World!";

    std::cout << "The number is: " << num << std::endl;

    std::cout.put(ch);
    std::cout << std::endl;

    std::cout << str << std::endl;

    return 0;
}

输出:

The number is: 42
A
Hello World!

这样,可以更详细地了解每个输入/输出函数在C/C++中的使用方式和特点。请注意,由于C和C++之间的语法和库函数略有不同,因此在选择输入输出函数时应根据所使用的编程语言来做出适当的选择。而且,一些函数已被标记为不安全,不推荐在实际编程中使用。因此,请根据实际需求选择最合适的函数来进行输入和输出操作。


  1. 输入函数:

  • scanf()(C):使用 scanf() 函数,可以从标准输入读取数据,并按照指定的格式存储在变量中。

示例:

#include <stdio.h>

int main() {
    int num;
    printf("Enter a number: ");
    scanf("%d", &num);
    printf("You entered: %d\\n", num);
    return 0;
}

输入:

Enter a number: 42

输出:

You entered: 42
  • getchar()(C):getchar() 函数从标准输入读取一个字符,并返回对应的字符值。

示例:

#include <stdio.h>

int main() {
    char ch;
    printf("Enter a character: ");
    ch = getchar();
    printf("You entered: %c\\n", ch);
    return 0;
}

输入:

Enter a character: A

输出:

You entered: A
  • gets()(C):gets() 函数从标准输入读取一行字符串,但不进行输入边界检查,不推荐使用。

示例:

#include <stdio.h>

int main() {
    char str[100];
    printf("Enter a sentence: ");
    gets(str);
    printf("You entered: %s\\n", str);
    return 0;
}

输入:

Enter a sentence: Hello World!

输出:

You entered: Hello World!
  1. 输出函数:

  • printf()(C):使用 printf() 函数,可以将格式化的数据输出到标准输出。

示例:

#include <stdio.h>

int main() {
    int num = 42;
    printf("The number is: %d\\n", num);
    return 0;
}

输出:

The number is: 42
  • putchar()(C):putchar() 函数用于输出一个字符到标准输出。

示例:

#include <stdio.h>

int main() {
    char ch = 'A';
    putchar(ch);
    return 0;
}

输出:

A
  • puts()(C):puts() 函数输出一个以 null 结尾的字符串,并在末尾自动换行。

示例:

#include <stdio.h>

int main() {
    char str[] = "Hello World!";
    puts(str);
    return 0;
}

输出:

Hello World!
  • cout(C++):在C++中,cout 是输出流对象,用于格式化输出数据到标准输出。

示例:

#include <iostream>

int main() {
    int num = 42;
    std::cout << "The number is: " << num << std::endl;
    return 0;
}

输出:

The number is: 42
  • cout.put()(C++):cout.put() 函数用于输出一个字符到标准输出。

示例:

#include <iostream>

int main() {
    char ch = 'A';
    std::cout.put(ch);
    return 0;
}

输出:


A
  • cin(C++):在C++中,cin 是输入流对象,用于从标准输入读取数据。

示例:

#include <iostream>

int main() {
    int num;
    std::cout << "Enter a number: ";
    std::cin >> num;
    std::cout << "You entered: " << num << std::endl;
    return 0;
}

输入:

Enter a number: 42

输出:

You entered: 42
  • cin.get()(C++):cin.get() 函数从输入流读取一个字符,包括空格和换行符。

示例:

#include <iostream>

int main() {
    char ch;
    std::cout << "Enter a character: ";
    ch = std::cin.get();
    std::cout << "You entered: " << ch << std::endl;
    return 0;
}

输入:

Enter a character: A

输出:

You entered: A
  • cin.getline()(C++):cin.getline() 函数从输入流读取一行字符串,可以包含空格,但不包括换行符。

示例:

#include <iostream>

int main() {
    char str[100];
    std::cout << "Enter a sentence: ";
    std::cin.getline(str, 100);
    std::cout << "You entered: " << str << std::endl;
    return 0;
}

输入:

Enter a sentence: Hello World!

输出:

You entered: Hello World!
  • getline()(C++):getline() 函数类似于 cin.getline(),但不属于输入输出流。它用于从输入流中读取一行字符串,可以包含空格,但不包括换行符。

示例:

#include <iostream>
#include <string>

int main() {
    std::string str;
    std::cout << "Enter a sentence: ";
    std::getline(std::cin, str);
    std::cout << "You entered: " << str << std::endl;
    return 0;
}

输入:

Enter a sentence: Hello World!

输出:

You entered: Hello World!



逐个解释C/C++中的输入/输出函数,说明它们的用法和特点:

  1. cout(C++):cout 是C++中的输出流对象,属于 iostream 库。它用于将格式化的输出发送到标准输出(通常是控制台)。您可以使用 << 运算符将数据发送到 cout

示例:

#include <iostream>

int main() {
    int num = 42;
    std::cout << "The number is: " << num << std::endl;
    return 0;
}

输出:

The number is: 42
  1. cout.put()(C++):cout.put() 用于向标准输出写入单个字符。这是 cout 对象的成员函数。

示例:

#include <iostream>

int main() {
    char ch = 'A';
    std::cout.put(ch);
    return 0;
}

输出:

A
  1. cin(C++):cin 是C++中的输入流对象,属于 iostream 库。它用于从标准输入(通常是控制台)读取输入。您可以使用 >> 运算符从 cin 中读取数据。

示例:

#include <iostream>

int main() {
    int num;
    std::cout << "Enter a number: ";
    std::cin >> num;
    std::cout << "You entered: " << num << std::endl;
    return 0;
}

输入:

Enter a number: 42

输出:

You entered: 42
  1. cin.get()(C++):cin.get() 用于从输入流中读取一个字符,包括空格和换行符。

示例:

#include <iostream>

int main() {
    char ch;
    std::cout << "Enter a character: ";
    ch = std::cin.get();
    std::cout << "You entered: " << ch << std::endl;
    return 0;
}

输入:

Enter a character: A

输出:

You entered: A
  1. cin.getline()(C++):cin.getline() 用于从输入流中读取一行字符串,可以包含空格,但不包括换行符。它需要两个参数:要读取的字符数组和最大读取字符数(包括空格)。

示例:

#include <iostream>

int main() {
    char str[100];
    std::cout << "Enter a sentence: ";
    std::cin.getline(str, 100);
    std::cout << "You entered: " << str << std::endl;
    return 0;
}

输入:

Enter a sentence: Hello World!

输出:

You entered: Hello World!
  1. getline()(C++):getline() 函数类似于 cin.getline(),但不属于输入输出流。它是C++标准库中的全局函数。getline() 用于从输入流中读取一行字符串,可以包含空格,但不包括换行符。它需要两个参数:输入流对象和字符串。

示例:

#include <iostream>
#include <string>

int main() {
    std::string str;
    std::cout << "Enter a sentence: ";
    std::getline(std::cin, str);
    std::cout << "You entered: " << str << std::endl;
    return 0;
}

输入:

Enter a sentence: Hello World!

输出:

You entered: Hello World!
  1. puts()(C):puts() 用于将一个以 null 结尾的字符串输出到标准输出,然后自动换行。

示例:

#include <stdio.h>

int main() {
    char str[] = "Hello World!";
    puts(str);
    return 0;
}

输出:

Hello World!
  1. putchar()(C):putchar() 用于将一个字符输出到标准输出。

示例:

#include <stdio.h>

int main() {
    char ch = 'A';
    putchar(ch);
    return 0;
}

输出:

A
  1. printf()(C):printf() 是C中用于格式化输出的函数。它可以输出各种数据类型,并允许使用格式化控制字符串来指定输出的格式。

示例:

#include <stdio.h>

int main() {
    int num = 42;
    printf("The number is: %d\\n", num);
    return 0;
}

输出:

The number is: 42
  1. gets()(C):gets() 用于从标准输入中读取一行字符串,它不检查输入是否超出缓冲区大小,因此可能导致缓冲区溢出问题。注意:gets() 已被标记为不安全,不建议在实际编程中使用。

示例:

#include <stdio.h>

int main() {
    char str[100];
    printf("Enter a sentence: ");
    gets(str);
    printf("You entered: %s\\n", str);
    return 0;
}

输入:

Enter a sentence: Hello World!

输出:

You entered: Hello World!
  1. getchar()(C):getchar() 用于从标准输入中读取一个字符。

示例:

#include <stdio.h>

int main() {
    char ch;
    printf("Enter a character: ");
    ch = getchar();
    printf("You entered: %c\\n", ch);
    return 0;
}

输入:

Enter a character: A

输出:

You entered: A
  1. scanf()(C):scanf() 是C中用于从标准输入读取数据的函数。它可以按照指定的格式从输入中读取数据,并将其存储在变量中。

示例:

#include <stdio.h>

int main() {
    int num;
    printf("Enter a number: ");
    scanf("%d", &num);
    printf("You entered: %d\\n", num);
    return 0;
}

输入:

Enter a number: 42


野牛程序员教少儿编程与信息学奥赛-微信|电话:15892516892
野牛程序员教少儿编程与信息学竞赛-微信|电话:15892516892
相关推荐

最新推荐

热门点击