Good morning...

P

prassu

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()
{
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


main()
{
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
if we give space(s) before %c it is printing as in first case why?
main()
{
char a;
printf("enter the char\n");
scanf("%c ",&a);
printf("prasad is %c\n",a);
}

o/p : enter the char
2

if we give space(s) after %c it is not printing any thing , to come out
of that program we have to press ctrl+z
why?

main()
{
char a;
printf("enter the char\n");
scanf("% c",&a);
printf("prasad is %c\n",a);
}

o/p : enter the char
2
prasad is
if we give a space between % and c it is printing as above why?

thanking you in advance

bye....
 
J

Jeff Mullen

prassu said:
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()
{
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. 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.
main()
{
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
if we give space(s) before %c it is printing as in first case why?

Check books like "The C Programming Language." Unfortunately, I only
have the first edition of the Kernighhan and Ritchie book available.
In that book, the relevant information is found on page 147.

On page 148, the first edition clearly states that blanks, tabs and
newlines in a control string are ignored. Thus, this is standard
behavior.
main()
{
char a;
printf("enter the char\n");
scanf("%c ",&a);
printf("prasad is %c\n",a);
}

o/p : enter the char
2

if we give space(s) after %c it is not printing any thing , to come out
of that program we have to press ctrl+z
why?

Did you try typing a space after the 2?
main()
{
char a;
printf("enter the char\n");
scanf("% c",&a);
printf("prasad is %c\n",a);
}

o/p : enter the char
2
prasad is
if we give a space between % and c it is printing as above why?

Because "% " is not a valid conversion specification.
thanking you in advance

bye....

You're welcome in advance.

Take care.
 
K

Kenny McCormack

Jeff Mullen said:
when it is place on the stack in the call to scanf anyway, and

You're not allowed to say the word "stack" in this newsgroup.

The regs will be all over you like Cubans on Miami.
 
M

Mike Wahler

prassu said:
This is prasad. I started learning C just now.

Which book(s) are you using? Your code indicates
it/they are teaching a prestandard, obsolete form
of C.

For answers to your questions, see the link cited below.
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()
{
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


main()
{
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
if we give space(s) before %c it is printing as in first case why?
main()
{
char a;
printf("enter the char\n");
scanf("%c ",&a);
printf("prasad is %c\n",a);
}

o/p : enter the char
2

if we give space(s) after %c it is not printing any thing , to come out
of that program we have to press ctrl+z
why?

main()
{
char a;
printf("enter the char\n");
scanf("% c",&a);
printf("prasad is %c\n",a);
}

o/p : enter the char
2
prasad is
if we give a space between % and c it is printing as above why?

See: http://tinyurl.com/2xt638
(from "Dinkum Compleat Libraries Reference")

Note: Please read and observe Dinkumware's copyright notice at:
http://www.dinkumware.com/manuals/?manual=compleat&page=crit_pjp.html


-Mike
 
R

Richard Heathfield

Jeff Mullen said:
You may wish to declare a as an int.

No need for that - in fact it's probably a bad idea in this case (although,
as you note, it is essential for getchar). But he definitely needs
<stdio.h> if he's going to call printf or scanf.
 
K

Keith Thompson

Should be

int main(void)
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().

[...]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top