Odd printf() Behavior

Discussion in 'C Programming' started by Brian Dude, May 8, 2013.

  1. Brian Dude

    Brian Dude Guest

    Hello, I'm working on some graphics routines and just started trying to
    read in some bitmap files. For troubleshooting, I'm printing out various
    values to make sure they're all okay.

    I have two globals and a local whose values I wanted to verify. I made a
    bitmap header structure for obtaining info.

    int chunx;
    int scan;

    int main(int argc,char *argv[])
    {
    int slwb; /*Scan Line Width in Bytes*/
    struct W3_BMP_header Q;
    long int X;
    FILE *bmpsource;
    /*other variables*/

    readBMheader(&Q,bmpsource);
    scan=(int)Q.height;
    X= Q.width%2==0 ? Q.width/2 : Q.width/2+1;
    slwb=X+(4-X%4)%4;
    chunx=(int)(Q.width/8+(Q.width%8>0));
    printf("\nchunx = %d\nslwb = %d\nscan = %d\n",chunx,slwb,scan);

    For some reason, the value for 'scan' keeps showing up as 0, only in
    this particular printf() call. Both 'chunx' and 'slwb' show up
    correctly. I made a separate printf() call displaying only the 'scan'
    variable (both before and after the other printf), and it shows up fine.
    What's going on?

    TIA,
    Brian Dude
    Brian Dude, May 8, 2013
    #1
    1. Advertising

  2. Brian Dude

    BartC Guest

    "Brian Dude" <> wrote in message
    news:fKfit.18304$...

    > int chunx;
    > int scan;
    >
    > int main(int argc,char *argv[])
    > {
    > int slwb; /*Scan Line Width in Bytes*/


    > printf("\nchunx = %d\nslwb = %d\nscan = %d\n",chunx,slwb,scan);
    >
    > For some reason, the value for 'scan' keeps showing up as 0, only in this
    > particular printf() call. Both 'chunx' and 'slwb' show up correctly.


    Is the above the code *exactly* as you have it?

    I can make scan come up 0 when slwb is made 'long long int' instead of 'int'
    (so that it occupies 64-bits in the parameter list, and %d expects only
    32-bits; the extra 32-bits is read by the final "%d", and happen to be
    zero). There might be other ways this can happen.

    What happens if you mix up the order of these three variables? What if you
    add one or two extra 'scan' arguments to the printf, together with two more
    %d formats; are those zero too? (Perhaps change "%d" for scan, to "<%d>", to
    check the final characters are not suppressed, hidden, or overwritten for
    any reason, and the zero doesn't belong to anything else.)

    --
    Bartc
    BartC, May 8, 2013
    #2
    1. Advertising

  3. Brian Dude

    Brian Dude Guest

    On 5/7/2013 7:57 PM, BartC wrote:
    > "Brian Dude" <> wrote in message
    > news:fKfit.18304$...
    >
    >> int chunx;
    >> int scan;
    >>
    >> int main(int argc,char *argv[])
    >> {
    >> int slwb; /*Scan Line Width in Bytes*/

    >
    >> printf("\nchunx = %d\nslwb = %d\nscan = %d\n",chunx,slwb,scan);
    >>
    >> For some reason, the value for 'scan' keeps showing up as 0, only in this
    >> particular printf() call. Both 'chunx' and 'slwb' show up correctly.

    >
    > Is the above the code *exactly* as you have it?


    It's almost exact, but not 100%.

    >
    > I can make scan come up 0 when slwb is made 'long long int' instead of
    > 'int'
    > (so that it occupies 64-bits in the parameter list, and %d expects only
    > 32-bits; the extra 32-bits is read by the final "%d", and happen to be
    > zero). There might be other ways this can happen.
    >
    > What happens if you mix up the order of these three variables? What if you
    > add one or two extra 'scan' arguments to the printf, together with two more
    > %d formats; are those zero too? (Perhaps change "%d" for scan, to
    > "<%d>", to check the final characters are not suppressed, hidden, or
    > overwritten for any reason, and the zero doesn't belong to anything else.)
    >


    I tried rearranging the arguments:

    printf("\nchunx = %d\nscan = %d\nslwb = %d\n",chunx,scan,slwb);

    and all three printed correctly. I then put them back into the original
    order and put the angle brackets around 'scan'. It printed as <0>.
    Brian Dude, May 8, 2013
    #3
  4. Brian Dude

    BartC Guest

    "Brian Dude" <> wrote in message
    news:8Qgit.18313$...
    > On 5/7/2013 7:57 PM, BartC wrote:


    >> What happens if you mix up the order of these three variables? What if
    >> you
    >> add one or two extra 'scan' arguments to the printf, together with two
    >> more
    >> %d formats; are those zero too? (Perhaps change "%d" for scan, to
    >> "<%d>", to check the final characters are not suppressed, hidden, or
    >> overwritten for any reason, and the zero doesn't belong to anything
    >> else.)
    >>

    >
    > I tried rearranging the arguments:
    >
    > printf("\nchunx = %d\nscan = %d\nslwb = %d\n",chunx,scan,slwb);



    OK, try these (assuming that removing the labels doesn't hide the problem):

    printf("%d %d %d %d\n", chunx, slwb, scan, 12345);
    printf("%d %d %d %d\n", chunx, scan, slwb, 12345);

    What's shown after the 0 of scan in the first printf?

    Also perhaps:

    printf("%d %d %d\n", sizeof chunx, sizeof slwb, sizeof scan);

    (They should all be the same!)

    --
    Bartc
    BartC, May 8, 2013
    #4
  5. Brian Dude <> writes:
    > Hello, I'm working on some graphics routines and just started trying to
    > read in some bitmap files. For troubleshooting, I'm printing out various
    > values to make sure they're all okay.
    >
    > I have two globals and a local whose values I wanted to verify. I made a
    > bitmap header structure for obtaining info.
    >
    > int chunx;
    > int scan;
    >
    > int main(int argc,char *argv[])
    > {
    > int slwb; /*Scan Line Width in Bytes*/
    > struct W3_BMP_header Q;
    > long int X;
    > FILE *bmpsource;
    > /*other variables*/
    >
    > readBMheader(&Q,bmpsource);
    > scan=(int)Q.height;
    > X= Q.width%2==0 ? Q.width/2 : Q.width/2+1;
    > slwb=X+(4-X%4)%4;
    > chunx=(int)(Q.width/8+(Q.width%8>0));
    > printf("\nchunx = %d\nslwb = %d\nscan = %d\n",chunx,slwb,scan);
    >
    > For some reason, the value for 'scan' keeps showing up as 0, only in
    > this particular printf() call. Both 'chunx' and 'slwb' show up
    > correctly. I made a separate printf() call displaying only the 'scan'
    > variable (both before and after the other printf), and it shows up fine.
    > What's going on?


    Try adding *exactly* the following lines immediately after your existing
    printf call:

    printf("chunx = %d\n", chunx);
    printf("slwb = %d\n", slwb);
    printf("scan = %d\n", scan);

    Do these printf calls show the same values as your existing printf call,
    and are those values correct?

    Incidentally, the casts are unnecessary. If Q.height isn't of type int,
    it will be implicitly converted to int by the assignment. (And if
    Q.height isn't of type int, I wonder why scan isn't of the same type.)
    Likewise for chunx.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Working, but not speaking, for JetHead Development, Inc.
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, May 8, 2013
    #5
  6. Brian Dude

    James Kuyper Guest

    On 05/07/2013 07:02 PM, Brian Dude wrote:
    > Hello, I'm working on some graphics routines and just started trying to
    > read in some bitmap files. For troubleshooting, I'm printing out various
    > values to make sure they're all okay.
    >
    > I have two globals and a local whose values I wanted to verify. I made a
    > bitmap header structure for obtaining info.
    >
    > int chunx;
    > int scan;
    >
    > int main(int argc,char *argv[])
    > {
    > int slwb; /*Scan Line Width in Bytes*/
    > struct W3_BMP_header Q;
    > long int X;
    > FILE *bmpsource;


    bmpsource is uninitialized here.

    > /*other variables*/
    >
    > readBMheader(&Q,bmpsource);


    You use bmpsource here, despite the fact that it hasn't been
    initialized. I hope that in your actual code, bmpsource is filled in by
    a call to fopen()? Another indication that this program is incomplete is
    your use of struct W3_BMP_header, FILE, readBMPheader(), and printf().
    Each of those is probably declared in a header file, and your code, as
    written, doesn't #include any headers.

    Please keep in mind that if you knew what was wrong with your program,
    you wouldn't be coming here for help. Since you don't know what's wrong
    with it, there's a very good chance that something you think is
    irrelevant, isn't. Therefore, don't cut out anything just because you
    think it's irrelevant. Show us a program as simple as possible, while
    still demonstrating the problem. However, show us the complete program,
    not just the part you think is relevant. Do not re-type the program -
    cut and paste it directly from the source - otherwise we may end up
    wasting time investigating the typos you introduced while re-typing the
    code.

    > scan=(int)Q.height;


    Q.height was uninitialized at the start of your program. The only code
    you've shown us that had any chance of setting it to a defined value
    before this line is executed was the call to readBMPheader(). Therefore,
    if some thing's wrong, it's probably because readBMPheader() is not
    doing what you think it should be doing. This, in turn, is probably
    because you were wrong about what you thought it should be doing (the
    problem could be in readBMPheader(), but in general it's more likely to
    be your code). That means that this isn't a C problem, it's a problem
    with whatever package readBMPheader() is part of. Therefore, you'll
    probably get better help with your problem by taking it to a forum
    devoted to that package. comp.lang.c isn't that forum.
    --
    James Kuyper
    James Kuyper, May 8, 2013
    #6
  7. Brian Dude

    Brian Dude Guest

    On 5/7/2013 8:34 PM, BartC wrote:
    > "Brian Dude" <> wrote in message
    > news:8Qgit.18313$...
    >> On 5/7/2013 7:57 PM, BartC wrote:

    >
    >
    > OK, try these (assuming that removing the labels doesn't hide the problem):
    >
    > printf("%d %d %d %d\n", chunx, slwb, scan, 12345);
    > printf("%d %d %d %d\n", chunx, scan, slwb, 12345);
    >
    > What's shown after the 0 of scan in the first printf?
    >
    > Also perhaps:
    >
    > printf("%d %d %d\n", sizeof chunx, sizeof slwb, sizeof scan);
    >
    > (They should all be the same!)
    >


    Ahh, thank you for the sizeof idea. I accidentally had 'slwb' as a long
    (so the results *were* different). I'm been working on this function for
    too long, I should have stepped away for a bit and come back later.
    Brian Dude, May 9, 2013
    #7
  8. Brian Dude <> writes:
    > On 5/7/2013 8:34 PM, BartC wrote:
    >> "Brian Dude" <> wrote in message
    >> news:8Qgit.18313$...
    >>> On 5/7/2013 7:57 PM, BartC wrote:

    >> OK, try these (assuming that removing the labels doesn't hide the problem):
    >>
    >> printf("%d %d %d %d\n", chunx, slwb, scan, 12345);
    >> printf("%d %d %d %d\n", chunx, scan, slwb, 12345);
    >>
    >> What's shown after the 0 of scan in the first printf?
    >>
    >> Also perhaps:
    >>
    >> printf("%d %d %d\n", sizeof chunx, sizeof slwb, sizeof scan);
    >>
    >> (They should all be the same!)


    Don't use "%d" to print the result of sizeof. The sizeof operator
    yields a result of type size_t. If int and size_t happen to be the same
    size, it's likely to "work", but it's better to use explicit casts:

    printf("%d %d %d\n", (int)sizeof chunx, (int)sizeof slwb, (int)sizeof scan);

    or to use the "%zu" format, which is specifically intended for size_t values:

    printf("%zu %zu %zu\n", sizeof chunx, sizeof slwb, sizeof scan);

    (The only real reason *not* to use the latter is that some old
    implementations may not support "%zu", which was added by C99.)

    > Ahh, thank you for the sizeof idea. I accidentally had 'slwb' as a long
    > (so the results *were* different). I'm been working on this function for
    > too long, I should have stepped away for a bit and come back later.


    This is why you *really* need to post the exact code that you're
    compiling. Don't summarize it, don't re-type it, copy-and-paste it.
    Feel free to trim it down to something small enough to post, but
    verify that the trimmed version exhibits the problem before you
    post it here. (The code you posted clearly defined slwb as an int,
    so it was impossible for anyone to help you.)

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Working, but not speaking, for JetHead Development, Inc.
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, May 9, 2013
    #8
  9. Brian Dude

    Brian Dude Guest

    On 5/8/2013 11:58 PM, Keith Thompson wrote:
    >
    > This is why you *really* need to post the exact code that you're
    > compiling. Don't summarize it, don't re-type it, copy-and-paste it.
    > Feel free to trim it down to something small enough to post, but
    > verify that the trimmed version exhibits the problem before you
    > post it here. (The code you posted clearly defined slwb as an int,
    > so it was impossible for anyone to help you.)
    >


    Yes. Quite right :/
    Brian Dude, May 10, 2013
    #9
  10. Brian Dude

    Ken Brody Guest

    On 5/7/2013 8:17 PM, Brian Dude wrote:
    > On 5/7/2013 7:57 PM, BartC wrote:
    >> "Brian Dude" <> wrote in message
    >> news:fKfit.18304$...
    >>
    >>> int chunx;
    >>> int scan;
    >>>
    >>> int main(int argc,char *argv[])
    >>> {
    >>> int slwb; /*Scan Line Width in Bytes*/

    >>
    >>> printf("\nchunx = %d\nslwb = %d\nscan = %d\n",chunx,slwb,scan);
    >>>
    >>> For some reason, the value for 'scan' keeps showing up as 0, only in this
    >>> particular printf() call. Both 'chunx' and 'slwb' show up correctly.

    >>
    >> Is the above the code *exactly* as you have it?

    >
    > It's almost exact, but not 100%.

    [...]

    Why not? If you say that a printf() line prints the wrong value for a
    variable, but a different printf() prints the correct value, what make you
    think that giving us the "almost exact" printf() is of any use?

    Does the printf() that you gave above, if used "100% exact" as you posted in
    your question, work or not work?

    What is the "100% exact" printf() statement that does fail?

    Are the definitions of chunx, slwb, and scan "100% exact"? If not, then
    how, exactly, are they defined?
    Ken Brody, May 10, 2013
    #10
  11. Ken Brody <> writes:
    > On 5/7/2013 8:17 PM, Brian Dude wrote:
    >> On 5/7/2013 7:57 PM, BartC wrote:
    >>> "Brian Dude" <> wrote in message
    >>> news:fKfit.18304$...
    >>>
    >>>> int chunx;
    >>>> int scan;
    >>>>
    >>>> int main(int argc,char *argv[])
    >>>> {
    >>>> int slwb; /*Scan Line Width in Bytes*/
    >>>
    >>>> printf("\nchunx = %d\nslwb = %d\nscan = %d\n",chunx,slwb,scan);
    >>>>
    >>>> For some reason, the value for 'scan' keeps showing up as 0, only in this
    >>>> particular printf() call. Both 'chunx' and 'slwb' show up correctly.
    >>>
    >>> Is the above the code *exactly* as you have it?

    >>
    >> It's almost exact, but not 100%.

    > [...]
    >
    > Why not? If you say that a printf() line prints the wrong value for a
    > variable, but a different printf() prints the correct value, what make you
    > think that giving us the "almost exact" printf() is of any use?
    >
    > Does the printf() that you gave above, if used "100% exact" as you posted in
    > your question, work or not work?
    >
    > What is the "100% exact" printf() statement that does fail?
    >
    > Are the definitions of chunx, slwb, and scan "100% exact"? If not, then
    > how, exactly, are they defined?


    This was addressed a couple of days ago, in Message-ID:
    <JoCit.12$>. slwb was actually defined as a long.
    (Yes, the OP has been advised to post real code.)

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Working, but not speaking, for JetHead Development, Inc.
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, May 10, 2013
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ben
    Replies:
    4
    Views:
    615
    Martin Ambuhl
    Jun 26, 2004
  2. whatluo

    (void) printf vs printf

    whatluo, May 26, 2005, in forum: C Programming
    Replies:
    29
    Views:
    1,240
  3. Michael Speer

    Odd behavior with odd code

    Michael Speer, Feb 16, 2007, in forum: C Programming
    Replies:
    33
    Views:
    1,092
    Richard Heathfield
    Feb 18, 2007
  4. azza

    printf affects following printf/s

    azza, Oct 17, 2010, in forum: C Programming
    Replies:
    0
    Views:
    431
  5. guru
    Replies:
    8
    Views:
    281
Loading...

Share This Page