Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Example I don't get

Credits go to Evert, Karadoc ~~, miran, Peter Wang, and tobing for helping out!
This thread is locked; no one can reply to it. rss feed Print
Example I don't get
weapon_S
Member #7,859
October 2006
avatar

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:

1int depth_select(int depth) {
2 set_color_depth(depth);
3 return !set_gfx_mode(GFX_AUTODETECT, scr_width, scr_height, 0, 0);
4}
5 
6void 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?

gnolam
Member #2,030
March 2002
avatar

Quote:

I mean "||", what does this do?

... that's an OR. :P

--
Move to the Democratic People's Republic of Vivendi Universal (formerly known as Sweden) - officially democracy- and privacy-free since 2008-06-18!

weapon_S
Member #7,859
October 2006
avatar

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.

Ceagon Xylas
Member #5,495
February 2005
avatar

Tried using google?

Evert
Member #794
November 2000
avatar

Quote:

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.

Peter Wang
Member #23
April 2000

Quote:

I'm also not sure if the C standard guarentees the order of evaluation to be left to right.

For this, yes.

gnolam
Member #2,030
March 2002
avatar

It was a serious answer.

--
Move to the Democratic People's Republic of Vivendi Universal (formerly known as Sweden) - officially democracy- and privacy-free since 2008-06-18!

miran
Member #2,407
June 2002

Quote:

Tried using google?

How does one use google to find what || means?

--
sig used to be here

tobing
Member #5,213
November 2004
avatar

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);

Karadoc ~~
Member #2,749
September 2002
avatar

miran said:

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

-----------

weapon_S
Member #7,859
October 2006
avatar

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 ;D
Maybe another day...

gnolam
Member #2,030
March 2002
avatar

Quote:

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.

--
Move to the Democratic People's Republic of Vivendi Universal (formerly known as Sweden) - officially democracy- and privacy-free since 2008-06-18!

Audric
Member #907
January 2001

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

Evert
Member #794
November 2000
avatar

For the record, I wouldn't consider doing that in a C program to be good coding style...

Kikaru
Member #7,616
August 2006
avatar

Putting "||" into Google shows nothing. ;)

Thomas Fjellstrom
Member #476
June 2000
avatar

Or you could use your head and search for C Operator "or".

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Paul whoknows
Member #5,081
September 2004
avatar

This is how it looks like
150px-ORGate.png

____

"The unlimited potential has been replaced by the concrete reality of what I programmed today." - Jordan Mechner.

Ceagon Xylas
Member #5,495
February 2005
avatar

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.

weapon_S said:

I know it's probably a stupid question, because it is such a basic operator

He apparently knew what operators were.

Karadoc ~~ said:

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.

Johan Halmén
Member #1,550
September 2001

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]

Quote:

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.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Years of thorough research have revealed that the red "x" that closes a window, really isn't red, but white on red background.

Years of thorough research have revealed that what people find beautiful about the Mandelbrot set is not the set itself, but all the rest.

X-G
Member #856
December 2000
avatar

--
Since 2008-Jun-18, democracy in Sweden is dead. | 悪霊退散!悪霊退散!怨霊、物の怪、困った時は ドーマン!セーマン!ドーマン!セーマン! 直ぐに呼びましょう陰陽師レッツゴー!

Go to: