10 ms·
The random idiom I got was: > Idiom #120 Read integer from stdin > Read an integer value from the standard input into variable n int n[15]; fgets(n, 15,
by Miiko 5y ago
The random idiom I got was:
> Idiom #120 Read integer from stdin
> Read an integer value from the standard input into variable n
int n[15];
fgets(n, 15, stdin);
Really?
- akomtu 5y agoYes. You asked for "an integer" from stdin? Here's your integer. Specify constraints better next time. (That's probably how the upcoming AI-assisted code gen tools will look like).
- WJW 5y agoThe longer I look at this example, the more weirdness I spot: - There are no standard integer types that take 15 (decimal) digits to represent. - The array contains ints instead of chars - Why would you use fgets() instead of just gets()? (Though I don't touch C very often so perhaps that is considered proper style) - Obviously no conversion of the digits into else, let alone specifying a base or handling a `0x` prefix for hexadecimal or a minus sign for negative numbers.
- leetcrew 5y agogets is inherently unsafe unless the input is guaranteed (externally) to never overflow the buffer.
- jhgb 5y ago> Why would you use fgets() instead of just gets()? I assume it's because gets() ranks as "-10: It's impossible to get right" on Rusty's API Design Manifesto? (http://sweng.the-davies.net/Home/rustys-api-design-manifesto http://sweng.the-davies.net/Home/rustys-api-design-manifesto)
- WJW 5y agoTIL. I have been leading a sheltered life in languages with garbage collectors that obscured the true horror of gets() from me.
- mpweiher 5y agoman gets ... SECURITY CONSIDERATIONS The gets() function cannot be used securely. Because of its lack of bounds checking, and the inability for the calling program to reliably determine the length of the next incoming line, the use of this function enables malicious users to arbitrarily change a running program's func- tionality through a buffer overflow attack. It is strongly suggested that the fgets() function be used in all cases. (See the FSA.)
- Someone 5y ago> There are no standard integer types that take 15 (decimal) digits to represent Nitpick: that is irrelevant. The code reads in at most 14 characters. > Why would you use fgets() instead of just gets()? You don’t use gets because it doesn’t exist anymore. It got removed in C11 (it rightfully was deemed so bad that backwards compatibility was sacrificed). You can use char *gets_s( char *str, rsize_t n ) , though. (https://en.cppreference.com/w/c/io/gets https://en.cppreference.com/w/c/io/gets)
- besnn00 5y agoas far as I saw they were pretty useless and the way they were presented wasn't that great (same language variants could be grouped in one commented section)
- jstanley 5y agoThe random idiom I got was: > Idiom #137 Check if string contains only digits > Set boolean b to true if string s contains only characters in range '0'..'9', false otherwise. char b = 0; for (int i = 0; i < strlen(s); i++) { if (! (b = (s[i] >= '0' && s[i] <= '9'))) break; } I appreciate the funny assignment-and-test-and-early-break in one (although I'd hardly say it's idiomatic), but I could do without the quadratic strlen().
- Miiko 5y agoNot to mention that proper idiom for this task would be: int n = strspn(s,"0123456789"); BOOL b = (s[n] == 0);
- Stratoscope 5y agoThat is nice and simple, but it makes ten comparisons for each character in s, where only two are needed. Of course it would be a good approach if the set of characters you're testing against is not contiguous, unlike 0..9.
- toxik 5y agoGood news then, it’s also linear time like the marginally faster but enormously grotesque for loop provided previously. I would take that clarity of intent a hundred times over squeezing a couple of comparisons out.
- Stratoscope 5y ago> it’s also linear time You raise an interesting point. It got me thinking about how big-O notation has failed us in some ways: it teaches us to ignore constant factors. In big-O, an algorithm that makes 1000 comparisons per element is no different from one that makes a single comparison per element. They are both linear time. But you can't deny that one of these will likely take 1000 times as long as the other. Of course, like you, I favor simple and readable code over grotesque code that is hard to understand and mentally verify.
- blunte 5y agoJust wait until this gets rolled into GitHub Copilot...
- still_grokking 5y agoWhat? You dare criticizing the concept of coding-AI trained on random code found on the inter-webs? But that's the future of programing! Just ask Microsoft or JetBrains. Soon, with the help of AI, any random dude will be empowered to write software! Big layoffs are to be expected as AI will take over most of the high-paying jobs in the software industry. Belief me. /s