&&

P

plmanikandan

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
 
P

pemo

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.
 
C

Chris Dollin

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.
 
J

John Bode

(e-mail address removed) 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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top