Share getchar c++

lycatchrysler

New member
## Getchar C ++

** GetChar trong C ++ ** là một hàm đọc một ký tự duy nhất từ luồng đầu vào tiêu chuẩn.Nó được khai báo trong tệp tiêu đề `<cstdio>`.Cú pháp của hàm `getchar ()` như sau:

`` `C ++
int getchar ();
`` `

Hàm `getchar ()` trả về ký tự tiếp theo từ luồng đầu vào tiêu chuẩn dưới dạng giá trị số nguyên.Giá trị trả về là mã ASCII của ký tự.Ví dụ: nếu ký tự tiếp theo từ luồng đầu vào tiêu chuẩn là chữ cái `a`, hàm` getchar () `sẽ trả về giá trị số nguyên 65.

Hàm `getchar ()` là một hàm chặn, có nghĩa là nó sẽ đợi cho đến khi một ký tự có sẵn từ luồng đầu vào tiêu chuẩn trước khi quay lại.Nếu không có ký tự có sẵn từ luồng đầu vào tiêu chuẩn, hàm `getchar ()` sẽ chặn cho đến khi nhập ký tự.

Hàm `getchar ()` có thể được sử dụng để đọc một ký tự duy nhất từ luồng đầu vào tiêu chuẩn.Nó có thể được sử dụng để đọc đầu vào của người dùng hoặc để đọc dữ liệu từ một tệp.

**Ví dụ**

Mã sau đây cho thấy cách sử dụng hàm `getchar ()` để đọc một ký tự duy nhất từ luồng đầu vào tiêu chuẩn:

`` `C ++
#include <Istream>

int main () {
// Đọc một ký tự từ luồng đầu vào tiêu chuẩn.
char c = getchar ();

// In ký tự vào luồng đầu ra tiêu chuẩn.
std :: cout << c << std :: endl;

trả lại 0;
}
`` `

Khi mã này được biên dịch và thực thi, nó sẽ nhắc người dùng nhập ký tự.Khi người dùng nhập một ký tự, mã sẽ in ký tự vào luồng đầu ra tiêu chuẩn.

** hashtags **

* #C ++
* #Đầu vào
* #Tính cách
*
* Chức năng #Blocking
=======================================
## Getchar C++

**Getchar in C++** is a function that reads a single character from the standard input stream. It is declared in the header file `<cstdio>`. The syntax of the `getchar()` function is as follows:

```c++
int getchar();
```

The `getchar()` function returns the next character from the standard input stream as an integer value. The returned value is the ASCII code of the character. For example, if the next character from the standard input stream is the letter `A`, the `getchar()` function will return the integer value 65.

The `getchar()` function is a blocking function, which means that it will wait until a character is available from the standard input stream before returning. If there is no character available from the standard input stream, the `getchar()` function will block until a character is entered.

The `getchar()` function can be used to read a single character from the standard input stream. It can be used to read user input, or to read data from a file.

**Example**

The following code shows how to use the `getchar()` function to read a single character from the standard input stream:

```c++
#include <iostream>

int main() {
// Read a character from the standard input stream.
char c = getchar();

// Print the character to the standard output stream.
std::cout << c << std::endl;

return 0;
}
```

When this code is compiled and executed, it will prompt the user to enter a character. When the user enters a character, the code will print the character to the standard output stream.

**Hashtags**

* #C++
* #input
* #Character
* #standard input stream
* #Blocking function
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top