Problems of several input and output functions of C language

Source: Internet
Author: User

1 scanf () GetChar ()

1 #include <stdio.h>2     intMain ()3     {4  Charch1, CH2;5scanf"%c", &ch1); 6scanf"%c", &CH2);7printf"%d%d\n", CH1, CH2);8  return 0;9     }Ten     One  A #include <stdio.h> -     intMain () -     { the  Charch1, CH2; -CH1 =GetChar (); -CH2 =GetChar (); -printf"%d%d\n", CH1, CH2); +  return 0; -}

Function 1

The two programs run the same effect, for example, I enter a 1, just give me 49 10, no wait for me to enter a second character.

Reason: scanf and GetChar will take the carriage return as a character to read out and 10 is just the return of the ASC code

Improved method:

#include <stdio.h>intMain () {Charch1, CH2; scanf ("%c", &ch1);  while(GetChar ()! ='\ n');//Empty input buffersscanf"%c", &CH2); printf ("%d%d\n", CH1, CH2); return 0; } #include<stdio.h>intMain () {Charch1, CH2; CH1=GetChar (); while(GetChar ()! ='\ n'); CH2=GetChar (); printf ("%d%d\n", CH1, CH2); return 0; }

Function 2

After the improvement, you can enter 2 characters normally and then print out their ASC code.

Let's talk about scanf and GetChar.

or function 1 when we enter a few spaces, then enter a 1, and finally press ENTER, print out is 2 (scanf and getchar the same)

Try it again. function 2 When we enter a few spaces from the keyboard and enter a 1, then press ENTER, then enter a 2 and then press ENTER to enter the last input of 32 a 50

In summary, we enter from the keyboard to end with a carriage return, and scanf or GetChar is read from the input buffer, is the carriage return Space tab to end.

Next look at the scanf and gets input string

1#include <stdio.h>2 3 intMain ()4     {5  Charstr1[ -], str2[ -];6scanf"%s", STR1); 7printf"%s\n", STR1); 8scanf"%s", STR2); 9printf"%s\n", STR2); Ten  return 0; One     } A  -Procedure 3

Test run: 1 when we enter 123 and then enter a number of spaces at random and then enter a 12 and finally press ENTER

The result is a direct output of 123 and 12.

2 When we enter 123, then enter a few tab keys (the number of random) and then enter a 12 and finally press ENTER

As a result, we entered 123 and 12 directly.

Summary 1:scanf encounters a space or TAB key to the end of the input, that is, a space or a tab behind the things he can't read

2:SCANF ignores the opening spaces and tabs each time the data is read

Or program 3 when we enter 123, press ENTER, he will directly output 12 and then enter 12, enter, he will output 12

Summary: scanf encountered a carriage return directly ended, the above program executed 2 times from the keyboard read the string, indicating that the first read from the keyboard carriage return is discarded,

That is, scanf reads the string and discards the last carriage return.

Next talk about gets.

Obviously when you want to enter a string with a space or TAB key, it's not appropriate to use scanf

1 intMain ()2 {3  Charstr1[ -], str2[ -];4 gets (str1);5printf"%s\n", STR1); 6 gets (STR2); 7printf"%s\n", STR2); 8  return 0;9 }Ten  OneProcedure 4

Test:
Hello world! Input
Hello world! Output
12345 [input]
12345 [Output]

Finally, to say the difference between printf and puts

1  intMain ()2      {3    Charstr1[ -], str2[ -];4scanf"%s", STR1); 5printf"%s", STR1); 6scanf"%s", STR2); 7printf"%s", STR2); 8   return 0;9      }Ten  One Test 1 A  -  - intMain () the      { -    Charstr1[ -], str2[ -]; -scanf"%s", STR1);  -printf"%s\n", STR1);  +scanf"%s", STR2);  -printf"%s\n", STR2);  +   return 0; A      } at Test 2 -  -  - intMain () -       { -    Charstr1[ -], str2[ -]; inscanf"%s", STR1);  - puts (STR1);  toscanf"%s", STR2);  + puts (STR2);  -   return 0; the      } *Test 3

Yourselves enter a QW and then output a QW

Re-enter WW DC output WW DC

When both encounters a space, the tab stops and ends with ' \ n '.

The only difference is that the puts will be wrapped automatically, and if you want to wrap it, you must add a line break ' \ n '

End

Questions about several input and output functions for C language

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.