Live data from Hacker News

Why 1 && 2 == 2

blog.chewxy.com

11–20 of 58 posts

Re: Why 1 && 2 == 2

#11
post #10
post #9

Earlier quoted context omitted.

He probably thought pointers are the only kind of variables and that he needed to dereference them. I imagine something like int *a = 1, *b = 2; printf("%d\n", *a && *b); Either that or he used `%s` as a format specifier. That seems more probable. Edit: Or, as I just realised the people below me meant, printf(a && b);

Nah mate, it's just some compiler flag that I didn't turn on (don't know which. my gcc-fu is fail since it's been really a long time since I touched C)

Just for my curiosity's sake, would you mind posting the code you tried that segfaulted?

Re: Why 1 && 2 == 2

#13
post #9
post #8

Earlier quoted context omitted.

I suck at C?

He probably thought pointers are the only kind of variables and that he needed to dereference them. I imagine something like int *a = 1, *b = 2; printf("%d\n", *a && *b); Either that or he used `%s` as a format specifier. That seems more probable. Edit: Or, as I just realised the people below me meant, printf(a && b);

[deleted]

Re: Why 1 && 2 == 2

#14
post #8
post #7

How did he get an expression that shouldn't involve any memory references to segfault?

I suck at C?

#include

int main(void){ printf("1 && 2 = %d\n", 1 && 2); printf("1 || 2 = %d\n", 1 || 2); }

What were you doing?

Re: Why 1 && 2 == 2

#15
post #9
post #8

Earlier quoted context omitted.

I suck at C?

He probably thought pointers are the only kind of variables and that he needed to dereference them. I imagine something like int *a = 1, *b = 2; printf("%d\n", *a && *b); Either that or he used `%s` as a format specifier. That seems more probable. Edit: Or, as I just realised the people below me meant, printf(a && b);

Or didn't put a format specifier, which makes sense coming from languages that don't have one in their print statements.

Re: Why 1 && 2 == 2

#16
post #13
post #9

Earlier quoted context omitted.

He probably thought pointers are the only kind of variables and that he needed to dereference them. I imagine something like int *a = 1, *b = 2; printf("%d\n", *a && *b); Either that or he used `%s` as a format specifier. That seems more probable. Edit: Or, as I just realised the people below me meant, printf(a && b);

[deleted]

Yes it will: http://ideone.com/DYmxj0

Re: Why 1 && 2 == 2

#17
post #15
post #9

Earlier quoted context omitted.

He probably thought pointers are the only kind of variables and that he needed to dereference them. I imagine something like int *a = 1, *b = 2; printf("%d\n", *a && *b); Either that or he used `%s` as a format specifier. That seems more probable. Edit: Or, as I just realised the people below me meant, printf(a && b);

Or didn't put a format specifier, which makes sense coming from languages that don't have one in their print statements.

Not putting a format specifier wouldn't cause a segfault.

Re: Why 1 && 2 == 2

#18
post #11
post #10

Earlier quoted context omitted.

Nah mate, it's just some compiler flag that I didn't turn on (don't know which. my gcc-fu is fail since it's been really a long time since I touched C)

Just for my curiosity's sake, would you mind posting the code you tried that segfaulted?

Yeah I'm dying to see the original C code but it isn't on the page anywhere, AFAICT.

From the article, about the C version: "Mine hadn't worked, which I suspect is some sort of compiler issue."

Yeah I'm going to go out on a limb and proclaim that there's no way this was due to a compiler "issue" in gcc. Not that gcc is perfect, but there's no way it is barfing on one of these handful of line programs that does nothing but some logical operations and printfs.

Re: Why 1 && 2 == 2

#19

For Python, a common idiom is to do this: condition and truecase or falsecase See "Boolean operations" and "Conditional expressions": http://docs.python.org/3/reference/expressions.html#boolean-...

This idiom is however out of flavor, unless you need to support python 2.4.

Nowadays you would do this: truecase if condition else falsecase

Re: Why 1 && 2 == 2

#20
post #17
post #15

Earlier quoted context omitted.

Or didn't put a format specifier, which makes sense coming from languages that don't have one in their print statements.

Not putting a format specifier wouldn't cause a segfault.

[deleted]
Post reply on HN