sprintf中的奇怪符号

Man*_*uro 1 c

我在C代码中有以下定义:

#define set_str(r,s) sprintf(r, "%-.*s", (int)sizeof(r)-1,s)
Run Code Online (Sandbox Code Playgroud)

我尝试在我的代码中调用函数set_str来理解符号的含义,但它没有给出任何特殊的格式或任何东西,变量只是按原样复制.谁能告诉我这是什么意思?

Omk*_*ant 5

它只是格式化的输出和一些字符.

%- >表示后面的字符%是占位符,将被相应的参数替换.

But here few things came into picture that's why you are confused.
Run Code Online (Sandbox Code Playgroud)

.- >正好*意味着sizeof(r)-1占用空间

*- >将指定要打印的字符的大小或宽度,*并将替换为sizeof(r)-1.

- - >用于左调整或对齐.

last s- >将替换s为string.

sprintf()意味着打印到缓冲区.在这种情况下它是r.

编辑:如果是.,请参阅此打印的一般情况.

printf("%sx.yz",args); 
// just forget about the `args` it can be as many as the format specifiers,
// it's an example for one argument.

s = sign, can be `+` or `-`.`+` means right adjustment `-`means left adjustment. 
x = At least `x` characters wide.
y = Exactly `y` charactes wide.
z = format specifier like `s`,`d`,`f` and so on as for corresponding arguments. `
Run Code Online (Sandbox Code Playgroud)