Well it's not the examples themselves I don't get, but... to the point;
I downloaded these Allegro examples and in each there is this thing I don't get.
It's (in the last line) used for initialization. Here's the part I don't get:
1 | int depth_select(int depth) { |
2 | set_color_depth(depth); |
3 | return !set_gfx_mode(GFX_AUTODETECT, scr_width, scr_height, 0, 0); |
4 | } |
5 | |
6 | void init_all() { |
7 | allegro_init(); |
8 | |
9 | install_keyboard(); |
10 | install_timer(); |
11 | |
12 | LOCK_VARIABLE(elapsed_time); |
13 | LOCK_FUNCTION(__inc_elapsed_time); |
14 | install_int_ex(__inc_elapsed_time, BPS_TO_TIMER(1000)); |
15 | |
16 | depth_select(32) || depth_select(24) || depth_select(16) || depth_select(15);//?? |
17 | } |
Now, I'm probably not being too bright, but what is that last line? I know about set_color_depth, but this seems weird. I mean "||", what does this do?
I mean "||", what does this do?
... that's an OR.
Hilarious... T_T
I know it's probably a stupid question, because it is such a basic operator, but I would appreciate a serious answer. Really I would.
Tried using google?
Now, I'm probably not being too bright, but what is that last line? I know about set_color_depth, but this seems weird. I mean "||", what does this do?
A feature of short-circuit logic. If the first operand evaluates to TRUE, the second operand is not evaluated at all (since its value is not important for wether the end result is TRUE or FALSE).
This "trick" is common in shellscripts or Perl code (eg, open FILE, "<file" || die "file not found"; ), but I've never seen it used in C code. I'm also not sure if the C standard guarentees the order of evaluation to be left to right.
I'm also not sure if the C standard guarentees the order of evaluation to be left to right.
For this, yes.
It was a serious answer.
Tried using google?
How does one use google to find what || means?
What it does: If the first fails (i.e. evaluates to false) then the second is evaluated, if that fails then the third is evaluated etc.
Would be equivalent to
if(!depth_select(32)) if(!depth_select(24)) if(!depth_select(16)) depth_select(15);
[quote Ceagon Xylas]Tried using google?
How does one use google to find what || means?
</quote>I was wondering the same thing. Actually, I thought the google comment was rude and unhelpful. It's like just posting "I'm not going to help you, work it out yourself - idiot."
If you don't want to help, just don't help. There is no need to announce that you aren't going to help.
Ah, yes, thanks.
( I figured it out when I was off-line; amazing how just posting a question makes you think...) Does anyone know whether this works for sure? I.e. there's no compiler which scrambles the order for optimization?
And I don't do shellscripts... so that's probably why...
Now I need help understanding the Exlights example
Maybe another day...
I.e. there's no compiler which scrambles the order for optimization?
If they do, they're broken. Left-to-right order and minimal evaluation is guaranteed by the standard.
What gnolam says.
It allows expressions like :
if (bmp != NULL && bmp->width > SCR_WIDTH) {/*do stuff*/}
The second term would horribly break if it was evaluated before of the first - or when the first was already known to be false.
edit: For the record, AFAIK, the only non-determined order of evaluation is functions arguments:
i=0; printf ("%d %d\n", i++, i++);
It could print "1 2" or "2 1".
For the record, I wouldn't consider doing that in a C program to be good coding style...
Putting "||" into Google shows nothing.
Or you could use your head and search for C Operator "or".
This is how it looks like
Well, wasn't trying to be rude, but I googled 'C++ operator' As you would expect, it came up with a list of operators including ||, the or operator... This page also proceeded to explain what that operator does.
I know it's probably a stupid question, because it is such a basic operator
He apparently knew what operators were.
If you don't want to help, just don't help. There is no need to announce that you aren't going to help.
I guess my help was: I found it by this simple method which I'll now remind you to use. It discourages having it handed to you.
Don't ever adapt that coding style. It is just stupid, even though it looks neat. Depend more on your compiler's ability to optimize your nested ifs. IIRC there's a compiler option that makes all comparisons evaluated in a line like that. If you ever need to set that option for any reason, you don't want your program to run in wrong colour depth.
I avoid the following, too:
if (b == 0 || a / b > foo) bar();
Instead:
if (b == 0) bar(); else if (a / b > foo) bar();
For same reason.
[edit]
Well, wasn't trying to be rude, but I googled 'C++ operator'
I remember when I first saw || in the code (might have been just |), ages ago. Even though I knew some C and had coded simple games, I just didn't recognise that it was an operator. I hardly knew the word 'operator'. OTOH there was no Google at that time.