我有一个非常简单的程序来打印字符串中的字符,但由于某种原因它不起作用:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void * print_chars(char *process_string) {
int i;
int string_len;
string_len = strlen(process_string);
printf("String is %s, and its length is %d", process_string, string_len);
for(i = 0; i < string_len; i++) {
printf(process_string[i]);
}
printf("\n");
}
int main(void) {
char *process_string;
process_string = "This is the parent process.";
print_chars(process_string);
return 0;
}
当我在 Netbeans 中运行它时,我得到以下信息:
RUN FAILED (exit value 1, total time: 98ms)
如果我删除行
printf(process_string[i]);
程序运行但没有任何内容输出到控制台(很明显)。
知道我在这里遗漏了什么吗?
最佳答案
行中需要格式
printf(process_string[i]);
即
printf("%c", process_string[i]);
关于c - 在C中打印字符串中的所有字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13457616/