Path: csiph.com!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: Suggested method for returning a string from a C program? Date: Tue, 18 Mar 2025 19:05:11 -0700 Organization: None to speak of Lines: 64 Message-ID: <87plidvkq0.fsf@nosuchdomain.example.com> References: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Injection-Date: Wed, 19 Mar 2025 03:05:13 +0100 (CET) Injection-Info: dont-email.me; posting-host="6b2adc3539d30183fea78a893643ef1f"; logging-data="3935211"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+tj6M1IS8ExPAUCjh9DynV" User-Agent: Gnus/5.13 (Gnus v5.13) Cancel-Lock: sha1:UfnTzVXc6bOy/TEUyX2e/tZ9+KY= sha1:A9CByKALBQCCBfNEOwv2fgfOlUM= Xref: csiph.com comp.lang.c:391320 DFS writes: > I'm doing these algorithm problems at > https://cses.fi/problemset/list/ > > For instance: Weird Algorithm > https://cses.fi/problemset/task/1068 > > My code works fine locally (prints the correct solution to the > console), but when I submit the .c file the auto-tester flags it with > 'runtime error' and says the output is empty. > > ------------------------------------------------------------ > // If n is even, divide it by two. > // If n is odd, multiply it by three and add one. > // Repeat until n is one. > // n = 3: output is 3 10 5 16 8 4 2 1 > > > #include > #include > #include > > int main(int argc, char *argv[]) > { >     int n = atoi(argv[1]); >     int len = 0; >     char result[10000] = ""; >     sprintf(result, "%d ", n); > >     while(1) { > if((n % 2) == 0) > {n /= 2;} > else > {n = (n * 3) + 1;} > > if(n != 1) > { > len = strlen(result); > sprintf(result + len, "%d ", n); > } > else > break; >     } > >     len = strlen(result); >     sprintf(result + len, "1 "); >     printf("%s\n",result); > >     return 0; > } > ------------------------------------------------------------ I don't see any problem with the code, and neither does gcc on my system. But the code you posted contains a number of NO-BREAK SPACE characters (0xa0). "clang -Wno-unicode-whitespace" accepts those characters without complaint, and gives non-fatal warnings without that option. gcc treats them as a fatal error. A minor point: You print a trailing space. I don't know whether the auto-tester will accept that. -- Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com void Void(void) { Void(); } /* The recursive call of the void */