C / C ++ function of the keyboard input (scanf, gets, getchar, getline, cin, cin.get ())

1. scanf function

 scanf () may receive a variety of data formats, encounters a carriage return ends Tab, when a space, an input;

 scanf () to retain the carriage return, when two consecutive calls scanf, directly reading carriage return "\ n" (0x0a) at the upper end of the last scanf; Enter key will not shield

#include <iostream>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

using namespace std;


int main(int argc, char** argv)
{
    char buf[100]; scanf("%s", buf); printf("first input:%s\n", buf); char ret; scanf("%c", &ret); printf("second input:0x%x\n", ret); return 0; }

After performing:

test
first input:test
second input:0xa

Scanf again when a carriage return is read out ( '\ n') (0x0a);

 

When the input character string with spaces:

test space // input string with spaces 
First the INPUT: the Test 
SECOND, the INPUT: Space

At this time, prompted only once, when the second performance scanf string read directly after the space;

 

2.gets()

   Function prototype: char * gets (char * string)

   1. scanf and similar, but encountered a space, Tab, the end will not write, write only when faced with the end of the carriage;

   2. When the Enter key shield, calling scanf function again, does not read the Enter key

#include <iostream>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

using namespace std;


int main(int argc, char** argv)
{
    char buf[100]; gets(buf); printf("first input:%s\n", buf); char test; scanf("%c", &test); printf("second input:0x%x\n", test); return 0; }

operation result:

tttt // string type without the spaces 
First INPUT: TTTT 
yyyyy 
SECOND INPUT: 0x79 // At this time not 0x0a, again showing the read character is not a carriage return, 
SECOND INPUT: Y

 

Type the string operating results with spaces:

tttt yyyy // type spaces string 
first input: tttt yyyy // print properly 
uuuuuu 
SECOND, the INPUT: 0x75 // gets blocked due to the Enter key, resulting in acquired here is not "\ the n-" 
SECOND, the INPUT: U

 

3.getchar()

    Returns only the first character typed, not block carriage;

 Type the characters greater than 1, getting again, it will continue to read the rest of the characters;

#include <iostream>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

using namespace std;

int main(int argc, char** argv)
{
    char test; test = getchar(); printf("first input:%c\n", test); test = getchar(); printf("second input:0x%x\n", test); return 0; }

Type a character, the results:

t // type a character, when getchar again, is read into the carriage; 
First INPUT: T 
SECOND INPUT: 0xA

As you type more characters, the results:

TYT 
First INPUT: T 
SECOND INPUT: 0x79 // when typing multiple characters, when getchar again, read directly into the remaining characters; 
SECOND INPUT: Y

These are input function in C language;

---------------------------------------- The following is a C ++ type the function ----- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- ---------------------------------------

1. gin

 Like scanf and encountered a space, Tab, Enter both ends;

 

 

Guess you like

Origin www.cnblogs.com/weiyouqing/p/12554915.html