Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6635423
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:03:08+00:00 2026-05-25T23:03:08+00:00

Possible Duplicate: Is short-circuiting boolean operators mandated in C/C++? And evaluation order? AFAIK Short

  • 0

Possible Duplicate:
Is short-circuiting boolean operators mandated in C/C++? And evaluation order?

AFAIK Short circuit evaluation means that a boolean expression is evaluated only up to the point that we can guarantee its outcome.

This is a common idiom in perl where we can write things like:
(is_ok() returns non-zero value on “OK”)

is_ok() || die "It's not OK!!\n";

instead of

if ( ! is_ok() ) {
    die "It's not OK!!\n";
}

This only works because the order of evaluation is always left-to right and that guarantees that the rightmost statement is only executed if the first statement if not “false”.

In C I can do something simillar like:

struct foo {
    int some_flag;
} *ptr = 0;

/* do some work that may change value of ptr */
if ( 0!=ptr && ptr->some_flag ) {
    /* do something */
}

Is it safe to use this kind of idiom?

Or is there any chance that the compiler may generate code that evaluates ptr->some_flag before making sure that ptr is not a zero pointer? (I am assuming that if it is non-null it points to some valid memory region).

This syntax is convenient to use because it saves typing without losing readability (in my opinion anyway). However I’m not sure if it is entirely safe which is why I’d like to learn more on this.

NB: If the compiler has an effect on this, I’m using gcc 4.x

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T23:03:09+00:00Added an answer on May 25, 2026 at 11:03 pm

    The evaluation order of short-circuit operators (|| and &&) is guaranteed by the standard to be left to right (otherwise they would lose part of their usefulness).

    §6.5.13 ¶4

    Unlike the bitwise binary & operator, the && operator guarantees left-to-right evaluation;
    there is a sequence point after the evaluation of the first operand. If the first operand
    compares equal to 0, the second operand is not evaluated.

    §6.5.14 ¶4

    Unlike the bitwise | operator, the || operator guarantees left-to-right evaluation; there is
    a sequence point after the evaluation of the first operand. If the first operand compares
    unequal to 0, the second operand is not evaluated.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Is short-circuiting boolean operators mandated in C/C++? And evaluation order? Is there
Possible Duplicate: SQL Server - Query Short-Circuiting? Is the SQL WHERE clause short-circuit evaluated?
Possible Duplicate: Safety concerns about short circuit evaluation What does the standard say about
Possible Duplicate: Are PHP short tags acceptable to use? So I noticed that you
Possible Duplicate: Integer summing blues, short += short problem I feel dumb that this
Possible Duplicate: Calling Python from Objective-C I'm a long-time Python programmer and short-time Cocoa
Possible Duplicate: How do I write a short literal in C++? I can use
Possible Duplicate: Qt equivalent of PathAppend? Short story: does Qt 4 have an analog
Possible Duplicate: Convert some code from C++ to C I've got some code that
Possible Duplicate: Does java evaluate remaining conditions after boolean result is known Why do

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.