我怎么可以显示不使用标准库,C语言的字符串值? [英] How do I can display a string value without using the standard libraries in C language?
本文介绍了我怎么可以显示不使用标准库,C语言的字符串值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我怎么可以显示不使用标准库,C语言的字符串值?请参阅下面的code:
//而不使用其他创建它的标准库
诠释主(){
字符串str =你好;
//我怎么可以不使用其他创建它的标准库展示海峡价值?
}
解决方案
下面是你如何能做到这一点:
//声明原型为write()函数,
与未指定的参数//因为何乐而不为。
EXTERN长的write();诠释主要(无效){
字符常量*海峡=你好; //检索长度
无符号长大小= 0;
而(STR【尺寸】)
++大小; //写入stdout(FD 1)
写(1,STR,大小); 返回0;
}
住在Coliru 。
当然,它的非便携,因为它得到,并可能无法在大多数比我拉离(这是一个Linux系统调用,在<$ C宣布报关其他系统连接或触发UB $ C> unistd.h中这是我从Coliru检索)。但随后,这就是为什么我们必须摆在首位的标准库。
How do I can display a string value without using the standard libraries in C language? please see the following code:
//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?
}
解决方案
Here's how you can do 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;
}
Of course, it's as non-portable as it gets, and will probably fail to link or trigger UB on the majority of systems other than the one I pulled the declaration from (it's a Linux system call, declared in unistd.h
which I retrieved from Coliru). But then, that's why we have the standard library in the first place.
这篇关于我怎么可以显示不使用标准库,C语言的字符串值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文