help needed for printf()

This is a discussion on help needed for printf() within the C Programming forums, part of the General Programming Boards category; accidentally i wrote this code and got some output but why like this.... Code: main() { int a=1,b=2,c=3; printf("%d%d%d"); } ...

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    17

    help needed for printf()

    accidentally i wrote this code and got some output but why like this....
    Code:
    main()
    {
    int a=1,b=2,c=3;
    printf("%d%d%d");
    }
    and the output is 3 2 1...... why like this

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    10
    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.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    17
    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....

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    10
    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.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    17
    please try this code
    Code:
    #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.... the out put is the reverse printing of a and b

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,417
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    Oct 2006
    Posts
    10
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with basic program
    By JOlszewski in forum C Programming
    Replies: 3
    Last Post: 02-01-2006, 03:19 PM
  2. program not working...please look at this
    By JOlszewski in forum C Programming
    Replies: 3
    Last Post: 01-30-2006, 09:33 PM
  3. sprintf and fprintf segmentation error
    By kona1 in forum C Programming
    Replies: 5
    Last Post: 06-21-2005, 10:55 AM
  4. fprintf to stderr crash programs
    By jlai in forum Windows Programming
    Replies: 2
    Last Post: 04-12-2005, 08:51 AM
  5. fprintf
    By bennyandthejets in forum Windows Programming
    Replies: 10
    Last Post: 11-16-2002, 05:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21