Home > Back-end >  In c language on the define and the increase of the decrement operator
In c language on the define and the increase of the decrement operator

Time:09-21

 
this code is



# include
# define SQ (y) (y) * (y))
Int sq (int y)
{
Return ((y) * (y));
}
Int main ()
{
Int I=1, j=1;
While (i
=5)Printf (" % d \ n ", SQ (i++));
printf("\n");
While (j
=5)Printf (" % d \ n ", sq (j++))
;
printf("\n");
return 0;
}
Why is 1 3 party 5 party SQ in
Hope that they can help me answer the bosses

CodePudding user response:


Because SQ (i++) is equal to
(i++) * (i++)

CodePudding user response:

Your macro definition belong to undefined behavior
The equivalent of
While (i<{
=5)Printf (" % d \ n ", (i++) * (i++));//macro definition is just your expression, the compiler is different, the result will be different, it is impossible to know the results
}

The function is different
Sq (j++) is equivalent to sq (j), j++; Such a comma expression, the result is knowable


CodePudding user response:

refer to the second floor qybao response:
your macro definition belong to undefined behavior
The equivalent of
While (i<{
=5)Printf (" % d \ n ", (i++) * (i++));//macro definition is just your expression, the compiler is different, the result will be different, it is impossible to know the results
}

The function is different
Sq (j++) is equivalent to sq (j), j++; Such a comma expression, the result is knowable
(i++) * (i++) here is the multiplication first, then the operation since to add?
  • Related