我想打印字符串的一部分。我的程序应该打印从开始到字符串中的字符,例如这样的字符:\"
例如:
char* text = "abcdef\"ghij";
我想得到:abcdef
我没有使用 C 语言的经验,所以请以明确的方式帮助我。
最佳答案
使用*
字段宽度来选择要打印的字符数:
#include <stdio.h>
#include <string.h>
char* text = "abcdef\"ghij";
printf("%.*s\n", (int) (strchr(text, '"') - text), text);
关于c - 如何在C中打印字符串的一部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24229996/