Deutsch English Français Italiano |
<vrdb12$3q151$1@dont-email.me> View for Bookmarking (what is this?) Look up another Usenet article |
Path: ...!weretis.net!feeder9.news.weretis.net!news.quux.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!eternal-september.org!.POSTED!not-for-mail From: DFS <nospam@dfs.com> Newsgroups: comp.lang.c Subject: Re: Suggested method for returning a string from a C program? Date: Tue, 18 Mar 2025 22:43:47 -0400 Organization: A noiseless patient Spider Lines: 81 Message-ID: <vrdb12$3q151$1@dont-email.me> References: <vrd77d$3nvtf$2@dont-email.me> <87plidvkq0.fsf@nosuchdomain.example.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Injection-Date: Wed, 19 Mar 2025 03:43:47 +0100 (CET) Injection-Info: dont-email.me; posting-host="92f16538b7b31d02767c6fdbe2b76261"; logging-data="3998881"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19fwcgDjU24JdYs3n86DDp9" User-Agent: Betterbird (Windows) Cancel-Lock: sha1:R/JgawwP+5TSnE29Jsi+oQrUZ60= In-Reply-To: <87plidvkq0.fsf@nosuchdomain.example.com> Content-Language: en-US Bytes: 3339 On 3/18/2025 10:05 PM, Keith Thompson wrote: > DFS <nospam@dfs.com> 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 <stdio.h> >> #include <stdlib.h> >> #include <string.h> >> >> 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. It also compiles cleanly on theirs. > 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. Those were probably added by one of our newsreaders. Here's what it looks like on Notepad++ (showing end of line symbols) https://imgur.com/DTX9fZG > A minor point: You print a trailing space. I don't know whether > the auto-tester will accept that. I removed that, and no such luck. Thanks for looking at it.