Simple differences between%s and%c in scanf functions

Source: Internet
Author: User

first of all: after typing characters in the keyboard, the character Inode is first saved in the keyboard buffer for the SCANF function to read (Scanf,getchar and other functions are read buffers, the Getch function is the console information read, that is, read directly from the keyboard). Also pay special attention to the keyboard type enter the actual "\ r \ n", but when stored in the buffer will discard ' \ R ' and only store \ n. For example, we type the character "12,345 return", the buffer is stored in "12345\n".

"_" in the article stands for Space

Code:

Char *s;

scanf ("%s", s);

printf ("%s\n", s);

scanf ("%s", s);

printf ("%s\n", s);

Cases:

Input: 123_345 Carriage return "_" for space

Output: 123

345

Analysis: For scanf in the%s format, "Space, newline, tab," and so on as two data separators exist, that is, before and after the delimiter is two data, read the delimiter is not read in.

Cases:

Input: 123 return car

456 return car

Output: 123

456

Cause: As in case 1, enter 123 back to the car, buffer memory is 123\n, after the first read, the buffer only left \ n, when the second read, \ n is a delimiter exists, do not read into the second time, so there is no data in the buffer, so the need to continue to enter data.

Cases:

Enter: _ Return "_" for space

This input will require input all the time, because the space and \ n are delimiters, there is no data, so there is no way to read. The same direct input carriage is always required input


Code:

Char *s;

scanf ("%s", s);

printf ("%s\n", s);

scanf ("%c", s);

printf ("%d\n", *s);

Cases:

Input: 1234_12 Carriage return "_" for space

Output: 1234

32 (ASCII of space is 32)

Cause: The delimiter "space" is encountered when reading the first%s, but the space is still stored in the buffer. For%c read-in, "white space, \ n, tab" and so on are normal characters, so the second read in the buffer to read the first character "space", so the output is 32.


Case 2:

Input: 123 return car

Output: 123

(\ n ASCII is 10)

Reason:%s read-in buffer middle finger is left \ nthe second time to read in%c when reading into \ n, the output is 10.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Simple differences between%s and%c in scanf functions

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.