#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2010
    Posts
    67
    Rep Power
    0

    Smile Please explain output of this code


    Code:
    #include<stdio.h>
    #include<conio.h>
    
    
    #define PRINT(int) printf("%d\n",int)
    	
    	void main()
    	{
    	int x,y,z;
    
    	x=2;
    	y=1;
    	z=0;
    
    	x=x&&y || z;
    	PRINT(x);
    	PRINT(x || !y&&z);
    
    	x=y=1; 
    	z=x ++ -1;
    	PRINT(x);
    	PRINT(z);
    	z+= -x ++ + ++ y;
    	PRINT(x); 
    	PRINT(z); 
    	z=x/++x; 
    	PRINT(z);
    
    getch();
    }

    output:
    Code:
    1
    1
    2
    0
    3
    0
    1
  2. #2
  3. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,109
    Rep Power
    1802
    Not another one!?

    The point of the code is to demonstrate the folly of writing such code. No other explanation is required - just don't write ridiculous code (if it was ever necessary to state that).


    Academics who think setting this sort of exercise is useful need a reality check. Few developers would have ever thought to write such code unless the idea were planted by such an exercise.

    Always remember what Brian Kernhigan says:
    Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
    Last edited by clifford; December 23rd, 2012 at 02:56 AM.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2010
    Posts
    67
    Rep Power
    0
    Originally Posted by clifford
    Not another one!?

    The point of the code is to demonstrate the folly of writing such code. No other explanation is required - just don't write ridiculous code (if it was ever necessary to state that).


    Academics who think setting this sort of exercise is useful need a reality check. Few developers would have ever thought to write such code unless the idea were planted by such an exercise.

    Always remember what Brian Kernhigan says:
    This was a puzzle that i didnt understand .... not my own ... if u feel comfortable you can explain even if its ridiculous...afterall aptitude has such ridiculous questions...
  6. #4
  7. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,379
    Rep Power
    1871
    It's your exam / homework / assignment, you figure it out.

    Well at least as much as you're able.

    Post your thoughts here and we'll tell you if you're on the right track or whether you need to re-think your approach.

    You're not going to get away with a "gimme the answers" post around here. Start pulling your own weight a bit.

    Start with the first one - it's pretty easy.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  8. #5
  9. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,109
    Rep Power
    1802
    It is all very simple and well defined except:

    Code:
    z+= -x ++ + ++ y;
    If you cannot fathom all except perhaps that part, then you are truly lost.

    Things you need:
    • In a boolean expression any non-zero operand is regarded as TRUE, zero is FALSE.
    • The value of a boolean expression resolving to TRUE when cast to an integer is 1.
    • TRUE OR <any-value> = TRUE
    • <any-value> OR TRUE = TRUE
    • The order of evaluation in the absence of parenthesis is determined by the operator order of precedence
    • The requirements of pre/post decrement/increment operators conflict with order of precedence in ways that are arcane, confusing, and not always well-defined - avoid.

IMN logo majestic logo threadwatch logo seochat tools logo