C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 22nd, 2012, 11:03 PM
swapy swapy is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2010
Posts: 66 swapy Negative: is most likely a SPAMMER and a traitor to the cause. 
Time spent in forums: 14 h 6 m 25 sec
Reputation 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

Reply With Quote
  #2  
Old December 23rd, 2012, 02:50 AM
clifford's Avatar
clifford clifford is offline
Contributing User
Dev Shed Demi-God (4500 - 4999 posts)
 
Join Date: Aug 2003
Location: UK
Posts: 4,824 clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 2 Days 21 h 1 m
Reputation Power: 1800
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:
Quote:
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.

Reply With Quote
  #3  
Old December 23rd, 2012, 08:38 AM
swapy swapy is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2010
Posts: 66 swapy Negative: is most likely a SPAMMER and a traitor to the cause. 
Time spent in forums: 14 h 6 m 25 sec
Reputation Power: 0
Quote:
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...

Reply With Quote
  #4  
Old December 23rd, 2012, 02:09 PM
salem's Avatar
salem salem is online now
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,905 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 4 Days 1 h 9 m 41 sec
Reputation Power: 1774
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

Reply With Quote
  #5  
Old December 24th, 2012, 08:16 AM
clifford's Avatar
clifford clifford is offline
Contributing User
Dev Shed Demi-God (4500 - 4999 posts)
 
Join Date: Aug 2003
Location: UK
Posts: 4,824 clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 2 Days 21 h 1 m
Reputation Power: 1800
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Please explain output of this code

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap