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

标签 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 */

最佳答案

在 C 中,没有原生的 string 类型。

C 将字符串作为以 null 结尾的 char 数组处理。

例如:

char* string = "this is a string";

string.h存在以对类型为 char* 的 C 字符串执行字符串操作函数。

使用 printf 打印字符串时, 将传入一个 char* 变量:

char* string_to_print = "Hello";
printf("%s", string_to_print);

有关 C 中字符串的更多信息,Wikipedia page on C strings将是一个好的开始。

关于C编程。如何制作打印字符串的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1414052/

相关文章:

r - 从 R 中的字符串中提取最后 n 个字符

c++ - 如何将 CString 转换为字节

Python 3 [类型错误 : 'str' object cannot be interpreted as an integer] when working with sockets

c - 我是否要转换 malloc 的结果?

c - 如何仅使用 c 获取 eflags

c - 为什么正则表达式不是 ISO C99 的一部分

java - JAVA中的递归字符串

c++ - std::stringstream 与 std::string 用于连接多个字符串

c - 意外的 pthread 函数输出

c - 如何使用 strcmp 比较 csv 数据中的名称