Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Exclamation Marks '!' in C

Tags:

c

int

punctuation

I have come across a problem involving exclamation marks and integers whilst reading a code in my reference book.

Let us say I have declared an integer variable named number - int number = 0;

I then use a while function involving an exclamation mark and number

while(!number)
{
    ...
}

I am confused with this because I do not know what does !number mean and what would be possible returned results? I am not sure if this can be used, but as I said, I saw it in my book.

Therefore, it would be great if someone could tell me what does !number mean and what does it evaluate?

Thank you in advance.

like image 835
Paul Filch Avatar asked Apr 04 '14 06:04

Paul Filch


1 Answers

We can treat ! as not. So if a number is non-zero (either positive or negative) it returns Zero. If it is zero, it returns 1.

int i = 13;
printf("i = %d, !i = %d\n", i, !i);
printf("!0 = %d\n", !(0));
like image 106
gangadhars Avatar answered Sep 17 '22 05:09

gangadhars