如何在我的 C 代码中向操作系统返回一个字符串? [英] How can I return a string to the operating system in my C code?

查看:60
本文介绍了如何在我的 C 代码中向操作系统返回一个字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 C 初学者,这是我的 C 代码:

#include #include 主要的(){printf("你好,世界!\n");返回'ss';}

那会显示错误.那么如何在 C 代码中返回一个字符串呢?

解决方案

如果你想从一个函数(除了 main 之外)返回一个字符串,你应该做这样的事情.

>

#include const char * getString();int main(){printf("你好,世界!\n");printf("%s\n", getString());返回0;}const char * getString(){const char *x = "abcstring";返回 x;}

I am a C beginner and this is my C code:

#include <stdio.h>
#include <stdlib.h>

main()
{
    printf("Hello, World!\n");
    return 'sss';
}

That will show an error. So how can I return a string in C code?

解决方案

If you are looking to return a string from a function (other than main), you should do something like this.

#include <stdio.h>

const char * getString();

int main()
{
    printf("Hello, World!\n");
    printf("%s\n", getString());
    return 0;
}

const char * getString()
{
    const char *x = "abcstring";
    return x;
}

这篇关于如何在我的 C 代码中向操作系统返回一个字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆