Why Doesn't Sprintf() Output Anything

Why doesn't sprintf() output anything?

sprintf() returns a string, printf() displays it.

The following two are equal:

printf(currentDateTime());
print sprintf(currentDateTime());

Why doesn't sprintf() print anything?

sprintf returns a variable (a string).

You need printf

sprintf sentence give me bad output

sprintf doesn't actually print the text, it returns it as a string.

You probably meant to use printf instead, or you can echo the results of your sprintf statement.

sprintf not doing anything - trying to output as two digit number (leading zero)

it's problem with casting you are casting of sprint() not variables so try

$obj = array(
"h" => (int)$hours,
"m" => sprintf("%02d", (int)$minutes),
"s" => sprintf("%02d", (int)$seconds)
);

Why does this output seem non-deterministic? (Is it the sprintf, printf, or syntax of hexadecimal literals?)

The problem is that your string literal has an embedded NUL byte, and that marks the end of the string as far as sprintf is concerned. So your call is identical to:

sprintf(login,
"\x15",
_user,
_password);

And that writes into the login array only two bytes: 0x15 0x00.

There are several approaches to solve this mixing of bytes and characters. My choice would be something along the lines of:

memcpy(login, "\x15\x00\x01", 3);
sprintf(login + 3,
"%-8s%-10s",
_user,
_password);

The call to memcpy takes as parameter the number of bytes, so it is immune to the embedded NUL problem.

But note that sprintf automaticall adds a NUL byte at the end of the output string, so you actually need 22 bytes: 3 + 8 + 10 + 1 = 22:

char login[22];

Using floats with sprintf() in embedded C

Since you're on an embedded platform, it's quite possible that you don't have the full range of capabilities from the printf()-style functions.

Assuming you have floats at all (still not necessarily a given for embedded stuff), you can emulate it with something like:

char str[100];
float adc_read = 678.0123;

char *tmpSign = (adc_read < 0) ? "-" : "";
float tmpVal = (adc_read < 0) ? -adc_read : adc_read;

int tmpInt1 = tmpVal; // Get the integer (678).
float tmpFrac = tmpVal - tmpInt1; // Get fraction (0.0123).
int tmpInt2 = trunc(tmpFrac * 10000); // Turn into integer (123).

// Print as parts, note that you need 0-padding for fractional bit.

sprintf (str, "adc_read = %s%d.%04d\n", tmpSign, tmpInt1, tmpInt2);

You'll need to restrict how many characters come after the decimal based on the sizes of your integers. For example, with a 16-bit signed integer, you're limited to four digits (9,999 is the largest power-of-ten-minus-one that can be represented).

However, there are ways to handle this by further processing the fractional part, shifting it by four decimal digits each time (and using/subtracting the integer part) until you have the precision you desire.


Update:

One final point you mentioned that you were using avr-gcc in a response to one of the other answers. I found the following web page that seems to describe what you need to do to use %f in your printf() statements here.

As I originally suspected, you need to do some extra legwork to get floating point support. This is because embedded stuff rarely needs floating point (at least none of the stuff I've ever done). It involves setting extra parameters in your makefile and linking with extra libraries.

However, that's likely to increase your code size quite a bit due to the need to handle general output formats. If you can restrict your float outputs to 4 decimal places or less, I'd suggest turning my code into a function and just using that - it's likely to take up far less room.

In case that link ever disappears, what you have to do is ensure that your gcc command has "-Wl,-u,vfprintf -lprintf_flt -lm". This translates to:

  • force vfprintf to be initially undefined (so that the linker has to resolve it).
  • specify the floating point printf() library for searching.
  • specify the math library for searching.

Why is sprintf giving asterisks instead of my formatted string?

This issue was actually caused because the sequence of numbers matched the Luhn Algorithm, and was being masked in my PCI environment. I manually excluded the file from the test, and everything now is working as expected.



Related Topics



Leave a reply



Submit