Open In App

Logical Not ! operator in C with Examples

Last Updated : 15 Oct, 2019
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

! is a type of Logical Operator and is read as “NOT” or “Logical NOT“. This operator is used to perform “logical NOT” operation, i.e. the function similar to Inverter gate in digital electronics.

Syntax:

!Condition

// returns true if the conditions is false
// else returns false

Below is an example to demonstrate ! operator:

Example:




// C program to demonstrate working
// of logical NOT '!' operators
  
#include <stdio.h>
  
int main()
{
  
    // Taking a variable a
    // and set it to 0 (false)
    int a = 0;
  
    // logical NOT example
  
    // Since 0 is considered to be false
    // !a will yield true
    if (!a)
        printf("Condition yielded True\n");
    else
        printf("Condition yielded False\n");
  
    // set a to non-zero value (true)
    a = 1;
  
    // Since a non-zero value is considered to be true
    // !a will yield false
    if (!a)
        printf("Condition yielded True\n");
    else
        printf("Condition yielded False\n");
  
    return 0;
}


Output:

Condition yielded True
Condition yielded False


Similar Reads

Code Optimization Technique (logical AND and logical OR)
Logical AND (&amp;&amp;)While using &amp;&amp; (logical AND), we must put the condition first whose probability of getting false is high so that compiler doesn’t need to check the second condition if the first condition is false. C/C++ Code #include &lt;bits/stdc++.h&gt; using namespace std; // Function to check whether n is odd bool isOdd(int n) {
7 min read
Order of operands for logical operators
The order of operands of logical operators &amp;&amp;, || are important in C/C++. In mathematics, logical AND, OR, etc... operations are commutative. The result will not change even if we swap RHS and LHS of the operator. In C/C++ (may be in other languages as well)  even though these operators are commutative, their order is critical. For example
1 min read
What are the differences between bitwise and logical AND operators in C/C++?
A Bitwise And operator is represented as '&amp;' and a logical operator is represented as '&amp;&amp;'. The following are some basic differences between the two operators. a) The logical and operator '&amp;&amp;' expects its operands to be boolean expressions (either 1 or 0) and returns a boolean value. The bitwise and operator '&amp;' work on Inte
4 min read
C Logical Operators
Logical operators in C are used to combine multiple conditions/constraints. Logical Operators returns either 0 or 1, it depends on whether the expression result is true or false. In C programming for decision-making, we use logical operators. We have 3 logical operators in the C language: Logical AND ( &amp;&amp; )Logical OR ( || )Logical NOT ( ! )
6 min read
Operands for sizeof operator
The sizeof operator is used to return the size of its operand, in bytes. This operator always precedes its operand. The operand either may be a data-type or an expression. Let's look at both the operands through proper examples. type-name: The type-name must be specified in parentheses. sizeof(type - name) Let's look at the code: C/C++ Code #includ
2 min read
sizeof operator in C
Sizeof is a much-used operator in the C. It is a compile-time unary operator which can be used to compute the size of its operand. The result of sizeof is of the unsigned integral type which is usually denoted by size_t. sizeof can be applied to any data type, including primitive types such as integer and floating-point types, pointer types, or com
3 min read
C++ | Operator Overloading | Question 10
How can we restrict dynamic allocation of objects of a class using new? (A) By overloading new operator (B) By making an empty private new operator. (C) By making an empty private new and new[] operators (D) By overloading new operator and new[] operators Answer: (C) Explanation: If we declare new and [] new operators, then the objects cannot be cr
1 min read
C++ | Operator Overloading | Question 2
Which of the following operators cannot be overloaded (A) . (Member Access or Dot operator) (B) ?: (Ternary or Conditional Operator ) (C) :: (Scope Resolution Operator) (D) .* (Pointer-to-member Operator ) (E) All of the above Answer: (E) Explanation: See What are the operators that cannot be overloaded in C++?Quiz of this Question
1 min read
C++ | Operator Overloading | Question 3
Which of the following operators are overloaded by default by the compiler in every user defined classes even if user has not written? 1) Comparison Operator ( == ) 2) Assignment Operator ( = ) (A) Both 1 and 2 (B) Only 1 (C) Only 2 (D) None of the two Answer: (C) Explanation: Assign operator is by default available in all user defined classes even
1 min read
C++ | Operator Overloading | Question 4
Which of the following operators should be preferred to overload as a global function rather than a member method? (A) Postfix ++ (B) Comparison Operator (C) Insertion Operator &lt;&lt; (D) Prefix++ Answer: (C) Explanation: cout is an object of ostream class which is a compiler defined class. When we do "cout &lt;&lt; obj&quot; where obj is an obje
1 min read
C++ | Operator Overloading | Question 6
Predict the output #include&lt;iostream&gt; using namespace std; class A { int i; public: A(int ii = 0) : i(ii) {} void show() { cout &lt;&lt; i &lt;&lt; endl; } }; class B { int x; public: B(int xx) : x(xx) {} operator A() const { return A(x); } }; void g(A a) { a.show(); } int main() { B b(10); g(b); g(20); return 0; } (A) Compiler Error (B) 10 2
1 min read
C++ | Operator Overloading | Question 7
Output of following program? #include &lt;iostream&gt; using namespace std; class Test2 { int y; }; class Test { int x; Test2 t2; public: operator Test2 () { return t2; } operator int () { return x; } }; void fun ( int x) { cout &lt;&lt; &quot;fun(int) called&quot;; } void fun ( Test2 t ) { cout &lt;&lt; &quot;fun(Test 2) called&quot;; } int main()
1 min read
C++ | Operator Overloading | Question 10
Predict the output? #include&lt;stdlib.h&gt; #include&lt;stdio.h&gt; #include&lt;iostream&gt; using namespace std; class Test { int x; public: void* operator new(size_t size); void operator delete(void*); Test(int i) { x = i; cout &lt;&lt; &quot;Constructor called \n&quot;; } ~Test() { cout &lt;&lt; &quot;Destructor called \n&quot;; } }; void* Test
2 min read
C++ | Operator Overloading | Question 9
#include&lt;iostream&gt; using namespace std; class Point { private: int x, y; public: Point() : x(0), y(0) { } Point&amp; operator()(int dx, int dy); void show() {cout &lt;&lt; &quot;x = &quot; &lt;&lt; x &lt;&lt; &quot;, y = &quot; &lt;&lt; y; } }; Point&amp; Point::operator()(int dx, int dy) { x = dx; y = dy; return *this; } int main() { Point p
1 min read
C++ | Operator Overloading | Question 10
Which of the following operator functions cannot be global, i.e., must be a member function. (A) new (B) delete (C) Conversion Operator (D) All of the above Answer: (C) Explanation: new and delete can be global, see following example. #include #include #include using namespace std; class Myclass { int x; public: friend void* operator new(size_t siz
1 min read
C++ | Nested Ternary Operator
Ternary operator also known as conditional operator uses three operands to perform operation. Syntax : op1 ? op2 : op3; Nested Ternary operator: Ternary operator can be nested. A nested ternary operator can have many forms like : a ? b : ca ? b: c ? d : e ? f : g ? h : ia ? b ? c : d : e Let us understand the syntaxes one by one : a ? b : c =&gt; T
5 min read
How can we use Comma operator in place of curly braces?
In C and C++, comma (, ) can be used in two contexts: Comma as an operator Comma as a separator But in this article, we will discuss how a comma can be used as curly braces. The curly braces are used to define the body of function and scope of control statements. The opening curly brace ({) indicates starting scope and closing curly brace (}) indic
3 min read
dot (.) Operator in C
The C dot (.) operator is used for direct member selection via the name of variables of type struct and union. Also known as the direct member access operator, it is a binary operator that helps us to extract the value of members of the structures and unions. Syntax of Dot Operatorvariable_name.member;variable_name: An instance of a structure or a
2 min read
C/C++ Ternary Operator - Some Interesting Observations
Predict the output of following C++ program. #include &lt;iostream&gt; using namespace std; int main() { int test = 0; cout &lt;&lt; &quot;First  character &quot; &lt;&lt; '1' &lt;&lt; endl; cout &lt;&lt; &quot;Second character &quot; &lt;&lt; (test ? 3 : '1') &lt;&lt; endl; return 0; } One would expect the output will be same in both the print sta
3 min read
Result of comma operator as l-value in C and C++
Using the result of the comma operator as l-value is not valid in C. But in C++, the result of the comma operator can be used as l-value if the right operand of the comma operator is l-value. For example, if we compile the following program as a C++ program, then it works and prints b = 30. And if we compile the same program as a C program, then it
1 min read
A comma operator question
Consider the following C programs. C/C++ Code // PROGRAM 1 #include&amp;lt;stdio.h&amp;gt; int main(void) { int a = 1, 2, 3; printf(&amp;quot;%d&amp;quot;, a); return 0; } The above program fails in compilation, but the following program compiles fine and prints 1. C/C++ Code // PROGRAM 2 #include&amp;lt;stdio.h&amp;gt; int main(void) { int a; a =
2 min read
When should we write our own assignment operator in C++?
The answer is same as Copy Constructor. If a class doesn't contain pointers, then there is no need to write assignment operator and copy constructor. The compiler creates a default copy constructor and assignment operators for every class. The compiler created copy constructor and assignment operator may not be sufficient when we have pointers or a
3 min read
Default Assignment Operator and References in C++
We have discussed assignment operator overloading for dynamically allocated resources here. In this article, we discussed that when we don't write our own assignment operator, the compiler creates an assignment operator itself that does shallow copy and thus causes problems. The difference between shallow copy and deep copy becomes visible when the
2 min read
Scope Resolution Operator vs this pointer in C++
Scope resolution operator is for accessing static or class members and this pointer is for accessing object members when there is a local variable with the same name. Consider below C++ program: C/C++ Code // C++ program to show that local parameters hide // class members #include &lt;iostream&gt; using namespace std; class Test { int a; public: Te
3 min read
Implementing ternary operator without any conditional statement
How to implement ternary operator in C++ without using conditional statements.In the following condition: a ? b: c If a is true, b will be executed. Otherwise, c will be executed.We can assume a, b and c as values. 1. Using Binary Operator We can code the equation as : Result = (!!a)*b + (!a)*c In above equation, if a is true, the result will be b.
4 min read
Operator Precedence and Associativity in C
The concept of operator precedence and associativity in C helps in determining which operators will be given priority when there are multiple operators in the expression. It is very common to have multiple operators in C language and the compiler first evaluates the operater with higher precedence. It helps to maintain the ambiguity of the expressi
8 min read
To find sum of two numbers without using any operator
Write a program to find sum of positive integers without using any operator. Only use of printf() is allowed. No other library function can be used. Solution It's a trick question. We can use printf() to find sum of two numbers as printf() returns the number of characters printed. The width field in printf() can be used to find the sum of two numbe
9 min read
Bitwise Complement Operator (~ tilde)
Pre-requisite:Bitwise Operators in C/ C++Bitwise Operators in Java The bitwise complement operator is a unary operator (works on only one operand). It takes one number and inverts all bits of it. When bitwise operator is applied on bits then, all the 1's become 0's and vice versa. The operator for the bitwise complement is ~ (Tilde). Example: Input
3 min read
Why only subtraction of addresses allowed and not division/addition/multiplication
Why Subtraction is allowed? Two addresses can be subtracted because the memory between the two addresses will be valid memory. Let's assume memory Ptr_1 and ptr_2 valid addresses. It is obvious that memory between these two addresses is valid. Pointer ptr_1 is pointing to 0x1cb0010 memory location and ptr_2 is pointing to 0x1cb0030 memory location.
2 min read
Use of &amp; in scanf() but not in printf()
Why there is need of using '&amp;' in case of scanf function while not in case of printf function. Examples: scanf("%d %d", &amp;a, &amp;b);printf("%d %d", a, b);As a and b above are two variable and each has their own address assigned but instead of a and b, we send the address of a and b respectively. The reason is, scanf() needs to modify values
1 min read
Article Tags :