本文介绍了scanf(“%C”,& opt)和scanf(“%C”,& opt)之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在IDE TurboC3中执行我的c语言程序。
非工作代码如下
I am executing my c language program in IDE TurboC3.
Non working code is as follows
#include<stdio.h>
#include<conio.h>
void main()
{
int i, j, t;
char opt;
clrscr();
printf("Enter two values" );
scanf("%d", &i);
scanf("%d", &j);
printf("\n\n press + for sum\n");
printf("press * for mult\n");
printf("print - for subtract\n\n");
scanf("%c", &opt); // code that is creating problem //
switch(opt)
{
case '+':
{t=i+j;
printf("\nsum =%d", t);
}
break;
case '*':
{t=i*j;
printf("\nproduct =%d", t);
}
break;
case '-':
{t=i-j;
printf("\ndifference =%d", t);
}
break;
default:
printf("\ninvalid choice");
}
getch();
}
代码块已更正,缩进添加 - OriginalGriff [/ edit]
我尝试了什么:
帮助我执行代码的更正如下
[edit]Code block corrected, indentation added - OriginalGriff[/edit]
What I have tried:
The correction that helped me execute the code is as follows
scanf(" %c", &opt); // code without problem //
即我在和%c之间插入了一个空格和代码工作。
请帮我理解其中的区别。
i.e. i have inserted a space between " and %c and the code worked.
Please help me understand the difference.
推荐答案
这篇关于scanf(“%C”,& opt)和scanf(“%C”,& opt)之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!