C语言 如何在我的 C 代码中向操作系统返回一个字符串?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4929329/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 07:43:55  来源:igfitidea点击:

How can I return a string to the operating system in my C code?

cstringreturn

提问by zjm1126

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

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

#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?

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

回答by aNish

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

如果您希望从函数(除了main)返回一个字符串,您应该这样做。

#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;
}

回答by Mohamed El Shenawy

The magic is in the key word staticwhich preserves the memory content of the string even after the function ends. (You can consider it like extending the scope of the variable.)

神奇之处在于static即使在函数结束后仍保留字符串的内存内容的关键字。(您可以将其视为扩展变量的范围。)

This code takes one character each time, then concatenates them in a string and saves it into a file:

这段代码每次取一个字符,然后将它们连接成一个字符串并将其保存到一个文件中:

#include <stdio.h>
#include <conio.h>

char* strbsmallah ()
{
  static char input[50];
  char position = 0, letter;
  scanf("%c", &letter);
  while (letter != '~') { // Press '~' to end your text
    input[position] = letter;
    ++position;
    scanf("%c", &letter);
  }
  input[position] = '
void foo(char **value_to_return) {
    *value_to_return = malloc(256); // Store 256 characters
    strcpy(*value_to_return, "deposited string");
}

int main() {
    char *deposit;
    foo(&deposit);
    printf("%s", deposit);
    return 0;
}
'; char *y; y = (char*) &input; //printf("%s\n ", y); return y; } int main() { printf("\n"); FILE *fp; fp = fopen("bsmallah.txt", "w+"); fprintf(fp, strbsmallah()); while (!_kbhit()) ; return 0; }

回答by FutureSci

You could do this in a way similar to scanf. In other words:

您可以通过类似于scanf. 换句话说:

int main()
{
    int err = 0; // 0 is "success" is most C programs
    printf("Hello, World!!\n");

    switch( err )
    {
      case 0:
        printf("Program shutdown successfully!\n");
        break;
      case 1:
        printf("We had an issue somewhere. Please fix your input data\n");
        break;
      //case 2, 3, etc...
    };

   return err;
}

回答by Chad

Sadly there is no way to do that.

可悲的是没有办法做到这一点。

You could add something to the end of your C program like:

您可以在 C 程序的末尾添加一些内容,例如:

##代码##

回答by dotalchemy

You don't return a string. Applications exit with an integer exit code.

你不返回一个字符串。应用程序以整数退出代码退出。

Conventionally, exiting with a return of 0 will always show that your application exited without error / completed. You return an integer other than 0 to show that your application exited abnormally.

通常,以 0 返回退出将始终表明您的应用程序退出时没有错误/已完成。您返回 0 以外的整数以表明您的应用程序异常退出。

You could throw an exception and handle it with a logging method higher in the call stack, or you could just return something other than 0 and make sure you had it documented in your release notes as to what each error integer means.

您可以抛出异常并使用调用堆栈中更高的日志记录方法来处理它,或者您可以只返回 0 以外的值,并确保在您的发行说明中记录了每个错误整数的含义。

回答by Peyman

You might be able to use environment variables for that. Not sure though.

您也许可以为此使用环境变量。虽然不确定。