C/C++

NAVIGATION
CATEGORIES
REFERRENCE
LINKS
  • &&

    4 answers - 653 bytes - related search similar search Add To My Delicious Add To My Stumble Upon Add To My Google Mark Add To My Facebook Add To My Digg Add To My Reddit

    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
  • No.1 | | 1271 bytes | |

    plmanikandan@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.
  • No.2 | | 337 bytes | |


    plmanikandan@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.

  • No.3 | | 909 bytes | |

    plmanikandan@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()

    N `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.
  • No.4 | | 310 bytes | |


    plmanikandan@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.

Re: &&


max 4000 letters.
Your nickname that display:
In order to stop the spam: 6 + 5 =
QUESTION ON "C/C++"

EMSDN.COM