Why does `printf “%s”` concatenate two following strings together? [duplicate]











up vote
10
down vote

favorite
3













This question already has an answer here:




  • How does printf with multiple arguments separated by space work

    1 answer




$ printf "%s" a b
ab$ printf "%s%s" a b
ab


I have some problem understand the format specifier for printf. If I am correct it is mostly the same as those for strings in the C programming language.



Why does the format specifier %s concatenate the two following strings together?



Why does %s not mean that there is only one string to substitute it, and ignore the remaining string?



Why are the results for two strings under %s and under %s%s the same?










share|improve this question









New contributor




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











marked as duplicate by Isaac, thrig, RalfFriedl, Filipe Brandenburger, muru yesterday


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • What do you get if you do printf "%s 123" "hello" "world", yes it surprised me as well, but it is in the manual.
    – ctrl-alt-delor
    2 days ago

















up vote
10
down vote

favorite
3













This question already has an answer here:




  • How does printf with multiple arguments separated by space work

    1 answer




$ printf "%s" a b
ab$ printf "%s%s" a b
ab


I have some problem understand the format specifier for printf. If I am correct it is mostly the same as those for strings in the C programming language.



Why does the format specifier %s concatenate the two following strings together?



Why does %s not mean that there is only one string to substitute it, and ignore the remaining string?



Why are the results for two strings under %s and under %s%s the same?










share|improve this question









New contributor




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











marked as duplicate by Isaac, thrig, RalfFriedl, Filipe Brandenburger, muru yesterday


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • What do you get if you do printf "%s 123" "hello" "world", yes it surprised me as well, but it is in the manual.
    – ctrl-alt-delor
    2 days ago















up vote
10
down vote

favorite
3









up vote
10
down vote

favorite
3






3






This question already has an answer here:




  • How does printf with multiple arguments separated by space work

    1 answer




$ printf "%s" a b
ab$ printf "%s%s" a b
ab


I have some problem understand the format specifier for printf. If I am correct it is mostly the same as those for strings in the C programming language.



Why does the format specifier %s concatenate the two following strings together?



Why does %s not mean that there is only one string to substitute it, and ignore the remaining string?



Why are the results for two strings under %s and under %s%s the same?










share|improve this question









New contributor




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












This question already has an answer here:




  • How does printf with multiple arguments separated by space work

    1 answer




$ printf "%s" a b
ab$ printf "%s%s" a b
ab


I have some problem understand the format specifier for printf. If I am correct it is mostly the same as those for strings in the C programming language.



Why does the format specifier %s concatenate the two following strings together?



Why does %s not mean that there is only one string to substitute it, and ignore the remaining string?



Why are the results for two strings under %s and under %s%s the same?





This question already has an answer here:




  • How does printf with multiple arguments separated by space work

    1 answer








bash printf






share|improve this question









New contributor




Ben 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




Ben 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 2 days ago









Rui F Ribeiro

38.2k1475125




38.2k1475125






New contributor




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









asked 2 days ago









Ben

2788




2788




New contributor




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





New contributor





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






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




marked as duplicate by Isaac, thrig, RalfFriedl, Filipe Brandenburger, muru yesterday


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by Isaac, thrig, RalfFriedl, Filipe Brandenburger, muru yesterday


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • What do you get if you do printf "%s 123" "hello" "world", yes it surprised me as well, but it is in the manual.
    – ctrl-alt-delor
    2 days ago




















  • What do you get if you do printf "%s 123" "hello" "world", yes it surprised me as well, but it is in the manual.
    – ctrl-alt-delor
    2 days ago


















What do you get if you do printf "%s 123" "hello" "world", yes it surprised me as well, but it is in the manual.
– ctrl-alt-delor
2 days ago






What do you get if you do printf "%s 123" "hello" "world", yes it surprised me as well, but it is in the manual.
– ctrl-alt-delor
2 days ago












2 Answers
2






active

oldest

votes

















up vote
14
down vote



accepted










That’s how printf is specified to behave:




The format operand shall be reused as often as necessary to satisfy the argument operands. Any extra b, c, or s conversion specifiers shall be evaluated as if a null string argument were supplied; other extra conversion specifications shall be evaluated as if a zero argument were supplied. If the format operand contains no conversion specifications and argument operands are present, the results are unspecified.




In your case, the %s format is repeated as many times as necessary to handle all the arguments.



printf "%s" a b


and



printf "%s%s" a b


produce the same result because in the first case, %s is repeated twice, which is equivalent to %s%s.






share|improve this answer





















  • Does format specifier in C also work in the same or similiar way?
    – Ben
    2 days ago






  • 6




    No, it doesn’t — it ignores extra arguments. Modern C compilers will warn about mismatches between the format string and the arguments.
    – Stephen Kitt
    2 days ago






  • 5




    @Ben in C, the printf function is a variadic function: all the parameters but the first are accepted via .... This means that unless told via the first argument how many are present (and which types), the function can't ever know what the caller has passed to it. In the shell the command knows exactly how many command-line arguments it has.
    – Ruslan
    2 days ago




















up vote
7
down vote













If you supply more parameters to printf than the format string expects then the format string is repeated.



For example



$ printf "%s -- %s" a b c d e
a -- bc -- de --


We can see that the %s -- %s format is effectively repeated.



This can be useful; eg for formatting



$ printf "%s -- %sn" a b c d e
a -- b
c -- d
e --





share|improve this answer




























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    14
    down vote



    accepted










    That’s how printf is specified to behave:




    The format operand shall be reused as often as necessary to satisfy the argument operands. Any extra b, c, or s conversion specifiers shall be evaluated as if a null string argument were supplied; other extra conversion specifications shall be evaluated as if a zero argument were supplied. If the format operand contains no conversion specifications and argument operands are present, the results are unspecified.




    In your case, the %s format is repeated as many times as necessary to handle all the arguments.



    printf "%s" a b


    and



    printf "%s%s" a b


    produce the same result because in the first case, %s is repeated twice, which is equivalent to %s%s.






    share|improve this answer





















    • Does format specifier in C also work in the same or similiar way?
      – Ben
      2 days ago






    • 6




      No, it doesn’t — it ignores extra arguments. Modern C compilers will warn about mismatches between the format string and the arguments.
      – Stephen Kitt
      2 days ago






    • 5




      @Ben in C, the printf function is a variadic function: all the parameters but the first are accepted via .... This means that unless told via the first argument how many are present (and which types), the function can't ever know what the caller has passed to it. In the shell the command knows exactly how many command-line arguments it has.
      – Ruslan
      2 days ago

















    up vote
    14
    down vote



    accepted










    That’s how printf is specified to behave:




    The format operand shall be reused as often as necessary to satisfy the argument operands. Any extra b, c, or s conversion specifiers shall be evaluated as if a null string argument were supplied; other extra conversion specifications shall be evaluated as if a zero argument were supplied. If the format operand contains no conversion specifications and argument operands are present, the results are unspecified.




    In your case, the %s format is repeated as many times as necessary to handle all the arguments.



    printf "%s" a b


    and



    printf "%s%s" a b


    produce the same result because in the first case, %s is repeated twice, which is equivalent to %s%s.






    share|improve this answer





















    • Does format specifier in C also work in the same or similiar way?
      – Ben
      2 days ago






    • 6




      No, it doesn’t — it ignores extra arguments. Modern C compilers will warn about mismatches between the format string and the arguments.
      – Stephen Kitt
      2 days ago






    • 5




      @Ben in C, the printf function is a variadic function: all the parameters but the first are accepted via .... This means that unless told via the first argument how many are present (and which types), the function can't ever know what the caller has passed to it. In the shell the command knows exactly how many command-line arguments it has.
      – Ruslan
      2 days ago















    up vote
    14
    down vote



    accepted







    up vote
    14
    down vote



    accepted






    That’s how printf is specified to behave:




    The format operand shall be reused as often as necessary to satisfy the argument operands. Any extra b, c, or s conversion specifiers shall be evaluated as if a null string argument were supplied; other extra conversion specifications shall be evaluated as if a zero argument were supplied. If the format operand contains no conversion specifications and argument operands are present, the results are unspecified.




    In your case, the %s format is repeated as many times as necessary to handle all the arguments.



    printf "%s" a b


    and



    printf "%s%s" a b


    produce the same result because in the first case, %s is repeated twice, which is equivalent to %s%s.






    share|improve this answer












    That’s how printf is specified to behave:




    The format operand shall be reused as often as necessary to satisfy the argument operands. Any extra b, c, or s conversion specifiers shall be evaluated as if a null string argument were supplied; other extra conversion specifications shall be evaluated as if a zero argument were supplied. If the format operand contains no conversion specifications and argument operands are present, the results are unspecified.




    In your case, the %s format is repeated as many times as necessary to handle all the arguments.



    printf "%s" a b


    and



    printf "%s%s" a b


    produce the same result because in the first case, %s is repeated twice, which is equivalent to %s%s.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered 2 days ago









    Stephen Kitt

    158k23345421




    158k23345421












    • Does format specifier in C also work in the same or similiar way?
      – Ben
      2 days ago






    • 6




      No, it doesn’t — it ignores extra arguments. Modern C compilers will warn about mismatches between the format string and the arguments.
      – Stephen Kitt
      2 days ago






    • 5




      @Ben in C, the printf function is a variadic function: all the parameters but the first are accepted via .... This means that unless told via the first argument how many are present (and which types), the function can't ever know what the caller has passed to it. In the shell the command knows exactly how many command-line arguments it has.
      – Ruslan
      2 days ago




















    • Does format specifier in C also work in the same or similiar way?
      – Ben
      2 days ago






    • 6




      No, it doesn’t — it ignores extra arguments. Modern C compilers will warn about mismatches between the format string and the arguments.
      – Stephen Kitt
      2 days ago






    • 5




      @Ben in C, the printf function is a variadic function: all the parameters but the first are accepted via .... This means that unless told via the first argument how many are present (and which types), the function can't ever know what the caller has passed to it. In the shell the command knows exactly how many command-line arguments it has.
      – Ruslan
      2 days ago


















    Does format specifier in C also work in the same or similiar way?
    – Ben
    2 days ago




    Does format specifier in C also work in the same or similiar way?
    – Ben
    2 days ago




    6




    6




    No, it doesn’t — it ignores extra arguments. Modern C compilers will warn about mismatches between the format string and the arguments.
    – Stephen Kitt
    2 days ago




    No, it doesn’t — it ignores extra arguments. Modern C compilers will warn about mismatches between the format string and the arguments.
    – Stephen Kitt
    2 days ago




    5




    5




    @Ben in C, the printf function is a variadic function: all the parameters but the first are accepted via .... This means that unless told via the first argument how many are present (and which types), the function can't ever know what the caller has passed to it. In the shell the command knows exactly how many command-line arguments it has.
    – Ruslan
    2 days ago






    @Ben in C, the printf function is a variadic function: all the parameters but the first are accepted via .... This means that unless told via the first argument how many are present (and which types), the function can't ever know what the caller has passed to it. In the shell the command knows exactly how many command-line arguments it has.
    – Ruslan
    2 days ago














    up vote
    7
    down vote













    If you supply more parameters to printf than the format string expects then the format string is repeated.



    For example



    $ printf "%s -- %s" a b c d e
    a -- bc -- de --


    We can see that the %s -- %s format is effectively repeated.



    This can be useful; eg for formatting



    $ printf "%s -- %sn" a b c d e
    a -- b
    c -- d
    e --





    share|improve this answer

























      up vote
      7
      down vote













      If you supply more parameters to printf than the format string expects then the format string is repeated.



      For example



      $ printf "%s -- %s" a b c d e
      a -- bc -- de --


      We can see that the %s -- %s format is effectively repeated.



      This can be useful; eg for formatting



      $ printf "%s -- %sn" a b c d e
      a -- b
      c -- d
      e --





      share|improve this answer























        up vote
        7
        down vote










        up vote
        7
        down vote









        If you supply more parameters to printf than the format string expects then the format string is repeated.



        For example



        $ printf "%s -- %s" a b c d e
        a -- bc -- de --


        We can see that the %s -- %s format is effectively repeated.



        This can be useful; eg for formatting



        $ printf "%s -- %sn" a b c d e
        a -- b
        c -- d
        e --





        share|improve this answer












        If you supply more parameters to printf than the format string expects then the format string is repeated.



        For example



        $ printf "%s -- %s" a b c d e
        a -- bc -- de --


        We can see that the %s -- %s format is effectively repeated.



        This can be useful; eg for formatting



        $ printf "%s -- %sn" a b c d e
        a -- b
        c -- d
        e --






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 2 days ago









        Stephen Harris

        23k24176




        23k24176















            Popular posts from this blog

            If I really need a card on my start hand, how many mulligans make sense? [duplicate]

            Alcedinidae

            Can an atomic nucleus contain both particles and antiparticles? [duplicate]