The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Please explain output of this code
Discuss Please explain output of this code in the C Programming forum on Dev Shed. Please explain output of this code C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

December 22nd, 2012, 11:03 PM
|
|
Contributing User
|
|
Join Date: Feb 2010
Posts: 66
Time spent in forums: 14 h 6 m 25 sec
Reputation Power: 0
|
|
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:
|

December 23rd, 2012, 02:50 AM
|
 |
Contributing User
|
|
Join Date: Aug 2003
Location: UK
|
|
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.
|

December 23rd, 2012, 08:38 AM
|
|
Contributing User
|
|
Join Date: Feb 2010
Posts: 66
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...
|

December 23rd, 2012, 02:09 PM
|
 |
Contributed User
|
|
|
|
|
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.
|

December 24th, 2012, 08:16 AM
|
 |
Contributing User
|
|
Join Date: Aug 2003
Location: UK
|
|
It is all very simple and well defined except:
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.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|