Why does C's printf format string have both

前端 未结 11 618
孤城傲影
孤城傲影 2020-12-02 15:17

Why does C\'s printf format string have both %c and %s?

I know that %c represents a single character and %s repre

相关标签:
11条回答
  • 2020-12-02 15:52

    The issue that is mentioned by others that a single character would have to be null terminated isn't a real one. This could be dealt with by providing a precision to the format %.1s would do the trick.

    What is more important in my view is that for %s in any of its forms you'd have to provide a pointer to one or several characters. That would mean that you wouldn't be able to print rvalues (computed expressions, function returns etc) or register variables.

    Edit: I am really pissed off by the reaction to this answer, so I will probably delete this, this is really not worth it. It seems that people react on this without even having read the question or knowing how to appreciate the technicality of the question.

    To make that clear: I don't say that you should prefer %.1s over %c. I only say that reasons why %c cannot be replaced by that are different than the other answer pretend to tell. These other answers are just technically wrong. Null termination is not an issue with %s.

    0 讨论(0)
  • 2020-12-02 15:56

    %s prints out chars until it reaches a 0 (or '\0', same thing).

    If you just have a char x;, printing it with printf("%s", &x); - you'd have to provide the address, since %s expects a char* - would yield unexpected results, as &x + 1 might not be 0.

    So you couldn't just print a single character unless it was null-terminated (very inefficent).

    EDIT: As other have pointed out, the two expect different things in the var args parameters - one a pointer, the other a single char. But that difference is somewhat clear.

    0 讨论(0)
  • 2020-12-02 15:57

    %s says print all the characters until you find a null (treat the variable as a pointer).

    %c says print just one character (treat the variable as a character code)

    Using %s for a character doesn't work because the character is going to be treated like a pointer, then it's going to try to print all the characters following that place in memory until it finds a null

    Stealing from the other answers to explain it in a different way.

    If you wanted to print a character using %s, you could use the following to properly pass it an address of a char and to keep it from writing garbage on the screen until finding a null.

    char c = 'c';
    printf('%.1s', &c);
    
    0 讨论(0)
  • 2020-12-02 15:59

    C has the %c and %s format specifiers because they handle different types.

    A char and a string are about as different as night and 1.

    0 讨论(0)
  • 2020-12-02 16:00

    Since no one has provided an answer with ANY reference whatsoever, here is a printf specification from pubs.opengroup.com which is similar to the format definition from IBM

    %c

    The int argument shall be converted to an unsigned char, and the resulting byte shall be written.

    %s

    The argument shall be a pointer to an array of char. Bytes from the array shall be written up to (but not including) any terminating null byte. If the precision is specified, no more than that many bytes shall be written. If the precision is not specified or is greater than the size of the array, the application shall ensure that the array contains a null byte.

    0 讨论(0)
提交回复
热议问题