不能在同一程序中使用char和integer [英] cannot use char and integer in the same program
问题描述
大家好吧,
我碰到了很多东西。
这个非常简单的代码,但是由于某种原因,程序很奇怪
这是我的代码:
#include< stdio.h>
#include< conio.h>
void main(void)
{
int a,c;
char b;
clrscr();
scanf("%d"& a);
scanf("%c",b);
scanf("%d" ,& c);< =它不停在这里,编程结束
}
可能是什么问题?
ya ****** @ gmail.com 写道:
大家好,
我碰到了很多的东西
很简单代码,但由于某种原因,该程序表现得很奇怪
这是我的代码:
#include< stdio.h>
#include< conio .h>
void main(void)
int main(void)
{
int a,c;
char b;
clrscr();
scanf("%d",& a);
scanf("%c",b);
scanf("%c",& b);
scanf("%) d",& c);< =它不停在这里,编程结束
}
可能是什么问题?
-
Ian Collins。
< ya ** ****@gmail.com写在消息中
新闻:11 ********************* @ p79g2000cwp.googlegro ups.com。 ..
大家好,
我碰到了很多东西
它非常简单的代码但由于某种原因,该程序表现得很奇怪
这是我的代码:
#include< stdio.h>
#include< conio。 h取代;
非标准标题(conio.h)
void main(void)
这应该是
int main(无效)
{
int a,c;
char b;
clrscr();
scanf("%d",& a) ;
scanf("%c",b);
我认为你的意思是& b。此外,您发现获取用户输入
并不容易。我建议编写一个包装函数,将所有内容读成字符串,然后尝试将内容解析为有意义的内容。
Allan
< br>
ya******@gmail.com 写道:
我碰到了极其怪异的
它非常简单的代码但是由于某种原因程序很奇怪
这是我的代码:
#include< stdio.h>
#include< conio.h>
这是一个非标准的标题。
void main(void)
int main(void)
{
int a,c;
char b;
clrscr();
这是一个非标准函数。为什么要清除屏幕
呢?这很粗鲁。
scanf("%d",& a);
Ok。
scanf("%c",b);
"%c"告诉scanf期待一个char *。你传给它一个字符(
被提升为int),以及一个未初始化的字符。未定义
行为。
scanf("%d",& c);< =它不停在这里,编程结束
返回0;
}
什么可以是问题?
我不知道。解决我已经指出的问题,然后再试一次。如果
你还有问题,请再次发帖。
-
Keith Thompson(The_Other_Keith) ks *** @ mib.org < http://www.ghoti.net/~kst>
San迭戈超级计算机中心< *< http://users.sdsc.edu/~kst>
我们必须做点什么。这是事情。因此,我们必须这样做。
hi all,
i''m bumping into smething very starnge
its very simple code but from some reason the program is acting strange
this is my code:
#include<stdio.h>
#include<conio.h>
void main(void)
{
int a,c;
char b;
clrscr();
scanf("%d",&a);
scanf("%c",b);
scanf("%d",&c);<=its not stopping here,the programming is ending
}
what can be the problem?
ya******@gmail.com wrote:hi all,
i''m bumping into smething very starnge
its very simple code but from some reason the program is acting strange
this is my code:
#include<stdio.h>
#include<conio.h>
void main(void)int main(void)
{
int a,c;
char b;
clrscr();
scanf("%d",&a);
scanf("%c",b);scanf("%c",&b);
scanf("%d",&c);<=its not stopping here,the programming is ending
}
what can be the problem?
--
Ian Collins.
<ya******@gmail.comwrote in message
news:11*********************@p79g2000cwp.googlegro ups.com...hi all,
i''m bumping into smething very starnge
its very simple code but from some reason the program is acting strange
this is my code:
#include<stdio.h>
#include<conio.h>non standard header (conio.h)
void main(void)this should be
int main(void)
{
int a,c;
char b;
clrscr();
scanf("%d",&a);
scanf("%c",b);I think you mean &b here. Also, you are finding out that getting user input
is not easy. I suggest writing a wrapper function which reads everything
into a string, then you try and parse the contents to something meaningful.
Allan
ya******@gmail.com writes:i''m bumping into smething very starnge
its very simple code but from some reason the program is acting strange
this is my code:
#include<stdio.h>
#include<conio.h>This is a non-standard header.
void main(void)int main(void)
{
int a,c;
char b;
clrscr();This is a non-standard function. Why do you want to clear the screen
anyway? It''s rude.
scanf("%d",&a);Ok.
scanf("%c",b);"%c" tells scanf to expect a char*. You''re passing it a char (which
is promoted to int), and an uninitialized char at that. Undefined
behavior.
scanf("%d",&c);<=its not stopping here,the programming is endingreturn 0;
}
what can be the problem?I don''t know. Fix the problems I''ve pointed out and try again. If
you''re still having problems, post again.
--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
这篇关于不能在同一程序中使用char和integer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!