我试图在C中创建一个函数,它将打印一个作为参数的字符串.这在C中甚至可能吗?
我的头文件中有这样的东西,但字符串不是有效的标识符.我知道C中没有字符串,但是string.h类是什么?
#include <string.h>
#ifndef _NEWMAIN_H
#define _NEWMAIN_H
#ifdef __cplusplus
extern "C" {
#endif
void print (string message){ //this is where i need help
printf("%s", message);
}
#ifdef __cplusplus
}
#endif
#endif /* _NEWMAIN_H */
Run Code Online (Sandbox Code Playgroud)
在C中,没有本机string类型.
C将字符串作为以null结尾的char数组处理.
例如:
char* string = "this is a string";
Run Code Online (Sandbox Code Playgroud)
的string.h存在对C字符串,这些类型的执行字符串操作函数char*.
使用时打印字符串时printf,会传入以下变量char*:
char* string_to_print = "Hello";
printf("%s", string_to_print);
Run Code Online (Sandbox Code Playgroud)
有关C中字符串的更多信息,C字符串上的Wikipedia页面将是一个良好的开端.