c - 如何在不使用 C 语言标准库的情况下显示字符串值?

标签 c

如何在不使用 C 语言标准库的情况下显示字符串值?请看下面的代码:

//without using the standard libraries that other created it
int main() {
string str = "Hello";
//How do I can display str value without using the standard libraries that other created it?
}

最佳答案

这里是你如何做到的:

// Declare the prototype for the write() function,
// with unspecified parameters because why not.
extern long write();

int main(void) {
    char const *str = "Hello";

    // Retrieve the length
    unsigned long size = 0;
    while(str[size])
        ++size;

    // Write to stdout (fd 1)
    write(1, str, size);

    return 0;
}

Live on Coliru .

当然,它是不可移植的,并且可能无法在大多数系统上链接或触发 UB,除了我从中提取声明的系统(这是一个 Linux 系统调用,在 中声明) unistd.h 我从 Coliru 中检索到的)。但是,这就是我们首先拥有标准库的原因。

关于c - 如何在不使用 C 语言标准库的情况下显示字符串值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30942275/

相关文章:

更改用户 ID 以分配附加功能

c - 浮点模运算

c - 通过 mex 传回数组

c - mkfifo() 和 mknod() 的区别

c - 了解典型系统中带符号数和负数表示的按位求反

c - 在 Linux 中查找链接到二进制文件的 SQLite 版本

c - C 中的常量值错误

c - Linux 内核链接

c - 从 STDIN 读取,鸡计划

c - 通过引用传递c中的指针数组