C编程.如何制作打印字符串的方法

use*_*514 1 c string

我试图在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)

coo*_*ird 5

在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页面将是一个良好的开端.

  • 我认为大多数C编译器都允许你使用非常量指针到字符串文字,但"这是一个字符串"真的是const,应该由const char*指向. (2认同)