Home > database >  C language (this problem can be very simple, but I really don't understand.)
C language (this problem can be very simple, but I really don't understand.)

Time:09-17

# include
Int main () {
Int a, b;
int c;
Char q;
Printf (" please enter the two Numbers: ");
The scanf (" % d % d ", & amp; A, & amp; B);
Printf (" please enter the operator: ");
The scanf (" % c ", & amp; Q);
If (q=='+') c=a + b;
If (q=='-') c=a - b;
If (q=='*') c=a * b;
If (q=='/' & amp; & B! C=a/b=0);
Printf (" the result is: % d ", c);

} when I was in the output after the output and input the first words I want to enter two Numbers, I only after the output of a symbol, it will output the second sentence, and the second sentence that place can't input symbols and has no operation,
Why is this? O great god answers,

CodePudding user response:

You enter two characters then press enter should show the second sentence then you input operator

CodePudding user response:

I am after enter it won't appear the second sentence, then I only after enter an operator to press enter again, he will output a word, and he won't operation but direct output last printf

CodePudding user response:

Two Numbers you enter a space separated

CodePudding user response:

Of course there will be,

CodePudding user response:

reference 4 floor weixin_46537264 response:
will, of course, yes,

That you try the scanf (" % d % d ! ", & amp; A, & amp; B); Remove the Spaces behind and have a look

CodePudding user response:

Ok, thank you

CodePudding user response:

Is very simple, the original poster even forget the enter key is also generates a character (\ n), recommend two kinds of solution to the problem: first, put the character input in front; Second, add an empty buffer after input digital fflush (stdin),

CodePudding user response:

To be honest, I have a big a freshman, you said I could not understand

CodePudding user response:

Two % d next to the scanf inside double quotes, what bits and pieces, don't add, should be the normal output

CodePudding user response:

Behind the scanf add a getchar () a buffer try enter

CodePudding user response:

Printf () function put you input the contents of the output buffer, when after the output buffer is full, or take the initiative to refresh buffer to the content of the output to the screen, you're the printf () to add a \ n, make him finish every time the output from the buffer immediately written to the screen to go,

CodePudding user response:

You can think so, often can be used in our program printf () function for data output, the printf () function to get the content output, will be within the function calls the output of system calls, just like going to the shops, to buy a bag of snacks in the morning, at noon to buy a packet of noodles, again in the evening to buy a packet of biscuits, then it gradually waste a lot of time on the road, so why not one-time buy back, all the same, the function is called system calls, from our state of the user to switch to the kernel mode will produce consumption, in order to avoid this kind of meaningless consumption we can keep the contents of the output is a place called the output buffer, when we need output, one-time all output, save money, so we use printf () function, usually written as printf (" XXXX \ n "), immediately make the data from the output buffer to the screen, or use the fflush (stdout) of mandatory refresh, among them, the stdout is the standard output,

CodePudding user response:

my name is RT reference 13 floor response:
you can think so, often can be used in our program printf () function for data output, the printf () function to get the content output, will be within the function calls the output of system calls, just like going to the shops, to buy a bag of snacks in the morning, at noon to buy a packet of noodles, again in the evening to buy a packet of biscuits, then it gradually waste a lot of time on the road, so why not one-time buy back, all the same, the function is called system calls, from our state of the user to switch to the kernel mode will produce consumption, in order to avoid this kind of meaningless consumption we can keep the contents of the output is a place called the output buffer, when we need output, one-time all output, save money, so we use printf () function, usually written as printf (" XXXX \ n "), immediately make the data from the output buffer to the screen, or use the fflush (stdout) of mandatory refresh, among them, the stdout is the standard output,

Got it, got it, thank you bosses

CodePudding user response:

引用 14 楼 weixin_46537264的回复:
Quote: 引用 13 楼 我叫RT的回复:
你可以这样想,在我们编写的程序中会经常用到printf()函数进行数据的输出,而printf()函数要想把内容输出,就要在该函数内部调用有关输出的系统调用,就想你去商店买东西,早上去买包零食,中午去买包泡面,晚上又去买包饼干,那么这一来二去在路上浪费了不少时间,那么何不一次性全买回来呢,同样的,在函数中调用系统调用时,从我们的用户状态切换到内核态也要产生消耗,为了避免这种无意义的消耗我们可以把输出的内容存在一个名为输出缓冲区的地方,当我们需要输出时,一次性全部输出,节省开销,所以我们使用printf()函数时,一般都会写成printf("xxxx \n") ,让数据立刻从输出缓冲区刷新到屏幕,或者使用fflush(stdout)进行强制刷新, 其中,stdout就是标准输出,

Got it, got it, thank you bosses

Young man, do you have in this the emergence of this phenomenon is because of you the scanf inside the two d % back with the Spaces, you remove them, program is normal, with other it doesn't matter, his hands-on computer try more

CodePudding user response:

That what, uncle, I only in the first space after the first % d, without a second behind,

CodePudding user response:

The problem of input format
  • Related