accidentally i wrote this code and got some output but why like this....
and the output is 3 2 1...... why like thisCode:main() { int a=1,b=2,c=3; printf("%d%d%d"); }
accidentally i wrote this code and got some output but why like this....
and the output is 3 2 1...... why like thisCode:main() { int a=1,b=2,c=3; printf("%d%d%d"); }
Pure cioncidence.
Possibly your specific platform as a little quirk, but usually if printf is not passed the arguments it just starts peeking at adress NULL or anywhich way it can find anything of use. It might be that on your platform the program stack is at the specific address, and of course a stack may just contain the recently declared values in reverse order.
no..i dont think its a coincidence ..i changed the values of a,b,c but still the output is the same...ie printing a b c in reverse order....
Still it can be just the program stack.
Seriously, my compiler gives "490200372890023776389" as output, and the second time "5523783989988370002389"
Believe me, there is no documented feature that does what happens to you. I do not know where things are stored by operating systems, but stack looks like the best explanation to me.
please try this code
i am using turbo c++ compiler.... the out put is the reverse printing of a and bCode:#include<stdio.h> #include<conio.h> main() { int a,b; printf("\nEnter the values of a and b:\n"); scanf("%d%d",&a,&b); printf("%d\n%d"); getch(); }
> i am using turbo c++ compiler
Get a new compiler, stop using stone-age tech.
> i dont think its a coincidence
Yes it is coincidence that you see something vaguely like you expect.
Your code is broken if the arguments to printf (in this case none at all) do not match the control string.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Printf();
accepts a sting followed by no arguments, 1 argument, 3000 arguments. Without throwing with things like vararg and other workings, if you do not pass an argument it is up to the specific compiler to handle that as it wishes, the array of arguments in this case is likely to pass some NULL+index on request (so first %d gets NULL, the second NULL+1, etc).
Thus:
You are reading what's on NULL if no exceptions are generated (which does not happen), on my computer, NULL points to 0, where biosdata is, this data is then interpreted as 4 byte ints and printed as such. It is still very possible that in the original experiment NULL points to a program stack (might be turbo C++ compiler compiles NULL relative to program start). This means that reading 2 int values pushes the first onto the stack, then the second, depending on the stacktype inplementation (which might even vary from AMD to INTEL) you COULD read back the pushes in either correct order, or exact reverse. However, this is still a coincidence with a big streak of random. Very machine and platform specific and as such not really interesting to go into any further on a C board.
[edit]
Indeed if I use vararg myself I get some indexed return value when trying to read from it when no args were given, it is not NULL however.
[/edit]
Last edited by Asmyldof; 10-25-2006 at 09:21 AM.