This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

sprintf with long?

Hello,

I am trying to create a string with a variable of type long, using sprintf.

char command [20];
long ofs = 363080;

sprintf (command, "VAL=%d", ofs);
The output string I get is this:
VAL=-30136
which means that the long variable was converted into a short.
Does sprintf not support long types?

Thank you for your help!
Holger

Parents
  • I only looked in a standard C-manual, where there's no such thing as an L

    Hmm... I think you need to throw that manual away and get a proper one!

    My ancient K&R (2nd Edition, 1988) says,
    "Between the % and the conversion character there may be ... an h if the integer is to be printed as a short, or l if as a long"

    It doesn't mention if the h and l are case-sensitive, but in C51 they certainly aren't (I prefer to use the uppercase L, as lowercase l looks too much like the digit 1 in many fonts)

    Note that K&R also says that printf, "will get confused, and you will get wrong answers, if there are not enough arguments or if they are the wrong type" (my emphasis)

Reply
  • I only looked in a standard C-manual, where there's no such thing as an L

    Hmm... I think you need to throw that manual away and get a proper one!

    My ancient K&R (2nd Edition, 1988) says,
    "Between the % and the conversion character there may be ... an h if the integer is to be printed as a short, or l if as a long"

    It doesn't mention if the h and l are case-sensitive, but in C51 they certainly aren't (I prefer to use the uppercase L, as lowercase l looks too much like the digit 1 in many fonts)

    Note that K&R also says that printf, "will get confused, and you will get wrong answers, if there are not enough arguments or if they are the wrong type" (my emphasis)

Children
No data