- void main()
-
{
-
int i=-3,j=2,k=0,m;
-
m=++i||++j&&++k;
-
printf("\n%d%d%d%d",i,j,k,m);
-
}
output -2 2 0 1
5 1289
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: - void main()
-
{
-
int i=-3, j=2, k=0, m;
-
m = ++i || ++j && ++k;
-
printf("\n%d%d%d%d", i, j, k, m);
-
}
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: - int void(){ /*...*/ }
-
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.
- int void(){ /*...*/ }
-
int void( int argc, char** argv){ /*...*/ }
huh huh huh he said "int void" :-)
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 >:)
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.
Great!
=)
first lesson fr a newbie(me :P) on these forums!!!
=P
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Yash |
last post by:
Hi,
Can someone please explain to me what the
StreamReader.DiscardBufferedData method does?
The documentation says
"Use DiscardBufferedData to seek to a known location in the underlying
stream...
|
by: Ina Schmitz |
last post by:
Hello all,
I don't succeed in displaying the explain plan. I use IBM DB2 Universal
Database 8.2.
I tried to do the example given in the online help for "Visual Explain".
The tables...
|
by: Jaspreet |
last post by:
I was recently asked this question in an interview. Unfortunately I was
not able to answer it and the interviewer made a decision on my C
strengths (or weekness) based on this single question and...
|
by: Patrick Hatcher |
last post by:
I have the following SQL with explain
Should I be concerned with the merge cond: Merge Cond:
(("outer".masked_acct_id)::text = "inner"."?column5?")? I have no idea
what column5 is or the same...
|
by: PINKA |
last post by:
can any one explain plz urgently
code was this
#include<stdio.h>
#include<conio.h>
void main()
{
double a=3.1428571;
int b=4347;
|
by: kabotnet |
last post by:
Hi,
I'm new in db2, I'm trying to execute EXPLAIN command on some queries
but i have error like:
And message similar to: Token EXPLAIN is not valid, valid tokens ( END
GET SET CALL DROP FREE...
|
by: td0g03 |
last post by:
Hello, I am new to C and I am new to English. I not sure what palindromes mean. I don't know exactly what my teacher wants me to do. If someone could explain it to me in a different way that would be...
|
by: sathishc58 |
last post by:
Hi All
Please explain why strlen returns() "16" as output here and explain the o/p for sizeof() as well
main()
{
char a={'a','b','c'};
printf("strlen=%d\n", strlen(a));...
|
by: lenygold via DBMonster.com |
last post by:
Total Cost: 43.712
Query Degree: 1
Rows
RETURN
( 1)
Cost
I/O
|
3.44
|
by: LanaR |
last post by:
Hello,
one sql statement is causing severe performance issue. The problem occurs only in UDB environment, the same statemnt on the mainframe is running fine. I have an explain output from the sql....
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |