21

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.

Paul Filch
  • 867
  • 2
  • 7
  • 13
  • 1
    Not. (True if `number` is zero, true otherwise.) As any C reference should have told you... – DevSolar Apr 04 '14 at 06:50
  • @DevSolar But I thought '!' can only be used for something like: while(number != 8) – Paul Filch Apr 04 '14 at 06:51
  • 2
    `while(!Number)` is equivalent to saying `while(Number == 0)` – Dayal rai Apr 04 '14 at 06:52
  • @PaulFilch: Be careful. `!=` is an operator in its own right -- not a combination of `!` and `=`. (The negation of an assignment wouldn't make much sense.) What source are you learning from? You *really* should use a book, not picking up tidbits online. Learning programming from online sources exposes you to all kinds of half-truths, incomplete information, bad habits, and sub-optimal examples. – DevSolar Apr 04 '14 at 06:55
  • 1
    @DevSolar I've been trying online tutorials, with a reference book by my side just in case. I guess that like with everything else on the internet there is some false info on C programming – Paul Filch Apr 04 '14 at 06:56
  • 2
    @PaulFilch: The vast majority of "programming tutorials" I have seen online so far have been written by people *who just had learned this stuff themselves*, because experienced developers don't often sit down and write stuff for beginners -- unless they're trying to make money from it, e.g. by selling a book (or farming reputation on StackOverflow ;-) ). For every language there are "standard" publications that are the recommended reading on the subject. For C, that would be Kerninghan & Ritchie's "The C Programming Language". – DevSolar Apr 04 '14 at 07:20

5 Answers5

19

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));
Santropedro
  • 105
  • 1
  • 7
gangadhars
  • 2,584
  • 7
  • 41
  • 68
10

In C, !number will evaluate to 1 if number == 0 and to 0 if number != 0. And in C, 1 is true and 0 is false.

Using an explicit comparison like number == 0 have the same effect but you might find it easier to read.

TheWalkingCube
  • 2,036
  • 21
  • 26
6

It's a negation or "not" operator. In practice !number means "true if number == 0, false otherwise." Google "unary operators" to learn more.

RobP
  • 9,144
  • 3
  • 20
  • 33
1

It is used for Negation of a number.It is a Unary Operator.

For Example:-

If we are using it with zero :- !0 then it will become 1

with one !1 = 0

Umesh
  • 11
  • 3
1

The negation operator (!) simply just reverses the meaning of its operand.

The operand or the expression must be of arithmetic or pointer type. But the operand/result of expression is implicitly converted to data type bool (boolean 0 means false, Non zero means True).

The result is true if the converted operand is false; the result is false if the converted operand is true. The result is of type bool.

so

while(!number)
{
    ...
}

since variable number is 0 , while(!number) ie, !0 which is 'negation of 0' which is 'TRUE' then it code enters the while loop()

Sorcrer
  • 1,634
  • 1
  • 14
  • 27