printf and scanf doubt...

Reply

Join Date: Apr 2009
Posts: 29
Reputation: Pavan_ is an unknown quantity at this point 
Solved Threads: 3
Pavan_ Pavan_ is offline Offline
Light Poster

printf and scanf doubt...

 
0
  #1
Jul 7th, 2009
  1. #include<stdio.h>
  2. int main()
  3. {
  4. int i=10,j=30;
  5.  
  6. printf("%d %d",i,j); // this line printing value of i and j...works fine
  7. printf("%d %d"); // this line again printing value of i and j
  8. printf("%d"); // this line printing value of i
  9.  
  10. return 0;
  11. }

On gcc compiler, 2nd and third printf() call taking value of i and j automatically..
but if in first printf() call ,i don't give i and j as argument then that prints garbage value..
can nyone explain it plz..

one more thing...
if i use scanf("%2d %4f",&g,&f); then what will happen...i normally don't use numeric number with %d..
so plz explain both questions.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,968
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: printf and scanf doubt...

 
1
  #2
Jul 7th, 2009
If you specify %d in the format string of the printf() function, you also have to specify a corresponding integer value to print.

General rule: For each format specifier you include in the format string, you also have to supply a corresponding value (of the same type as the format specifier dictates).

And if I'm not wrong, if you use the printf-function like this: printf("%d"); , then this will lead to undefined behavior.
Last edited by tux4life; Jul 7th, 2009 at 9:03 am. Reason: add some additional info
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 681
Reputation: Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of 
Solved Threads: 132
Tom Gunn's Avatar
Tom Gunn Tom Gunn is offline Offline
Practically a Master Poster

Re: printf and scanf doubt...

 
1
  #3
Jul 7th, 2009
On gcc compiler, 2nd and third printf() call taking value of i and j automatically..
but if in first printf() call ,i don't give i and j as argument then that prints garbage value..
can nyone explain it plz..
It happens to work out that way with how printf and varargs work on gcc. varargs probably look down the call stack unconditionally, and the call stack probably doesn't physically change until values are overwritten. The second call does not overwrite the two values and they stay the same for the sequential calls.

It's really pointless to explain how undefined behavior works because it could be different the next time you run the program, or in the next revision of the compiler, and certainly on a different compiler. Just don't do it.

Like tux4life said, you need at least as many values as there are format specifiers in the format string. If there are fewer values than format specifiers, it's undefined behavior. There can be more values, and the extra values will be ignored without it being UB, but that will confuse people reading your code.
Last edited by Tom Gunn; Jul 7th, 2009 at 9:25 am.
-Tommy (For Great Justice!) Gunn
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC