Live data from Hacker News

Programming Idioms

programming-idioms.org

11–20 of 99 posts

Re: Programming Idioms

#12
post #7

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?

The 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.

Re: Programming Idioms

#13
post #12
post #7

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?

The 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` pref…

gets is inherently unsafe unless the input is guaranteed (externally) to never overflow the buffer.

Re: Programming Idioms

#14
post #7

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?

The 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 = '0' && s[i] 
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().

Re: Programming Idioms

#15
post #7

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?

Yes. 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).

Re: Programming Idioms

#16
post #7

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?

The 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 = '0' && s[i] 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().

Not to mention that proper idiom for this task would be:

  int n = strspn(s,"0123456789");
  BOOL b = (s[n] == 0);

Re: Programming Idioms

#17
post #7

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?

The 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 = '0' && s[i] 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().

It's not unreasonable to assume the compiler will optimize it to a single call. Though I guess people who are capable of making that judgement won't need to look this idiom up on the internet.

Re: Programming Idioms

#18

Earlier quoted context omitted.

The 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 = '0' && s[i] 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().

It's not unreasonable to assume the compiler will optimize it to a single call. Though I guess people who are capable of making that judgement won't need to look this idiom up on the internet.

Is there something in the C spec that allows optimizing to a single strlen call?

Re: Programming Idioms

#19

Earlier quoted context omitted.

It's not unreasonable to assume the compiler will optimize it to a single call. Though I guess people who are capable of making that judgement won't need to look this idiom up on the internet.

Is there something in the C spec that allows optimizing to a single strlen call?

Absolutely. The gist of it:

"In the abstract machine, all expressions are evaluated as specified by the semantics. An actual implementation need not evaluate part of an expression if it can deduce that its value is not used and that no needed side effects are produced (including any caused by calling a function or accessing a volatile object)."

Re: Programming Idioms

#20
post #7

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?

The 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 = '0' && s[i] 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().

I just checked godbolt [0]. gcc only calls strlen once even with -O0.

[0]:https://godbolt.org/z/j4o1915vE

Post reply on HN