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

&&

P: n/a
Hi,

I am have basic doubt in && operator

for example

what happens when we manipulate negative value and postive value using
'AND' operation
void main()
int j=-10,int k=11,c=0;
c=j++ && k++;
printf("j=%d\nk=%d\nc=%d\n",j,k,c);
return 0;
}

output of the above program is
j=-9
k=12
c=1
when the program run j is incremented to -9 and k is incremented to
12.
when we '&&' with false(i.e negative value) and true(i.e positive
value) output will be false(i.e c=0).but the above program yields
c=1.Please explain the details

Regards,
Mani

Feb 9 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
pl**********@gmail.com wrote:
Hi,

I am have basic doubt in && operator

for example

what happens when we manipulate negative value and postive value using
'AND' operation
void main()
int j=-10,int k=11,c=0;
c=j++ && k++;
printf("j=%d\nk=%d\nc=%d\n",j,k,c);
return 0;
}

output of the above program is
j=-9
k=12
c=1
when the program run j is incremented to -9 and k is incremented to
12.
when we '&&' with false(i.e negative value) and true(i.e positive
value) output will be false(i.e c=0).but the above program yields
c=1.Please explain the details


&& is 'logical AND'. The result of a && b depends on the following truth
table:

a b | a && b
-----------------
1 1 | 1
1 0 | 0
0 1 | 0
0 0 | 0

Note that both a and b must be non-zero for the result to be non-zero.

The last bit you perhaps need to know is that in a logical expression in C,
the operands are treated as either being true or false. Anything that
evaluates to 0 is false, and anything that evaluates to *anything else* is
true.

E.g.,

the result of -42 && 1000000 is true because -41 is not zero - so it's true,
and the same is true for 1000000.


--
==============
*Not a pedant*
==============
Feb 9 '06 #2

P: n/a

pl**********@gmail.com wrote:
Hi,

I am have basic doubt in && operator <snip>
when we '&&' with false(i.e negative value)
<snip>

Regards, Mani


Hi
In C, any non-zero value is true ; even negative numbers.
HTH
Regards,
Frodo son of Drogo.

Feb 9 '06 #3

P: n/a
pl**********@gmail.com wrote:
Hi,

I am have basic doubt in && operator

for example

what happens when we manipulate negative value and postive value using
'AND' operation
Exactly what you'd expect given C's approach to booleans.
void main()
NO. `main` is supposed to return `int`.
int j=-10,int k=11,c=0;
Missing "{". Illegal declaration - you can't have `int` after the `,`.

Since the program you ran isn't the program you showed us, anything
we say is likely to be a guess ...
when we '&&' with false(i.e negative value) and true(i.e positive
value)


.... such as, you didn't even /try/ to find out about C's booleans,
did you? A /fundamental/ part of the language is that zero (and the
null pointer) is (are) false, and everything else (that can be
boolean) is true.

--
Chris "try, or try not -- there is no do" Dollin
Feb 9 '06 #4

P: n/a

pl**********@gmail.com wrote:

[snip]
when we '&&' with false(i.e negative value) and true(i.e positive
value) output will be false(i.e c=0)


In a boolean context, only 0 evaluates to false; all non-zero values
(positive and negative) evaluate to true.

Feb 9 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.