C/c++-style Input/Output function

Source: Internet
Author: User
Tags deprecated terminates

C-style input and output

(1) int getchar () and int putchar (int c)

GetChar reads characters from the stdin input stream and can read only one character at a time. If you want to read multiple characters at once, you can put them in a loop. GetChar reads all types of characters, including spaces.

Putchar prints a character to the screen and prints only one character at a time. For example:

Char A;
while ((A=getchar ()) = ' E ') # #输入流中是一长串的窗口输入, GetChar () reads the characters from the input stream, writes to a, and terminates the loop when the character E is encountered.
Putchar (a); # # The place is also available in printf ("%c", a);

(2) char* gets (char *s) and int puts (const char* s)

The char* gets (char *s) function reads a line from stdin to the cache space that s points to, a new line or EOF symbol as the end of a read.

For example:

Char str[100];

Gets (str);

printf ("\ n you entered:");

Puts (str);

Get (str) can also be used in loops as a criterion. The Std::gets function is also used in C + +, which is deprecated (deprecated, still available) in c++11 and is completely removed from the c++14.

(3) int scanf (const char* format, ...) and int printf (const char* format, ...)

Format can be%s (string),%d (integer),%c (character),%f (floating point), and so on.

SCANF can ignore spaces. scanf ("%d", &a); Cascade Input Mode:

int A, B;

scanf ("%d%d", &a, &b);

This input method can also be used as a criterion for looping, while (scanf ("%d%d", &a, &b)) and the following C + + style input std::cin type, the input data type and the variable declaration type is different, the scanf function returns 0, the loop terminates.

######## #C + + style input and output ###########

The input method is std::cin>> A, and can also be std::cin>>a>>b by cascading input. This input method can ignore spaces.

It is important to note that the resulting input values are very much related to the type of A/b declaration, for example:

int A;

Char b;

cin>>a>>b;

After compiling, load the row, enter 29, 29. Then eventually get the a=29, while the b=2. That is, a is defined as an arithmetic type int,29 input to a, B is defined as a character type, and only the first character 2 is read.

Also pay attention to the return type of cin>>a, for example:

while (Cin>>a)

When the input type is inconsistent with the declaration type of a, Cin>>a returns 0 and jumps out of the while loop. Returns a non-0 value when the input type is consistent, and the loop continues.

Output mode is std::cout<< c<< Endl. Endl for the line, the use is very simple, here do not repeat.

C/c++-style Input/Output function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.