473,473 Members | 1,807 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Value of k??

In the following program I can't understand the value of k

#include<stdio.h>
int main()
{
int i=-3, j=2, k=0,m;

m= ++i&&++j||++k;

printf("\ni[%d],j[%d],k[%d],m[%d]\n",i,j,k,m);

return(0);
}

Nov 15 '05 #1
9 1812


Meenu wrote:
In the following program I can't understand the value of k

#include<stdio.h>
int main()
{
int i=-3, j=2, k=0,m;

m= ++i&&++j||++k;
/* if left expression of || results in true, right expression will not
be evaluated */
printf("\ni[%d],j[%d],k[%d],m[%d]\n",i,j,k,m);

return(0);
}


Nov 15 '05 #2


Meenu wrote:
In the following program I can't understand the value of k

#include<stdio.h>
int main()
{
int i=-3, j=2, k=0,m;

m= ++i&&++j||++k;

C logical operators use what is called "short-circuit" evaluation; if
the result of the first operand determines the result of the whole
expression, the second operand isn't even evaluated.

For example, given the expression

a && b

if a evaluates to 0 (false), then the whole expression will evaluate to
false regardless of the value of b, so b isn't evaluated at all.
Similarly, given

a || b

if a evaluates to non-zero (true), then the whole expression will
evaluate to true regardless of the value of b, so b isn't evaluated at
all.

Logical operator precedence is such that && expressions are evaluted
before || expressions, so your statement above is evaluated as

(++i && ++j) || ++k

since the results of both ++i and ++j are non-zero, the expression (++i
&& ++j) evaluates to true, so the whole expression (++i && ++j) || ++k
will evaluate to true regardless of k, so ++k is never evaluated, so k
remains 0.
printf("\ni[%d],j[%d],k[%d],m[%d]\n",i,j,k,m);

return(0);
}


Nov 15 '05 #3


Meenu wrote:
In the following program I can't understand the value of k

#include<stdio.h>
int main()
{
int i=-3, j=2, k=0,m;

m= ++i&&++j||++k;

printf("\ni[%d],j[%d],k[%d],m[%d]\n",i,j,k,m);

return(0);
}

Er,yeah.k=0.Just because ++i is TRUE and ++j is TRUE,so ++i&&++j is
TRUE of course.The compiler is lazy!If computes x||y and x is TRUE,it
does NOT compute y at all!So ++k isn't computed.(EOF)

Nov 15 '05 #4


Meenu wrote:
In the following program I can't understand the value of k

#include<stdio.h>
int main()
{
int i=-3, j=2, k=0,m;

m= ++i&&++j||++k;

printf("\ni[%d],j[%d],k[%d],m[%d]\n",i,j,k,m);

return(0);
}

PS:'return' is keyword,not operator.() is NOT neccessary.

Nov 15 '05 #5


Meenu wrote:
In the following program I can't understand the value of k

#include<stdio.h>
int main()
{
int i=-3, j=2, k=0,m;

m= ++i&&++j||++k;

printf("\ni[%d],j[%d],k[%d],m[%d]\n",i,j,k,m);

return(0);
}


and what about
m= ++i||++j&&++k;

Nov 15 '05 #6
On Fri, 22 Jul 2005 04:35:17 -0700, Meenu wrote:
and what about
m= ++i||++j&&++k;


Cong just explained how it works. If you can't apply his explanation to
another (practically identical) expression then there's no hope for you.

Nov 15 '05 #7


Meenu wrote:
Meenu wrote:
In the following program I can't understand the value of k

#include<stdio.h>
int main()
{
int i=-3, j=2, k=0,m;

m= ++i&&++j||++k;

printf("\ni[%d],j[%d],k[%d],m[%d]\n",i,j,k,m);

return(0);
}


and what about
m= ++i||++j&&++k;


If ++i evaluates to a non-zero (true) value, then the expression (++j
&& ++K) doesn't need to be evaluated, so neither ++j nor ++k are
evaluated.

Nov 15 '05 #8


Meenu a écrit :
Meenu wrote:
In the following program I can't understand the value of k

#include<stdio.h>
int main()
{
int i=-3, j=2, k=0,m;

m= ++i&&++j||++k;

printf("\ni[%d],j[%d],k[%d],m[%d]\n",i,j,k,m);

return(0);
}


and what about
m= ++i||++j&&++k;

i equals -3 at the beginning, then -2 so "++i" is true. It's not
necessary to go further in the evaluation of the entire expression,
i.e. j and k aren't incremented. It has already been explained by
others.
m = 1 (strictly, it's something different of zero (true)).
i = -2, j = 2, k = 0.

Please read your C book or lessons to know more about expressions, the
priority of operators and sequence points in C. It seems that you
expect quick answers without searching by yourself for some homework.

Regis

Nov 15 '05 #9

"Cong Wang" <xi************@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...


Meenu wrote:
In the following program I can't understand the value of k

#include<stdio.h>
int main()
{
int i=-3, j=2, k=0,m;

m= ++i&&++j||++k;

printf("\ni[%d],j[%d],k[%d],m[%d]\n",i,j,k,m);

return(0);
}

PS:'return' is keyword,not operator.() is NOT neccessary.


The return STATEMENT doesn't require parens because it is not a function.
Your statement implies that 'operators' require parenthesis... that is
incorrect.

Mark
Nov 15 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: G Kannan | last post by:
Hey all! I have written a perl script to retrieve information from a HTML Form and insert the data into an Oracle database table. I am gettting the the following error message: "Use of...
3
by: otto | last post by:
i need to read a variable in a javascript and translate it to a form in html the javascript variable is: <SCRIPT LANGUAGE='JavaScript'>RF2N('Total');</script> and i need to put that...
3
by: Eric Chang | last post by:
I was working on this simple form with radio boxes. And when I click on one of the radio box, it tell me the value is "undefined" Why is that ? I did defined the value of each radio box: ...
16
by: cwizard | last post by:
I'm calling on a function from within this form, and there are values set but every time it gets called I get slammed with a run time error... document.frmKitAmount.txtTotalKitValue is null or not...
4
by: dmiller23462 | last post by:
So here's my problem.....I need to set up different email distributions based on which option in the following Select form has been chosen....For instance if "Putaway" is chosen it needs to email...
7
by: matthew_carver | last post by:
Hello, I have an ASP page that loops through a SQL Server 2000 table, then downloads an Excel sheet the users can save, etc. Works fine, except, I see that in one particular "comments" field the...
13
by: dbuchanan | last post by:
Hello, Here is the error message; ---------------------------- Exception Message: ForeignKeyConstraint Lkp_tbl040Cmpt_lkp302SensorType requires the child key values (5) to exist in the...
0
by: tania | last post by:
i have this table in my database: CREATE TABLE FILM( F_ID INT(5) NOT NULL AUTO_INCREMENT, F_TITLE VARCHAR(40) NOT NULL, DIRECTOR_FNAME VARCHAR(20) NOT NULL, DIRECTOR_LNAME VARCHAR(20) NOT NULL,...
1
by: cbellew | last post by:
Hi guys, I have a problem with an option group and a two corresponding text boxes. When the user chooses an option value i want the text boxes to populate with text dependent on the choice made....
275
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
0
marktang
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,...
0
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...
0
jinu1996
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...
0
tracyyun
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.