如何在不使用 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;
}
当然,它是不可移植的,并且可能无法在大多数系统上链接或触发 UB,除了我从中提取声明的系统(这是一个 Linux 系统调用,在 中声明) unistd.h
我从 Coliru 中检索到的)。但是,这就是我们首先拥有标准库的原因。
关于c - 如何在不使用 C 语言标准库的情况下显示字符串值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30942275/