views:

467

answers:

8
+2  Q: 

C boolean logic

I have been trying some programs in the C Language and come across to this...

#include<stdio.h>
int main()
{
    int j=3,k;
    k=!5&&j;
    printf("%d",k);
    return 0;
}

can anyone figure out what is the problem in this if i compile the program i will result to 0

and when i tried the same code in c#

 public void logic()
    {
        j = 5;
        k = !4 && j;
        Console.WriteLine("hence the value would be " + k);

    }

this will generate the error*( Error 1 Operator '!' cannot be applied to operand of type 'int' C:\Documents and Settings\SANDEEP\My Documents\Visual Studio 2005\Projects\ConsoleApplication18\ConsoleApplication18\Program.cs 21 17 ConsoleApplication18 )

i want to know why the output of my c code is not working and how can i use the ! operator in c#..... plz help ....

+9  A: 

5 when interpreted as a boolean value is true, so!5 gives the boolean result false, which causes && to give false as well. False interpreted as an int is 0.

Thomas
A: 

If you want to do bit arithmetic rather than simple boolean arithmetic, your C code is using the wrong operators. Here's a sample link with the correct operators.

In your current C code, k=!5&&j, 5 is considered true, so !5 is 0 (false). You then AND it with j, but any false ANDed with any boolean is false, so you get 0.

You probably want something like (tilda5)&j

Uri
The title says C boolean logic, not C bitwise operations.
Bertrand Marron
@fusbar The title was edited by me. The original specified only "logic".
anon
A: 

You are trying to do a logical NOT. You have to do a bitwise NOT on an int in C# (the ~ operator, not !). See here:

http://msdn.microsoft.com/en-us/library/d2bd4x66(VS.71).aspx

gmagana
+6  A: 

In C, any non-zero value such as 5 is considered to be "true", therefore !5 evaluates to 0, or "false". Thus, 0 && 3 is false as well. Logical operators in C.

In C#, the type system is a little stronger in this respect. The ! operator only works on values of type bool, which is completely independent of the integer types. See ! Operator in C#.

jleedev
+1; to be precise: any non-zero scalar value is considered 'true'
Christoph
+1  A: 

and how can i use the ! operator in c#.

That's the logical-not operator. You put it to the right of a bool and it produces the opposite. In C# there is no implicit conversion from integer to bool.

By the way, your "same code" in C# has different number values in it!

Daniel Earwicker
And, if you really want to achieve the same effect of the C logical-not on numbers in C# just use number==0.
Matteo Italia
+2  A: 

The readable C code: Compile it, run it, and how the expressions are evaluated.

#include <stdio.h>
int main() {
        int j = 3, k;
        k = !5 && j;

        printf("!5 = %d \n", !5);
        printf("j = %d \n", j);
        printf("!5 && j = %d \n", !5 && j);
        printf("k = %d \n",k);

        return 0;
}

You probably cannot apply "!" to ints in C# in this manner. I believe you can achieve that by using "~" on an int. You can aply "!" to a Boolean (I don't know how it's called in C#).

Andrei Ciobanu
+2  A: 

The equivalent code to this C code

#include<stdio.h> 
int main() 
{ 
    int j=3,k; 
    k=!5&&j; 
    printf("%d",k); 
    return 0; 
}

in C# would be

class Program {
static int Main() 
{ 
    int j=3, k; 
    k = !(5 != 0) && (j != 0) ? 1 : 0; 
    Console.WriteLine("{0}", k); 
    return 0; 
}
}

Notice that C# does not allow Boolean logic operators to be used on ints. If you want to convert ints to bools, you'll have to write code to do that, as I have here.

Both programs correctly output zero because 5 is not equal to 0, therefore !5 (C) or !(5!=0) (C#) is false. In C, the false converts to zero. In C#, the alternative of the conditional operator gives zero.

Does that answer your question?

Eric Lippert
A: 

In C# (and Java) you could do something like

k = (4 == 0) && (j != 0);

to get the same behaviour as your C program. Comparing an integer to 0 gives the same "logics" than C.

tristopia