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#.
Please help.
In C, any non-zero value such as
5is considered to be “true”, therefore!5evaluates to0, or “false”. Thus,0 && 3is 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 typebool, which is completely independent of the integer types. See ! Operator in C#.