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

前端 未结 6 500
星月不相逢
星月不相逢 2020-12-16 04:14

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

#include 
#include 

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


        
6条回答
  •  情书的邮戳
    2020-12-16 04:27

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

提交回复
热议问题