By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,775 Members | 1,425 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,775 IT Pros & Developers. It's quick & easy.

explain the output

P: 1
Expand|Select|Wrap|Line Numbers
  1. void main()
  2. {
  3.    int i=-3,j=2,k=0,m;
  4.    m=++i||++j&&++k;
  5.    printf("\n%d%d%d%d",i,j,k,m);
  6. }
output -2 2 0 1
Aug 6 '07 #1
Share this Question
Share on Google+
5 Replies


P: 19
Aright, I'll hold "the answer" until I or others will decide this isn't an assignment or something (because it sure looks like the standard C rookie exercise, despite the time of the year), but I would use the opportunity to point out:
  • There is a # sign when posting; this is used for wrapping up code. Please use it..
  • The first thing I thought when I saw your code was a lengthy praise on the importance of proper spacing.
I'll let the example speak for itself:
Expand|Select|Wrap|Line Numbers
  1. void main()
  2. {
  3.     int i=-3, j=2, k=0, m;
  4.     m = ++i || ++j && ++k;
  5.     printf("\n%d%d%d%d", i, j, k, m);
  6. }
On a "by the way" level, the void main part is usually regarded as bad practice by the more experienced people; I found that out in the more direct manner. This isn't just old hacker gruffness, it's actually on account of the ANSI C standard, which only supports two forms for main:
Expand|Select|Wrap|Line Numbers
  1. int void(){ /*...*/ }
  2. int void( int argc, char** argv){ /*...*/ }
The void main form is supported by most compilers, but it's still not standard, and because of that it might cause problems with other compilers; the gcc (GNU C Compiler) version has [used to have?] issues with it. No guarantees with more recent versions, so this last bit might be untrue.
Aug 6 '07 #2

P: 86
Expand|Select|Wrap|Line Numbers
  1. int void(){ /*...*/ }
  2. int void( int argc, char** argv){ /*...*/ }
huh huh huh he said "int void" :-)
Aug 6 '07 #3

P: 19
huh huh huh he said "int void" :-)
Aaaagh, int main... well, it's 22:28 here, and I do have a job. So that's my excuse.

So, any other errors in my post >:)
Aug 6 '07 #4

P: 19
Nobody said anything on the subject, so here comes the answer, as promised.

what's happening here, is that the compiler performs "short-circuit evaluation" or "minimal evaluation". It's a particular way of evaluating boolean operations, which relies on the fact that true || <anything> will always be true, and false && <anything> will always be false. So it makes sense for the compiler to evaluate any boolean expression until the answer is clear-cut, i.e. an expression with || has a member which is true, and an expression with && has a member which is false.

The practice is both praised and hated; its supporters point out various bits of nifty "deep magic" which can be performed with it, while its detractors call the technique an obfuscation and claim that it needlessly confuses more inexperienced programmers, who expect that all the components of an expression end up evaluated.

Back to our expression: in C, the && operator has higher priority than ||, so the expression becomes ++i || ( ++j && ++k ); at runtime, the first thing that will happen, is that ++i is executed; it evaluates to -2, which equates to TRUE, so the evaluation of the entire expression stops. The variable m receives TRUE, which is converted to int and becomes 1. (Interesting side-effect: in math, !!x = x, but in programs, !!5=1).
That's about it.
Aug 10 '07 #5

P: 2
Great!
=)

first lesson fr a newbie(me :P) on these forums!!!
=P
Aug 14 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.