Why does C's printf format string have both

前端 未结 11 632
孤城傲影
孤城傲影 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: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.

提交回复
热议问题