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
  • In C51, it is very important that you match the type of the actual parameters to the type of the format specifier; eg,
    %d is for a signed int;
    %u is for an unsigned int;
    %Ld is for a signed long;
    %Lu is for an unsigned long.

    I guess C166 is similar and you need to add the 'L' (not case-sensitive)

Reply
  • In C51, it is very important that you match the type of the actual parameters to the type of the format specifier; eg,
    %d is for a signed int;
    %u is for an unsigned int;
    %Ld is for a signed long;
    %Lu is for an unsigned long.

    I guess C166 is similar and you need to add the 'L' (not case-sensitive)

Children
More questions in this forum