Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

A C question

Brooks_Van_Horn
New Contributor I
280 Views

I am trying to implement some Fortran code based on some C code but there is a statement that I don't understand, so can someone please tell me whar does
    j += (i-j);

do? If i == j then is j incremented or is it incrementewd when they are no equal?

 

Thanks much,

Brooks

0 Kudos
4 Replies
mecej4
Honored Contributor III
280 Views

If you set aside any optimizations that the compiler can do, the following is a straightforward description of the effect of executing this statement.

The right hand side expression is evaluated with the current values of the variables i and j. The result is added to the current value of j, and the sum is stored in j.

Optimization may cause some of these operations to be bypassed if unnecessary, and memory reads and stores may be delayed or bypassed if j is already in a register or is used in the statements that come after the current one. The optimizer could note that the statement has the same effect as

     j = i

and implement this with load/store instructions without doing any arithmetic.

0 Kudos
Brooks_Van_Horn
New Contributor I
280 Views

You didn't answer my question. Ignore optimization. What is (i=j)  compiled to mean. If it is .true. or .false.? so what does "i += (i=j)" mean. If i == j then does this translate to i = i + 1? And if i /= j then does it translate to  i = i + 0? I know what += means I just want to know what "(i = j)" means related to the variable "i".

Brooks

0 Kudos
mecej4
Honored Contributor III
280 Views

What you ask in #3 is a different question than the one in #1. The way boolean expressions are handled is different in C and Fortran. In C, i=j; implies assignment, not comparison. If you use it as an expression or a sub-expression, the value is the result of the assignment. You seem to have the misconception that i=j is a boolean expression. You should consult an introductory book for C. The following program may help you understand how your two questions are answered in C.

#include <stdio.h>
int main(void){
int i,j;

i = 2; j = 3;
j += (i-j); printf("j = %d\n",j);   // #1
i = 2; j = 3;
i += (i=j); printf("i = %d\n",i);    // #3
}

The output from this program:

j = 2
i = 6

 

0 Kudos
Steven_L_Intel1
Employee
280 Views

Your original post had (I-j) which is subtraction. If indeed (I=j) is written, then the = in this case is assignment, not comparison. The result is that the value of j is assigned to i and then assigned back to j. I have to assume this is NOT what the actual code has - if it is, that's likely to be an error.

Now if the code actually has (I==j), then what gets assigned to j is 1 if I and j have the same value and 0 otherwise. C doesn't have the concept of .true. and .false..  If you wrote this same statement in Fortran, it would not be standard-conforming since you're not alloweed to assign a LOGICAL expression to an integer (I assume i and j are integers.) Intel Fortran lets you do this as an extension - you'll get -1 or 0 by default, or 1 and 0 if -standard-semantics is in effect.

Perhaps you need to restate the question and verify that you are representing the code correctly.

0 Kudos
Reply