| |

VerySource

 Forgot password?
 Register
Search
View: 873|Reply: 7

After entering the string with Scanf, why should I treat the following carriage returns?

[Copy link]

1

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-2-19 20:30:01
| Show all posts |Read mode
In "C Programming", the following example is used when applying the two functions fputc and fgetc:
#include <stdio.h>
#include <stdlib.h>
void main ()
{
 FILE * fp;
 char ch, filename [10];
 scanf ("% s", filename);
 if ((fp = fopen (filename, "w")) == NULL)
   {
    printf ("Can't open file\n");
    exit (0);
   }
 ch = getchar (); / * This statement is used to receive the last carriage return when the Scanf statement is executed * /
 ch = getchar ();
 while (ch! = '#')
   {
    fputc (ch, fp);
    putchar (ch);
    ch = getchar ();
   }
 putchar (10);
 fclose (fp);
}
I omitted the other comments first, I don't understand why I still need to use this statement to receive this carriage return. The younger brother just learned a little, and always thought that the program was executed somewhere to wait for the user's response. The carriage return was a sign for the machine to receive the user's response. How did it become another character here? If it is a character, why is there no such sentence when using scanf elsewhere? Why don't functions like getchar need to receive carriage returns like this?
Waiting for the master's explanation, thanks in advance!
Reply

Use magic Report

0

Threads

10

Posts

11.00

Credits

Newbie

Rank: 1

Credits
11.00

 China

Post time: 2020-5-10 12:30:02
| Show all posts
When the scanf () function accepts data, it usually uses the enter key as the input to end a data (space bar, tab key is also OK), you will understand by giving an example
#include <stdio.h>
int main ()
{
int n1, n2;
scanf ("% d",&n1);
n2 = getchar ();
printf ("n1 =% d, n2 =% d", n1, n2);
}
When entering, please enter 12 and Enter, where n2 = 10, 10 is the decimal number of '\n'
If you enter 12 and space and Enter when entering, where n2 = 32. 32 is the decimal number of the space bar

The examples in the book are handled according to the general situation
Reply

Use magic Report

0

Threads

18

Posts

12.00

Credits

Newbie

Rank: 1

Credits
12.00

 China

Post time: 2020-5-14 17:30:02
| Show all posts
If you don't want to enter, we usually use it like this
         int n1, n2;
scanf ("% d",&n1);
getchar ();
             ...
Reply

Use magic Report

1

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

 Author| Post time: 2020-7-14 15:15:01
| Show all posts
First of all, I thank the 2 masters upstairs!

  To discuss this issue with a friend today, he said that the carriage return character will be stored in the keyboard buffer. I still don’t understand, is it only the scanf function or is it the case with getchar() and gets()? For example, if I have a statement written

ch=getchar();

Then, after entering the characters, you still have to press Enter. Is there any carriage return in the buffer at this time? why?
Reply

Use magic Report

0

Threads

10

Posts

11.00

Credits

Newbie

Rank: 1

Credits
11.00

 China

Post time: 2020-7-15 23:30:02
| Show all posts
This is true for scanf and getchar. Don't use gets for it
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-7-19 21:30:01
| Show all posts
int n1,n2;
scanf("%d",&n1);
getchar();

Hey, this is Xiaoxi Mi, our teacher told us to use it
Reply

Use magic Report

1

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

 Author| Post time: 2020-7-29 18:15:01
| Show all posts
Khan's friend's teacher upstairs, why don't you use gets()?
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-7-29 18:45:01
| Show all posts
When you have keyboard input, the IO interrupt will process your input, and the keyboard driver will buffer it in the keyboard memory
Save your keys in Scanf, so when you use Scanf, the first input number is transferred to the memory of the second parameter of Scanf
At this time, there is still a carriage return in the keyboard buffer, so if you use getchar, the character you get is a carriage return.
After meeting, filter out this carriage return and call getchar once to clear the keyboard buffer.
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list