I have been using ! (logical negation) in C and in other languages, I am curious does anyone know how to make your own ! function? or have a creative way of making one?
-
2When you talk about a "! method", it sounds like you may be talking about C++. You can write a function that does logical negation in C, but you cannot override the ! operator. C++ allows you to write an operator! - either a method or a non-member function - but that's C++, not C. – Feb 23 '10 at 16:23
-
It wasn't as easy as I'd thought to find a Q&A with the part of the standard saying that `!` returns 1 if the input is 0 (as opposed to any other non-zero value in a compiler-dependent fashion), but it's [C99 standard 6.5.3.3](http://stackoverflow.com/questions/7945950/is-logical-negation-of-zero-0-compiler-dependent-in-c). Just FYI. – HostileFork says dont trust SE Oct 24 '14 at 03:03
5 Answers
int my_negate(int x)
{
return x == 0 ? 1 : 0;
}

- 3,851
- 1
- 22
- 21
-
Happens all the time to me. I'll write something, but actually meant the exact opposite. – Brian R. Bondy Feb 23 '10 at 16:26
-
1I don't think this works in all cases. I don't think there's any guarantee about the value when a null pointer value is transformed to an `int`. There's very little in the Standards I'm familiar with other than that a pointer can be cast to an integral type, and a constant integral expression with value zero will be cast to a null pointer value. – David Thornley Feb 23 '10 at 16:29
-
3Null pointers are always converted to integer value zero (even if the null pointer is not represented like that). (Harbison & Steele, §6.2.3) – Hans W Feb 23 '10 at 16:38
-
-
Pascal: `==0` would indeed do the job; the `? 1 : 0` part just makes it more clear what's going on. – Hans W Feb 23 '10 at 20:46
Remember the bang operator '!' or exclamation mark in english parlance, is built into the programming language as a means to negate.
Consider this ternary operator example:
(some condition) ? true : false;
Now, if that was negated, the ternary operator would be this
(some condition) ? false : true;
The common area where that can get some programmers in a bit of a fit is the strcmp
function, which returns 0 for the strings being the same, and 1 for two strings not the same:
if (strcmp(foo, "foo")){ }
When really it should be:
if (!strcmp(foo, "foo")){ }
In general when you negate, it is the opposite as shown in the ternary operator example...
Hope this helps.

- 34,087
- 8
- 78
- 110
-
'strcmp' returns 0 for strings the same; it returns a negative number (not necessarily -1) if the first string precedes the second, and a positive number (not necessarily +1) if the first string follows the second - where the ordering is based on the byte values of the (unsigned) characters that compose the strings. – Jonathan Leffler Feb 23 '10 at 16:28
-
@Jonathan: Correct, -1 is considered to be true.....as it is the max unsigned int...i.e. 0xFFFF...i.e. non-zero, regardless when -'ive or +'ive – t0mm13b Feb 23 '10 at 16:36
-
Your answer states 'the strcmp function, which returns 0 for the strings being the same, and 1 for two strings not the same'. This is incorrect; it returns a non-zero value for two strings that are not the same, not 'unconditionally 1 if the strings are different'. – Jonathan Leffler Feb 23 '10 at 18:42
C considers all non-zero values "true" and zero "false". Logical negation is done by checking against zero. If the input is exactly zero, output a non-zero value; otherwise, output zero. In code, you can write this as (input == 0) ? 1 : 0
(or you can convert it into an if
statement).
When you ask how to "make your own ! method", do you mean you want to write a function that negates a logic value or do you want to define what the exclamation-point operator does? If the former, then the statement I posted above should suffice. If the latter, then I'm afraid this is something that can't be done in C. C++ supports operator overloading, and if doing this is a strict necessity then I would suggest looking there.

- 43,959
- 6
- 69
- 99
If you want to overload an operator, the proper prototype is:
bool operator!();
I'm not a big fan of overloading operators, but some people like their syntactic sugar. EDIT: This is C++ only! Put it in the definition of your class.

- 632
- 3
- 13
-
Overloading operators works well with templates. Just make sure your `operator!()` has the same semantics as `!`. – David Thornley Feb 23 '10 at 16:30
-
If you put it in the class definition, it takes no arguments and operates on *this. bool operator!() const; The prototype you show could be used for a friend operator definition. – Ben Voigt Feb 23 '10 at 16:38
-
Right. Good use case for overloading an operator. If you didn't have a good reason like that, however, the operator isn't usually as intuitive as a well-named function. There's always exceptions, though. – thebretness Feb 23 '10 at 16:39