zjm1126 zjm1126 - 4 months ago 6
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>
main()
{
printf("Hello world !\n");
return 'sss';
}


that will show an error,

so how to return a string in C code?

Answer

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