Home » questions » C C C C C C C C C C C C C C C c?

C C C C C C C C C C C C C C C c?

2006-08-09 06:11:09, Category: Programming & Design
I'm using a Xp and turbo c++ compiler and now void main() { int i = 1,j = 2; printf("%d %d"); getch(); } when i run the above program the output is 2 1 why?? The code and the output i mentioned are exactly correct and if anybody wants try it on ur systems and any one cannot know the answers u can mail me for the answer and i will send it to u after 24 hours seeing all the replies i got from the great yahoo answers and i have the exact reason also... The code and the output i mentioned are exactly correct and if anybody wants try it on ur systems and any one cannot know the answers u can mail me for the answer and i will send it to u after 24 hours seeing all the replies i got from the great yahoo answers and i have the exact reason also... and my id: "enigma200687@yahoo.com"

Answers

  1. Earl D

    On 2006-08-09 06:16:48


    Because you're formatting as a double instead of an integer seems the most logical answer. It's been a long time for me and I never liked C but I had Turbo C once.
  2. pretty girl

    On 2006-08-09 06:25:39


    ur program shd be like that void main() { int i=1,j=2; printf("%d %d",j ,i); getch(); } then o/p is like that:- 2 1 else in my thinking there shd be no o/p
  3. avramti

    On 2006-08-09 06:25:12


    This code will print two random integers on the stdout. printf("%d %d",i,j) will print 1 2 and printf("%d %d",j,i) will print 2 1(simply because this is the output you requested!).
  4. Tom

    On 2006-08-09 07:31:36


    Compiler dependent...
  5. stale_poot_21

    On 2006-08-09 06:32:46


    you need to %> 1%> hume, hume , hume a hockman, hume, hume, hume, a mccotter logs/faskings.
  6. Preacher

    On 2006-08-09 06:14:53


    You need to tell it which variables you want it to print i.e %i or %j
  7. duggiedugg

    On 2006-08-09 06:14:54


    You should be getting 19.
  8. Dan M

    On 2006-08-09 06:32:09


    The program invokes undefined behaviour, anything could happen. My guess is that what is happening in this case is that as both i and j are local variables they are being created on the stack and when printf is called a pointer to the format string is also created on the stack then the printf function is passed a pointer to that entry. Now printf is a varargs function and it reads the first argument as requiring two further integers, which it is expecting to find on the stack, but (Because they were not created at function call time (but printf has no way to know that)), it pops the first two available arguments off the stack and prints them. However this is not required behavious and in fact the behaviour may even change from one run to the next. Regards, Dan.