Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Good morning...

Reply
Thread Tools

Good morning...

 
 
prassu
Guest
Posts: n/a
 
      01-22-2007


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....

 
Reply With Quote
 
 
 
 
Jeff Mullen
Guest
Posts: n/a
 
      01-22-2007
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()
> {
> 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.
 
Reply With Quote
 
 
 
 
Kenny McCormack
Guest
Posts: n/a
 
      01-22-2007
In article <e58f5$45b46ac0$49d77f6$>,
Jeff Mullen <> wrote:
....
>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.

 
Reply With Quote
 
Mike Wahler
Guest
Posts: n/a
 
      01-22-2007

"prassu" <> wrote in message
news: ups.com...
>
>
> 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/?m...=crit_pjp.html


-Mike


 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      01-22-2007
Jeff Mullen said:
> prassu wrote:
>>
>> 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.


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.


--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      01-22-2007
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
good algorithms come with practice and reading good code/books? vlsidesign C Programming 26 01-02-2007 09:50 AM
Good slide scanning service vs. good slide scanner for Do-It-Yourself? LAshooter Digital Photography 0 06-25-2005 07:14 AM
Signs are good, but WAN no good =?Utf-8?B?bmV0bnV0?= Wireless Networking 2 08-21-2004 12:41 PM
JLO situation+ why fastglass is good+DSLR is good Hugo Drax Digital Photography 0 01-17-2004 11:41 PM
Not even a newbee. Good at school course. please advise good start sikka noel C++ 8 08-05-2003 06:43 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57