8

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
zjm1126
  • 34,604
  • 53
  • 121
  • 166
  • 6
    `main` must return an `int`, no ifs, ands, or buts. What exactly do you expect to happen to this string? (Though the "string" is really a character constant since it uses `'` delimiters instead of `"`) – James McNellis Feb 08 '11 at 03:45
  • 1
    An error? Copy the error message exactly. What, precisely, did you expect to happen? – Josh Lee Feb 08 '11 at 03:49
  • 2
    There is a semicolon missing after the return statement; that is likely to be the error. Returning `'sss'` from `main` will technically work (since it is an `int`), but it is definitely not what is desired. – Jeremiah Willcock Feb 08 '11 at 03:55
  • 1
    This question needs to be clarified, many answers here assume that you're asking about: "How do I return a string to the operating system in C?" – Arafangion Mar 15 '11 at 14:20
  • 2
    Possible duplicate of [Returning C string from a function](http://stackoverflow.com/questions/1496313/returning-c-string-from-a-function) – Box Box Box Box Mar 14 '16 at 10:45

6 Answers6

12

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;
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
aNish
  • 1,079
  • 6
  • 11
4

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

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] = '\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;
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jay Shenawy
  • 953
  • 1
  • 12
  • 22
2

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

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;
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
FutureSci
  • 1,750
  • 1
  • 19
  • 27
1

Sadly there is no way to do that.

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

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;
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Chad
  • 43
  • 1
  • 9
1

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.

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.

dotalchemy
  • 2,459
  • 19
  • 24
-1

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

Peyman
  • 447
  • 3
  • 7