0
 #include <stdio.h>
 #include <stdlib.h>

 #define comp(a,b) ((a)*(a)+(b)*(b))

 void main(void){
     int x=1;
     int y=2;
     int z=comp(x++,++y);
     printf("x = %d \ny= %d\nz= %d\n",x,y,z);
     system("pause");
 } 

output on windows dos .. when use notepad and gcc compilor directly x=3 ,y=4,z=17 output with same code compiled on codeblocks is x=3 ,y=4,z=18 please help me .. i want to know steps code will do .. i am beginner .

Ulfalizer
  • 4,424
  • 1
  • 20
  • 28
  • 2
    Due to macro is is not an exact dup but pretty close to [Why are these constructs (using ++) undefined behavior?](http://stackoverflow.com/q/949433/1708801) – Shafik Yaghmour Mar 29 '15 at 00:51
  • then which answer is right ? .. – ياسر محمد Mar 29 '15 at 00:54
  • 4
    It is undefined behavior which basically means unpredictable behavior, so they are both right. So don't code like that, read some of more detailed answers in the linked question for more information. – Shafik Yaghmour Mar 29 '15 at 00:56
  • Thanks very much #Shafik Yaghmour ,was very helpful ... will continue my search about undefined behavior .. – ياسر محمد Mar 29 '15 at 01:15
  • in general, read the appropriate C standard. (sadly, it is not free) I think the c11 is the latest standard. If it is not specifically defined in the standard, then it is undefined behaviour. – user3629249 Mar 29 '15 at 01:46
  • don't use tabs in source code for indentation, they really mess up the displayed code, most every editor/word processor has a different setting about the width of a tab and where the tab stops are placed. Do use a consistent indentaion amount (I prefer 4 spaces as it is very readable, even with a variable width font. – user3629249 Mar 29 '15 at 01:49
  • there are only 2 ways to declare the main function: 1) int main( void ) 2) int main( int argc, char* argv[] ) The exact names of the parameters are up to the code writer, but argc and argv are well known and expected. some compilers will allow: int main() . therefore, 'void main( void ) is NOT valid – user3629249 Mar 29 '15 at 01:52
  • the posted invocation of the 'comp' macro has 'side effects'. (the side effects are the '++' sequences. It is not defined as to when the side effects are implemented in the execution sequence, so the result is undefined behaviour. In general, this means the order of execution is completely up to the compiler manufacture. I.E. it can be different in each compiler, even in different revisions of the same compiler – user3629249 Mar 29 '15 at 01:55
  • If you are writing code that its difficult to work out what is happening then perhaps it is a good idea to change your coding habits. If I was reviewing your code I would reject it. Have some pity on the poor person that has to maintain the code in the future – Ed Heal Mar 29 '15 at 07:22
  • Flagged as duplicate. A close enough version of the standard is available [here](https://raw.githubusercontent.com/pragma-/pbot/master/modules/n1570.txt). `Annex J.2` contains a listing of undefined behaviors, here the relevant part is: `A side effect on a scalar object is unsequenced relative to either a different side effect on the same scalar object or a value computation using the value of the same scalar object (6.5).` – a3f Mar 29 '15 at 07:33

0 Answers0