| | |
printf and scanf doubt...
![]() |
•
•
Join Date: Apr 2009
Posts: 29
Reputation:
Solved Threads: 3
C Syntax (Toggle Plain Text)
#include<stdio.h> int main() { int i=10,j=30; printf("%d %d",i,j); // this line printing value of i and j...works fine printf("%d %d"); // this line again printing value of i and j printf("%d"); // this line printing value of i return 0; }
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.
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:
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."
•
•
•
•
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'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
![]() |
Similar Threads
- scanf and printf with fstream (C++)
- convert cin,cout into scanf and printf (C)
- Source code for printf, scanf, cin, cout? (C)
- Implementing scanf with getchar (C)
- C++ advice (scanf/printf) (C++)
Other Threads in the C Forum
- Previous Thread: Re: mp3 compression
- Next Thread: sorted linked list
| Thread Tools | Search this Thread |
* adobe api array arrays binarysearch calculate centimeter char cm convert copyanyfile copypdffile cprogramme createcopyoffile createprocess() csyntax directory dynamic feet fflush file floatingpointvalidation fork forloop frequency getlasterror getlogicaldrivestrin givemetehcodez global graphics gtkgcurlcompiling gtkwinlinux hacking hardware highest homework i/o ide inches incrementoperators intmain() iso km linked linkedlist linux linuxsegmentationfault list locate logical_drives loopinsideloop. match matrix microsoft motherboard mqqueue mysql oddnumber odf open opendocumentformat opensource openwebfoundation pattern pdf performance pointer posix power program programming pyramidusingturboccodes read recursion recv recvblocked repetition scanf scheduling segmentationfault send shape single socketprograming socketprogramming stack standard strchr string suggestions test unix urboc user variable voidmain() whythiscodecausesegmentationfault win32api windows.h






