Why is 'n' parameter of snprintf ignored?
Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
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?
string
New contributor
add a comment |Â
up vote
1
down vote
favorite
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?
string
New contributor
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
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?
string
New contributor
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?
string
string
New contributor
New contributor
edited 23 mins ago
Greenonline
1,92241639
1,92241639
New contributor
asked 55 mins ago
Westin
82
82
New contributor
New contributor
add a comment |Â
add a comment |Â
3 Answers
3
active
oldest
votes
up vote
2
down vote
accepted
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.
add a comment |Â
up vote
3
down vote
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.
add a comment |Â
up vote
0
down vote
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.
add a comment |Â
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
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.
add a comment |Â
up vote
2
down vote
accepted
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.
add a comment |Â
up vote
2
down vote
accepted
up vote
2
down vote
accepted
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.
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.
answered 39 mins ago
JRobert
9,13811035
9,13811035
add a comment |Â
add a comment |Â
up vote
3
down vote
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.
add a comment |Â
up vote
3
down vote
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.
add a comment |Â
up vote
3
down vote
up vote
3
down vote
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.
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.
edited 26 mins ago
answered 32 mins ago
Jot
1,771416
1,771416
add a comment |Â
add a comment |Â
up vote
0
down vote
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.
add a comment |Â
up vote
0
down vote
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.
add a comment |Â
up vote
0
down vote
up vote
0
down vote
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.
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.
answered 14 mins ago
Greenonline
1,92241639
1,92241639
add a comment |Â
add a comment |Â
Westin is a new contributor. Be nice, and check out our Code of Conduct.
Westin is a new contributor. Be nice, and check out our Code of Conduct.
Westin is a new contributor. Be nice, and check out our Code of Conduct.
Westin is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2farduino.stackexchange.com%2fquestions%2f57086%2fwhy-is-n-parameter-of-snprintf-ignored%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password