Why is 'n' parameter of snprintf ignored?

The name of the pictureThe name of the pictureThe name of the pictureClash 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?










share|improve this question









New contributor




Westin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.























    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?










    share|improve this question









    New contributor




    Westin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.





















      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?










      share|improve this question









      New contributor




      Westin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      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






      share|improve this question









      New contributor




      Westin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question









      New contributor




      Westin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question








      edited 30 mins ago









      Greenonline

      1,92241639




      1,92241639






      New contributor




      Westin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 1 hour ago









      Westin

      82




      82




      New contributor




      Westin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      Westin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      Westin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.




















          3 Answers
          3






          active

          oldest

          votes

















          up vote
          3
          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.






          share|improve this answer
















          • 1




            It would be helpful because you could snprintf to a very small buffer, note the number returned, then malloc a buffer of the appropriate size, and do it again. That way you know how many bytes to allocate.
            – Nick Gammon♦
            2 mins ago


















          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.






          share|improve this answer





























            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.







            share|improve this answer




















              Your Answer





              StackExchange.ifUsing("editor", function ()
              return StackExchange.using("schematics", function ()
              StackExchange.schematics.init();
              );
              , "cicuitlab");

              StackExchange.ready(function()
              var channelOptions =
              tags: "".split(" "),
              id: "540"
              ;
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function()
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled)
              StackExchange.using("snippets", function()
              createEditor();
              );

              else
              createEditor();

              );

              function createEditor()
              StackExchange.prepareEditor(
              heartbeatType: 'answer',
              convertImagesToLinks: false,
              noModals: false,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: null,
              bindNavPrevention: true,
              postfix: "",
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              );



              );






              Westin is a new contributor. Be nice, and check out our Code of Conduct.









               

              draft saved


              draft discarded


















              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






























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              3
              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.






              share|improve this answer
















              • 1




                It would be helpful because you could snprintf to a very small buffer, note the number returned, then malloc a buffer of the appropriate size, and do it again. That way you know how many bytes to allocate.
                – Nick Gammon♦
                2 mins ago















              up vote
              3
              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.






              share|improve this answer
















              • 1




                It would be helpful because you could snprintf to a very small buffer, note the number returned, then malloc a buffer of the appropriate size, and do it again. That way you know how many bytes to allocate.
                – Nick Gammon♦
                2 mins ago













              up vote
              3
              down vote



              accepted







              up vote
              3
              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.






              share|improve this answer












              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.







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered 47 mins ago









              JRobert

              9,14811035




              9,14811035







              • 1




                It would be helpful because you could snprintf to a very small buffer, note the number returned, then malloc a buffer of the appropriate size, and do it again. That way you know how many bytes to allocate.
                – Nick Gammon♦
                2 mins ago













              • 1




                It would be helpful because you could snprintf to a very small buffer, note the number returned, then malloc a buffer of the appropriate size, and do it again. That way you know how many bytes to allocate.
                – Nick Gammon♦
                2 mins ago








              1




              1




              It would be helpful because you could snprintf to a very small buffer, note the number returned, then malloc a buffer of the appropriate size, and do it again. That way you know how many bytes to allocate.
              – Nick Gammon♦
              2 mins ago





              It would be helpful because you could snprintf to a very small buffer, note the number returned, then malloc a buffer of the appropriate size, and do it again. That way you know how many bytes to allocate.
              – Nick Gammon♦
              2 mins ago











              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.






              share|improve this answer


























                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.






                share|improve this answer
























                  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.






                  share|improve this answer














                  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.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited 33 mins ago

























                  answered 39 mins ago









                  Jot

                  1,771416




                  1,771416




















                      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.







                      share|improve this answer
























                        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.







                        share|improve this answer






















                          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.







                          share|improve this answer












                          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.








                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered 22 mins ago









                          Greenonline

                          1,92241639




                          1,92241639




















                              Westin is a new contributor. Be nice, and check out our Code of Conduct.









                               

                              draft saved


                              draft discarded


















                              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.













                               


                              draft saved


                              draft discarded














                              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













































































                              Comments

                              Popular posts from this blog

                              Long meetings (6-7 hours a day): Being “babysat” by supervisor

                              Is the Concept of Multiple Fantasy Races Scientifically Flawed? [closed]

                              Confectionery