这些产出有何不同? [英] How these outputs are different ?

查看:66
本文介绍了这些产出有何不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在c中的简单乘法程序中,如果我们想要用户输入的数字乘以,我们接受scanf的值(%d%d,& a,& b);然后程序在第1行和第2行询问输入并在第3行获取输出。但是为什么我使用scanf(%d%d \ n,& a,& b); ,它要求3次输入并给出前两个数字的第4行输出。如果是scanf(%d%d \ nn \ nn \ nn \ n \\ n,& a,& b); ,给出相同的结果,为什么它不需要那么多的输入数量,因为我们添加了更多的\ n,它仍然需要三个输入,而在第四行产生前两个数字的输出?



即时通讯使用



In a simple multiplication program in c, if we want to number which are entered by the user to get multiplied, we accept the values by scanf("%d%d",&a,&b); and then program asks input in 1st and 2nd line and gets the output in the third line. But why if I use scanf("%d%d\n",&a,&b); , it ask 3times input and gives 4th line output of 1st two numbers. Also if scanf ("%d%d\n\n\n\n\n\n",&a,&b); , give the same result, why it doesn't take as many numbers of input as we added more \n, it still takes three inputs and in 4th line produces the output of first two numbers?

im using

#include<stdio.h>
int main()
{
int a,b,result;
printf("enter two numbers\n");
scanf("%d%d",&a,&b);
result=a*b;
printf("%d",result);
return 0;
}



第二次尝试




2nd attempt

#include<stdio.h>
int main()
{
int a,b,result;
printf("enter two numbers\n");
scanf("%d%d\n",&a,&b);
result=a*b;
printf("%d",result);
return 0;
}



第3次尝试




3rd attempt

#include<stdio.h>
int main()
{
int a,b,result;
printf("enter two numbers\n");
scanf("%d%d\n\n\n\n\n",&a,&b);
result=a*b;
printf("%d",result);
return 0;
}





第二次和第三次尝试都有类似的结果



我尝试过:



第一次尝试





both second and third attempts have similar results

What I have tried:

1st attempt

#include<stdio.h>
int main()
{
int a,b,result;
printf("enter two numbers\n");
scanf("%d%d",&a,&b);
result=a*b;
printf("%d",result);
return 0;
}



第二次尝试




2nd attempt

#include<stdio.h>
int main()
{
int a,b,result;
printf("enter two numbers\n");
scanf("%d%d\n",&a,&b);
result=a*b;
printf("%d",result);
return 0;
}



第3次尝试




3rd attempt

#include<stdio.h>
int main()
{
int a,b,result;
printf("enter two numbers\n");
scanf("%d%d\n\n\n\n\n",&a,&b);
result=a*b;
printf("%d",result);
return 0;
}

推荐答案

参见格式规范字段:scanf和wscanf函数 [ ^ ]。


Quote:

scanf(% d%d,& a,& b);

scanf("%d%d",&a,&b);



假设用户输入


Suppose the user enters

1234568



穷人 scanf 函数如何检测第一个数字的结束点(从而开始第二个数字?

我不能。

你不能,可以吗?





你必须使用两个输入数字之间有一个分隔符。一个空白,如 Rick York 建议的那样。例如,试试:


How can the poor scanf function detect the ending point of the first number (and thus the starting of the second number?
I cannot.
You cannot, can you?


You have to use a separator between the two input numbers. A blank, as Rick York suggested would do the trick. Try, for instance:

#include <stdio.h>


int main()
{
  int a,b,result;
  printf("enter two numbers\n");
  if (scanf("%d %d",&a,&b) == 2)
  {
    result=a*b;
    printf("%d\n",result);
  }
  return 0;
}


这篇关于这些产出有何不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆