Jeff Mullen <> writes:
> prassu wrote:
> > This is prasad. I started learning C just now. Thats why my Questions
> > may be silly . please don`t mind and help me to learn C in a good way.
> > i tried the following in Vi editor..
> > My questions are
> > main()
Should be
int main(void)
> > {
> > char a;
> > printf("enter the char\n");
> > scanf("%c",&a);
> > printf("prasad is %c\n",a);
> > }
> > o/p : enter the char
> > 2
> > prasad is 2
> > no doubt for the above
> >
>
> You may wish to declare a as an int. It will be promoted to int
> when it is place on the stack in the call to scanf anyway, and
> then again in the call to printf.
scanf's "%c" format expects a pointer to char; you *can't* pass it a
pointer to int.
printf's "%c" expects a value of type int, but a char value (in this
particular context) is promoted to int anyway.
> Furthermore, there is a
> "character constant," EOF, that a char cannot represent, that
> is often used in character-based I/O routines. Though, to
> my knowledge, scanf() doesn't, getchar() does.
Right, you need an int to hold the result of getchar(); that doesn't
apply to scanf().
[...]
--
Keith Thompson (The_Other_Keith)
kst- <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.