zjm1126 zjm1126 - 6 months ago 14
C Question

How to return a string in my C code?

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

#include <stdio.h>
#include <stdlib.h>
printf("Hello world !\n");
return 'sss';

that will show an error,

so how to 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;