Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > scanf behaviour

Reply
Thread Tools

scanf behaviour

 
 
Mohd Azhar Hussain
Guest
Posts: n/a
 
      05-15-2007

I am new to C. I was trying to read input from key board using scanf,
here's my program


int main(void)
{

int i,j;

scanf("hai");
scanf("%d\n",&i);
printf("i value %d\n",i);
scanf("%d",&j);
printf("j value %d\n",j);



return 0;
}

its printing i and j values both at a time(desired should print i
value first and j next).
I am using gcc 4.2 on linux .
what does scanf("hai") mean?
Is there any special meaning if i give '\n' in scanf?

 
Reply With Quote
 
 
 
 
Argento Lee
Guest
Posts: n/a
 
      05-15-2007

"Mohd Azhar Hussain" <> wrote in message
news: ups.com...
>
> I am new to C. I was trying to read input from key board using scanf,
> here's my program
>
>
> int main(void)
> {
>
> int i,j;
>
> scanf("hai");
> scanf("%d\n",&i);
> printf("i value %d\n",i);
> scanf("%d",&j);
> printf("j value %d\n",j);
>
>
>
> return 0;
> }
>
> its printing i and j values both at a time(desired should print i
> value first and j next).
> I am using gcc 4.2 on linux .
> what does scanf("hai") mean?
> Is there any special meaning if i give '\n' in scanf?
>


I am a newbie too and I don't have all the answers to your questions but I
guess this reading would help you understand more about the '\n' part.
http://c-faq.com/stdio/scanfhang.html


 
Reply With Quote
 
 
 
 
Peter Nilsson
Guest
Posts: n/a
 
      05-15-2007
Mohd Azhar Hussain <azhar...@gmail.com> wrote:
> I am new to C.


Do you have a good reference book or tutorial?

<snip>
> scanf("%d\n",&i);

<snip>
> Is there any special meaning if i give '\n' in scanf?


Yes. Any good reference will tell you.

It's also a good idea to consult the FAQ before
posting...

http://c-faq.com/stdio/scanfhang.html

--
Peter

 
Reply With Quote
 
Mohd Azhar Hussain
Guest
Posts: n/a
 
      05-15-2007
On May 15, 8:26 am, Mohd Azhar Hussain <azhar...@gmail.com> wrote:
> I am new to C. I was trying to read input from key board using scanf,
> here's my program
>
> int main(void)
> {
>
> int i,j;
>
> scanf("hai");
> scanf("%d\n",&i);
> printf("i value %d\n",i);
> scanf("%d",&j);
> printf("j value %d\n",j);
>
> return 0;
>
> }
>
> its printing i and j values both at a time(desired should print i
> value first and j next).
> I am using gcc 4.2 on linux .
> what does scanf("hai") mean?
> Is there any special meaning if i give '\n' in scanf?


thanks for your answers..
I didnot understand what happnens if i write somethin like

scanf("%dabc",&i);

thanks once again.

 
Reply With Quote
 
mark_bluemel@pobox.com
Guest
Posts: n/a
 
      05-15-2007
On 15 May, 04:26, Mohd Azhar Hussain <azhar...@gmail.com> wrote:
> I am new to C. I was trying to read input from key board using scanf,


A huge mistake, IMHO and I'm not alone in that opinion. See large
parts of http://c-faq.com/stdio/index.html, notably
http://c-faq.com/stdio/scanfprobs.html.

> what does scanf("hai") mean?
> Is there any special meaning if i give '\n' in scanf?


If you read the manual (and perhaps the FAQ) carefully, I expect it
will tell it, frankly I don't know and don't care.

In common with strtok(), the number of places where scanf() is the
correct solution to a problem can almost be counted on the fingers of
one foot, and interactive input is definitely not in that set. It
should be very low on the list of functions for a learner to try to
work with...

 
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
difference between scanf("%i") and scanf("%d") ??? perhaps bug inVS2005? =?ISO-8859-1?Q?Martin_J=F8rgensen?= C Programming 18 05-02-2006 10:53 AM
scanf (yes/no) - doesn't work + deprecation errors scanf, fopen etc. =?ISO-8859-1?Q?Martin_J=F8rgensen?= C Programming 185 04-03-2006 02:49 PM
confused about behaviour of scanf Lalatendu Das C Programming 33 01-04-2006 05:27 AM
Scanf Behaviour sajjanharudit@gmail.com C Programming 7 12-30-2005 07:20 PM
Correct behaviour of scanf and sscanf Rob Thorpe C Programming 6 03-15-2005 04:25 PM



Advertisments