Scanf statement usage record, scanf statement usage

Source: Internet
Author: User

Scanf statement usage record, scanf statement usage

--- Restore content start ---

Format Character Description

% D input Integer Data

% F Input Single-precision floating point data

% Lf Double Precision Floating Point Data (float type available)

% C enter a character

% S enter a string of characters

% O input an octal integer

% Enter % characters

 

(1) Use of % d
1 #include "stdio.h" 2 int main(void) 3 { 4     int a,b,c; 5     scanf("%d%d%d",&a,&b,&c); 6     printf("%d,%d,%d/n",a,b,c);7     return 0;8 }  

Input Format:

Abc, a B c, etc.

Description: Space, tab, and newline characters can be added to letters a, B, and c.

1 #include"stdio.h" 2 int main(void) 3 { 4     int a,b,c;5     scanf("%d,%d,%d",&a,&b,&c);6     printf("%d,%d,%d/n",a,b,c);7     return 0;8 }  

Input Format:

A, B, c or a, B, c, etc.

Description: The rule is basically the same as in the previous example, but note that a "," must be added after each letter ",".

1 #include "stdio.h" 2 int main(void) 3 { 4     int a,b,c;5     scanf("%d    %d%d",&a,&b,&c);6     printf("%d,%d,%d/n",a,b,c);7     return 0;8 }  

Input Format:

Abc, a B c, etc.

Description: Adding space and tab to % d and % d in the scanf statement is equivalent to % d.

(2) Use of % c

Note: % C can read space, tab, and newline.

1 #include<stdio.h>2 int main()3 {4     char a, b, c;5     scanf("%c%c%c", &a, &b, &c);6     printf("%c%c%c", a, b, c);7     return 0;8 }

Input Format:

Abc

Description: The input data cannot contain any characters including space, tab, or newline.

1 #include<stdio.h>2 int main()3 {4     char a, b, c;5     scanf("%c %c%c", &a, &b, &c);6     printf("%c%c%c", a, b, c);7     return 0;8 }

Input Format:

A bc or

Bc

Description: If tab or space is added between % c and % c in the scanf statement, space, tab, newline (between % d and % c) can be added during input ).

1 #include<stdio.h>2 int main()3 {4     char a, b, c;5     scanf("%c,%c%c", &a, &b, &c);6     printf("%c%c%c", a, b, c);7     return 0;8 }

Input Format:

A, bc

Description: All characters except tab, space, and newling must be input. Otherwise, garbled characters may occur.

(3) Use of % s

Note: % S cannot read space, tab, or newline.

1 #include<stdio.h>2 int main()3 {4     char a[10],b[10];5     scanf("%s %s",a,b);6     printf("%s%s", a,b);7     return 0;8 }

Input Format:

I You or I

You

Description: % S cannot read space, tab, newline, and % s. The three characters entered between % s and % s are the same as % s.

Note: % S and % s do not add any characters except space and tab. To add characters, use space or tab interval.

Example: % S

% S

 

1 #include<stdio.h>2 int main()3 {4     char a[10], b[10], c;5     scanf("%s %c%s",a,&c,b);6     printf("%s%s%c", a,b,c);7     return 0;8 }

Input Format:

A B c or a bc or

Bc

Description: % C is similar to % s. When % s is input, a tab, space, or newline can be added. You can also enter a string of characters (the first character of this string is given to c, the remaining characters are given to B ). If no tab or space exists between % s and % c, c can only get '\ n'. If yes, tab, space, and newline can be added when input.

Additional note: % S and % d are mixed. You can use space, tab, or newline to separate data during input, but they cannot be separated.

--- Restore content end ---

Related Article

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.