-4

I was reading a code and I found these "!" all over the place what is it supposed to do ? this is a part from the code I found it in :

 if (!piocherMot(motSecret))
    exit(0);

piocherMot is a function in another file while "motsecret" is a variable in my main code.

TrebledJ
  • 8,713
  • 7
  • 26
  • 48
  • 3
    https://www.tutorialspoint.com/cprogramming/c_operators.htm – Eugene Sh. Mar 18 '19 at 16:18
  • 1
    For the future, try asking Google your question first. Searching "What does the exclamation mark in C language mean" in Google has many relevant results. – Kevin Mar 18 '19 at 17:22

1 Answers1

2

This

!

is logical NOT operator & its a unary operator i.e it takes only one operand, result of this operator is either true(1) or false(0). Truth table of logical NOT operator is

 A    !A
----------
| 0  |  1 |
| 1  |  0 |
----------

Hence if piocherMot(motSecret) results in true i.e !1 which is 0 then if block will not get executes, in reverse case it gets executes.

if(!1) { /* 0 i.e if block won't executes */
}

And

if(!0) { /* 1 i.e if blocks executes */
}
Achal
  • 11,821
  • 2
  • 15
  • 37