有没有办法只打印字符串的一部分?
例如,如果我有
char *str = "hello there";
有没有办法只打印 "hello"
,记住我要打印的子字符串是可变长度的,并不总是 5 个字符?
我知道我可以使用 for
循环和 putchar
或者我可以复制数组然后添加一个空终止符,但我想知道是否有更优雅的方式?
最佳答案
试试这个:
int length = 5;
printf("%*.*s", length, length, "hello there");
关于c - 在 C 中打印字符串的一部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4841219/