2

I have a char pointer initialized to NULL at the beginning of the program, further in the program the char* is used in a function call where it might get pointed to a string of char s and it might point to null char, and it might remain untouched. So is the following statement correct, it should be if the expressions are evaluated from left to right. If not then strlen ( charpointer ) is undefined behavior, if charpointer == NULL

if (  charpointer == NULL || strlen ( charpointer ) == 0  )

So, do they get evaluated from left to right ? Is this the correct way to go about checking like this ?

StudentX
  • 2,243
  • 6
  • 35
  • 67
  • 7
    I'm sure this is a duplicate many times over. The operands of `||` are required to be evaluated left-to-right, making your expression safe (as long as `charpointer` is either a null pointer or a valid pointer to a string). For most other operators, the order of evaluation is unspecified. – Keith Thompson Aug 26 '13 at 18:29
  • 2
    Note that the order of evaluation has nothing to do with the fact that the expression is used as the condition in an `if` statement; it's entirely about the `||` operator. – Keith Thompson Aug 26 '13 at 18:44

5 Answers5

11

The order of evaluation for || is from left to right, as Eric mentions this is a special property of || and &&, most operators do not enforce left to right evalation. It will not evaluate the right expression if the left one succeeds, from the C99 draft standard section 6.5.14 paragraph 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.

The C++ draft standard has similar language in section 5.15 paragraph 1.

Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
4

The characteristics of the evaluation process have absolutely nothing to do with if statement. The evaluation is dictated by the properties of the expression itself. The fact that it is used in if makes no difference whatsoever.

In your case the expression in question is

charpointer == NULL || strlen ( charpointer ) == 0

Its behavior is primaruly defined by the properties of || operator, which is guaranteed to be evaluated from left to right, with first operand's evaluation being sequenced before the second operand's evaluation, and with "premature completion" ("short-circuiting") in case the first operand evaluates to true.

That means that the controlling expression in your original post is perfecly safe.

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
2

Order of evaluation is, as indicated by others from left to right but it is important to realize that evaluation stops at the point where the truth (or lack thereof) of the logical expression can be determined.

This is the case for both || and &&.

This is particularly important because you CANNOT assume that the entire expression will be evaluated so if any of the components of the logical expression have side-effects (such as function calls, increments, assignments, etc.,) they do not all have to be executed.

amrith
  • 953
  • 6
  • 17
2

To test charpointer to see if it is a null.

   if (charpointer == NULL) { // etc.

If you want to test if charponter is pointing to a NULL char, then:

   if ((charpointer != NULL) && (strlen(charpointer) == 0) { // etc.

You can combine these statements as:

   if (charpointer == NULL) {

   } else {
        if (strlen(charpointer) == 0) { // etc.

or as:

   if ((charpointer != NULL) && (strlen(charpointer) == 0)) {// etc.

or you can rely on short-circuiting which is what your code is doing. But, the test for charpointer == NULL MUST come first because the expression is evaluated left to right.

Finally after you have written a fair amount of c code, you learn that:

   if (! charpointer)  {  // etc.

is the same as testing it for being equal to NULL. So, just to be fancy and write unreadable code, then:

   if (!charpointer || !(strlen(charpointer)) { // etc.
JackCColeman
  • 3,777
  • 1
  • 15
  • 21
0

order of evaluation from left to right.

In your case if loop will check first condition from left to right. If first condition fails it will go and check second condition from left to right.

sujin
  • 2,813
  • 2
  • 21
  • 33
  • And the order of evaluation is due to the logical or operator, and has nothing to do with `if` or a general rule (as your edit suggests). – Sebastian Redl Aug 26 '13 at 18:36