printf() problem

jdm jdm at hodge.UUCP
Wed Apr 26 10:25:48 AEST 1989


    Perhaps someone could explain this printf() phenomena to me.

    I have a file with binary data.  I want to read four consecutive
    bytes from the file and display them with printf().  The data
    in in the file in hex is:

        92 AB 4E 33

    I fopen() the file in binary mode and used the following line
    of code to read and print out the data:

        printf("%x %x %x %x\n", getc(fp), getc(fp), getc(fp), getc(fp));

    Although the order of the data in the file is:

        92 AB 4E 33

    printf() displays it as:

        33 4E AB 92

    Just the reverse of its order in the file.

    Changing the code to:

        a = getc(fp);
        b = getc(fp);
        c = getc(fp);
        d = getc(fp);

        printf("%x %x %x %x\n", a, b, c, d);

    solves the problem, but why did printf() screw the order up when
    I used getc() directly?  How might I correct this?

    This happens in both Turbo C 2.0 and MS C 5.1.



More information about the Comp.lang.c mailing list