scanf behaviour

M

Mohd Azhar Hussain

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?
 
A

Argento Lee

Mohd Azhar Hussain said:
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
 
M

Mohd Azhar Hussain

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

mark_bluemel

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

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

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top