user50502: I have found that the n parameter of snprintf() seems to be ignored in my code.
char asdf[10];
Serial1.println(snprintf(asdf, 2, "hello"));

This prints 5 when I would expect it to print 2. What is happening?

user30198: A test sketch for the Arduino Uno:
char buffer[10];

void setup() {
  Serial.begin(9600);
  int n = snprintf(buffer, 2, "hello");
  Serial.println(n);
  Serial.println(buffer);
}

void loop() {
}

As @JRobert wrote, the "would have" is the key. As far as I know only the snprintf and the vsnprintf return a "would have" number.
I think the reason is to be able to tell if the string was truncated. Suppose the 'size' parameter is 25 and the format string is very long, then the return value can be tested against 25. If the return value was 26 (the "would have" number of bytes), then the string was truncated.
This information was not possible to retrieve when the "would have" number was not available.

user6936: For completion, the man page for fprintf states:

The snprintf() function shall be equivalent to sprintf(), with the
  addition of the n argument which states the size of the buffer
  referred to by s.  If n is zero, nothing shall be written and s may
  be a null pointer. Otherwise, output bytes beyond the n‐1st shall be
  discarded instead of being written to the array, and a null byte is
  written at the end of the bytes actually written into the array.

and, more relevant:

Upon successful completion, the snprintf() function shall return the
         number of bytes that would be written to s had n been sufficiently
         large excluding the terminating null byte.


user313: snprintf() will not write more than <size> (snprintf's 2d argument) characters to your buffer, but it does count (and discard the extra) characters it would have written, had there been space enough, and that is the number it returns. Yeah, it can be confusing!
See this snprintf() reference.

50502: <p>I have found that the <code>n</code> parameter of <code>snprintf()</code> seems to be ignored in my code.</p>

<pre><code>char asdf[10];
Serial1.println(snprintf(asdf, 2, "hello"));
</code></pre>

<p>This prints 5 when I would expect it to print 2. What is happening?</p>
 30198: <p>A test sketch for the Arduino Uno:</p>

<pre><code>char buffer[10];

void setup() {
  Serial.begin(9600);
  int n = snprintf(buffer, 2, "hello");
  Serial.println(n);
  Serial.println(buffer);
}

void loop() {
}
</code></pre>

<p>As @JRobert wrote, the "would have" is the key. As far as I know only the <a href="http://www.cplusplus.com/reference/cstdio/snprintf/" rel="nofollow noreferrer">snprintf</a> and the <a href="http://www.cplusplus.com/reference/cstdio/vsnprintf/" rel="nofollow noreferrer">vsnprintf</a> return a "would have" number.</p>

<p>I think the reason is to be able to tell if the string was truncated. Suppose the 'size' parameter is 25 and the format string is very long, then the return value can be tested against 25. If the return value was 26 (the "would have" number of bytes), then the string was truncated.<br>
This information was not possible to retrieve when the "would have" number was not available.</p>
 6936: <p>For completion, the man page for <a href="http://man7.org/linux/man-pages/man3/fprintf.3p.html" rel="nofollow noreferrer"><code>fprintf</code></a> states:</p>

<blockquote>
  <p>The snprintf() function shall be equivalent to sprintf(), with the
  addition of the n argument which states the size of the buffer
  referred to by s.  If n is zero, nothing shall be written and s may
  be a null pointer. Otherwise, output bytes beyond the n‐1st shall be
  discarded instead of being written to the array, and a null byte is
  written at the end of the bytes actually written into the array.</p>
</blockquote>

<p>and, more relevant:</p>

<blockquote>
  <p>Upon successful completion, the snprintf() function shall return the
         number of bytes that would be written to s had n been sufficiently
         large excluding the terminating null byte.</p>
</blockquote>
 313: <p>snprintf() will not write more than &lt;size&gt; (snprintf's 2d argument) characters to your buffer, but it does count (and discard the extra) characters it would have written, had there been space enough, and that is the number it returns. Yeah, it can be confusing!</p>

<p>See this <a href="http://www.cplusplus.com/reference/cstdio/snprintf/" rel="noreferrer">snprintf() reference</a>.</p>
