logo

C의 논리 NOT(!) 연산자

이 섹션에서는 C 프로그래밍 언어의 논리적 NOT(!) 연산자에 대해 설명합니다. 이미 알고 있듯이 논리 연산자는 주어진 수식에 대해 둘 이상의 조건을 결합하여 논리 연산을 수행하는 데 사용됩니다. 피연산자의 논리 조건이 true이면 연산자는 true 부울 값 또는 1을 반환합니다. 그렇지 않으면 false 부울 값 또는 0을 반환합니다. 논리 연산자는 세 부분으로 분류됩니다. 논리 AND, 논리 OR 및 논리 NOT 연산자입니다.

C의 논리 NOT(!) 연산자

그만큼 논리 AND 연산자 주어진 표현식에서 남아 있는 둘 이상의 피연산자의 조건이 true인지 확인하는 데 사용됩니다. AND 연산자는 참 또는 0이 아닌(1) 값을 반환합니다. 그렇지 않으면 false 또는 0 값을 반환합니다. 따라서 논리 AND 연산자는 두 피연산자의 조건이 모두 true인 경우에만 표현식에서 수행할 수 있으며, 하나라도 true가 아닌 경우 0을 반환한다고 할 수 있습니다. 논리 AND 연산자는 이중 앰퍼샌드 '&&' 기호로 표시됩니다.

통사론:

 (A > b && b > c) 

그만큼 논리 OR 연산자 피연산자(A & B) 조건을 모두 확인하는 데 사용되며, 피연산자나 표현식 중 하나가 참이면 연산자는 참 부울 값을 반환합니다. 마찬가지로, 표현식 중 어느 것도 참이 아니면 거짓 또는 0 값을 반환합니다. 논리 OR 연산자는 이중 파이프 '||'로 표시됩니다. 상징.

통사론:

 (A &gt; B) || (A <c) < pre> <h3>Logical NOT operator</h3> <p>The logical NOT operator is represented as the &apos;!&apos; symbol, which is used to reverse the result of any given expression or condition. If the result of an expression is non-zero or true, the result will be reversed as zero or false value. Similarly, if the condition&apos;s result is false or 0, the NOT operator reverses the result and returns 1 or true.</p> <p>For example, suppose the user enters a non-zero value is 5, the logical NOT (!) operator returns the 0 or false Boolean value. And if the user enters a zero (0) value, the operator returns the true Boolean value or 1. </p> <p> <strong>Syntax of the logical NOT operator</strong> </p> <pre> ! (condition); // It &apos;!&apos; represents the NOT operator </pre> <p>Here, the &apos;!&apos; symbol represents the logical NOT operator, which inverses the result of the given condition.</p> <h3>The truth table of the logical NOT operator:</h3> <p>Following is the truth table of the logical not operator in C</p> <pre> condition !(condition) 1 0 0 1 </pre> <h3>Example 1: Program to use the logical NOT operator in C</h3> <p>Let&apos;s create a simple program to reverse the given condition of the operands in the C programming language.</p> <pre> /* demonstration the use of the logical not operator in C. */ #include #include int main () { // declare an initialize x variable int x = 5; // display the result generated using the NOT (!) operator printf (&apos; The return value = %d 
&apos;, ! (x == 5)); printf (&apos; The return value = %d 
&apos;, ! (x != 5)); printf (&apos; The return value = %d 
&apos;, ! (x &gt;= 3)); printf (&apos; The return value = %d 
&apos;, ! (x <3)); return 0; } < pre> <p> <strong>Output:</strong> </p> <pre> The return value = 0 The return value = 1 The return value = 0 The return value = 1 </pre> <p>In the above program, we use the NOT (!) operator to reverse the result of the various expression, such as the condition of the variable x is equal to 5, which is true. Still, the NOT operator reverses the result and returns 0. Similarly, we defined the condition (x!=5), but the logical operator changed its result and returned 1 and so on.</p> <h3>Example 2: Program to input a number to perform the logical NOT operator</h3> <p>Let&apos;s create a simple program to get the reverse result of an integer number using the logical NOT (!) operator in the C programming language.</p> <pre> /* demonstration the use of the logical not operator in C. */ #include #include int main () { // declare an initialize x variable int x, n; printf (&apos; Enter the number: &apos;); scanf (&apos;%d&apos;, &amp;x); n = !x; // use logical not operator to reverse the condition printf (&apos; The result of x: %d&apos;, n); // display the result return 0; } </pre> <p> <strong>Output:</strong> </p> <pre> Enter the number: 7 The result of x: 0 </pre> <p>In the above program, we input an integer number 7 from the user and store it into x variable. After that, the logical NOT (!) operator reverses the value of x (non-zero) and returns zero (0) to print the result of x.</p> <p> <strong>2<sup>nd</sup> execution:</strong> </p> <pre> Enter the number: 0 The result of x: 1 </pre> <p>Similarly, we input zero (0) from the user and use the logical NOT (!) operator to reverse the value of x to a non-zero value, which is 1.</p> <h3>Example 3: Program to find the leap year using the logical AND (&amp;&amp;), OR (||), and NOT (!) operator</h3> <p>Let&apos;s write a simple program to check whether the given year is a leap or not using the logical AND (&amp;&amp;), logical OR (||), and the logical NOT (!) operator in the C language.</p> <pre> #include #include int main () { int yr; // declare int type variable printf (&apos; Enter the year: &apos;); scanf (&apos;%d&apos;, &amp;yr); // use the if..else statement to check the condition /* &apos;&amp;&amp;&apos; (AND) operator to validate both operand, &apos;||&apos; (OR) operator check ny given expressions are true, &apos;!&apos; (NOT) check the result of (yr % 100 != 0). */ if ( (yr % 400 == 0) || (yr % 4 == 0 &amp;&amp; yr % 100 != 0)) { printf (&apos; %d is a leap year. 
&apos;, yr); } else { printf (&apos; %d is not a leap year. 
&apos;, yr); } return 0; } </pre> <p> <strong>Output:</strong> </p> <pre> Enter the year: 2020 2020 is a leap year. </pre> <p>In the above program, we enter 2020 and then check the given year by defining the if...else statement. In this statement, we defined two conditions;</p> <ol class="points"> <li>The given year is divided by 400, which is equal to 0. And then, we use the logical OR operator to check whether the left or right operand condition is true.</li> <li>In the second condition, the given year is divided by 4 and 100. But when we divide 2020 with 4, which is equal to 0. Similarly, we divide the year 2020 by 100, which is also not equal to 0. So, both the conditions are true that display the &apos;2020 is a leap year&apos;.</li> <li>But when we enter the year 2021, it prints the given result &apos;2021 is not a leap year&apos;.</li> </ol> <p> <strong>2<sup>nd</sup> execution:</strong> </p> <pre> Enter the year: 2021 2021 is not a leap year. </pre> <p> <strong>Example 4: Program to check different conditions using the AND, OR, and the NOT logical operator</strong> </p> <p>Let&apos;s write a program to demonstrate the multiple conditions of the given operands using the AND, OR, and the NOT logical operator in C.</p> <pre> /* program to check the various condition using the logical NOT operator in c. */ #include #include int main () { // declare and initialize variables int a = 20, b = 15; int n1 = 15, n2 = 17; // use logical &apos;AND&apos; and logical &apos;NOT&apos; operator if (a &gt; b &amp;&amp; a != 0) { printf (&apos; The AND (&amp;&amp;) operator said: Both conditions are true. 
 &apos;); } // use logical &apos;OR&apos; and logical &apos;NOT&apos; operator if (n1 &gt; n2 || n2 != 15) if ( ! (a &gt; b &amp;&amp; a != 0 )) { printf (&apos; The NOT (!) operator: Here both conditions are true. 
 &apos;); } else { printf (&apos; The NOT (!) operator: Here, both conditions are true. &apos; &apos; But, the status of the condition is reversed as false. 
&apos;); } return 0; } </pre> <p> <strong>Output:</strong> </p> <pre> The AND (&amp;&amp;) operator is said: Both conditions are true. The OR (||) operator is said: Only one condition is true. The NOT (!) operator: Here, both conditions are true. But, the status of the condition is reversed as false. </pre> <hr></3));></pre></c)>

여기서 '!' 기호는 주어진 조건의 결과를 반전시키는 논리 NOT 연산자를 나타냅니다.

논리 NOT 연산자의 진리표:

다음은 C의 논리 NOT 연산자의 진리표입니다.

 condition !(condition) 1 0 0 1 

예제 1: C에서 논리 NOT 연산자를 사용하는 프로그램

C 프로그래밍 언어에서 피연산자의 주어진 조건을 반전시키는 간단한 프로그램을 만들어 보겠습니다.

 /* demonstration the use of the logical not operator in C. */ #include #include int main () { // declare an initialize x variable int x = 5; // display the result generated using the NOT (!) operator printf (&apos; The return value = %d 
&apos;, ! (x == 5)); printf (&apos; The return value = %d 
&apos;, ! (x != 5)); printf (&apos; The return value = %d 
&apos;, ! (x &gt;= 3)); printf (&apos; The return value = %d 
&apos;, ! (x <3)); return 0; } < pre> <p> <strong>Output:</strong> </p> <pre> The return value = 0 The return value = 1 The return value = 0 The return value = 1 </pre> <p>In the above program, we use the NOT (!) operator to reverse the result of the various expression, such as the condition of the variable x is equal to 5, which is true. Still, the NOT operator reverses the result and returns 0. Similarly, we defined the condition (x!=5), but the logical operator changed its result and returned 1 and so on.</p> <h3>Example 2: Program to input a number to perform the logical NOT operator</h3> <p>Let&apos;s create a simple program to get the reverse result of an integer number using the logical NOT (!) operator in the C programming language.</p> <pre> /* demonstration the use of the logical not operator in C. */ #include #include int main () { // declare an initialize x variable int x, n; printf (&apos; Enter the number: &apos;); scanf (&apos;%d&apos;, &amp;x); n = !x; // use logical not operator to reverse the condition printf (&apos; The result of x: %d&apos;, n); // display the result return 0; } </pre> <p> <strong>Output:</strong> </p> <pre> Enter the number: 7 The result of x: 0 </pre> <p>In the above program, we input an integer number 7 from the user and store it into x variable. After that, the logical NOT (!) operator reverses the value of x (non-zero) and returns zero (0) to print the result of x.</p> <p> <strong>2<sup>nd</sup> execution:</strong> </p> <pre> Enter the number: 0 The result of x: 1 </pre> <p>Similarly, we input zero (0) from the user and use the logical NOT (!) operator to reverse the value of x to a non-zero value, which is 1.</p> <h3>Example 3: Program to find the leap year using the logical AND (&amp;&amp;), OR (||), and NOT (!) operator</h3> <p>Let&apos;s write a simple program to check whether the given year is a leap or not using the logical AND (&amp;&amp;), logical OR (||), and the logical NOT (!) operator in the C language.</p> <pre> #include #include int main () { int yr; // declare int type variable printf (&apos; Enter the year: &apos;); scanf (&apos;%d&apos;, &amp;yr); // use the if..else statement to check the condition /* &apos;&amp;&amp;&apos; (AND) operator to validate both operand, &apos;||&apos; (OR) operator check ny given expressions are true, &apos;!&apos; (NOT) check the result of (yr % 100 != 0). */ if ( (yr % 400 == 0) || (yr % 4 == 0 &amp;&amp; yr % 100 != 0)) { printf (&apos; %d is a leap year. 
&apos;, yr); } else { printf (&apos; %d is not a leap year. 
&apos;, yr); } return 0; } </pre> <p> <strong>Output:</strong> </p> <pre> Enter the year: 2020 2020 is a leap year. </pre> <p>In the above program, we enter 2020 and then check the given year by defining the if...else statement. In this statement, we defined two conditions;</p> <ol class="points"> <li>The given year is divided by 400, which is equal to 0. And then, we use the logical OR operator to check whether the left or right operand condition is true.</li> <li>In the second condition, the given year is divided by 4 and 100. But when we divide 2020 with 4, which is equal to 0. Similarly, we divide the year 2020 by 100, which is also not equal to 0. So, both the conditions are true that display the &apos;2020 is a leap year&apos;.</li> <li>But when we enter the year 2021, it prints the given result &apos;2021 is not a leap year&apos;.</li> </ol> <p> <strong>2<sup>nd</sup> execution:</strong> </p> <pre> Enter the year: 2021 2021 is not a leap year. </pre> <p> <strong>Example 4: Program to check different conditions using the AND, OR, and the NOT logical operator</strong> </p> <p>Let&apos;s write a program to demonstrate the multiple conditions of the given operands using the AND, OR, and the NOT logical operator in C.</p> <pre> /* program to check the various condition using the logical NOT operator in c. */ #include #include int main () { // declare and initialize variables int a = 20, b = 15; int n1 = 15, n2 = 17; // use logical &apos;AND&apos; and logical &apos;NOT&apos; operator if (a &gt; b &amp;&amp; a != 0) { printf (&apos; The AND (&amp;&amp;) operator said: Both conditions are true. 
 &apos;); } // use logical &apos;OR&apos; and logical &apos;NOT&apos; operator if (n1 &gt; n2 || n2 != 15) if ( ! (a &gt; b &amp;&amp; a != 0 )) { printf (&apos; The NOT (!) operator: Here both conditions are true. 
 &apos;); } else { printf (&apos; The NOT (!) operator: Here, both conditions are true. &apos; &apos; But, the status of the condition is reversed as false. 
&apos;); } return 0; } </pre> <p> <strong>Output:</strong> </p> <pre> The AND (&amp;&amp;) operator is said: Both conditions are true. The OR (||) operator is said: Only one condition is true. The NOT (!) operator: Here, both conditions are true. But, the status of the condition is reversed as false. </pre> <hr></3));>

위 프로그램에서는 NOT(!) 연산자를 사용하여 변수 x의 조건이 5(참)인 등 다양한 표현식의 결과를 뒤집습니다. 여전히 NOT 연산자는 결과를 반전시키고 0을 반환합니다. 마찬가지로 조건 (x!=5)을 정의했지만 논리 연산자는 결과를 변경하고 1을 반환했습니다.

예제 2: 논리 NOT 연산자를 수행하기 위해 숫자를 입력하는 프로그램

C 프로그래밍 언어에서 논리 NOT(!) 연산자를 사용하여 정수의 반대 결과를 얻는 간단한 프로그램을 만들어 보겠습니다.

 /* demonstration the use of the logical not operator in C. */ #include #include int main () { // declare an initialize x variable int x, n; printf (&apos; Enter the number: &apos;); scanf (&apos;%d&apos;, &amp;x); n = !x; // use logical not operator to reverse the condition printf (&apos; The result of x: %d&apos;, n); // display the result return 0; } 

산출:

 Enter the number: 7 The result of x: 0 

위 프로그램에서는 사용자로부터 정수 7을 입력하고 이를 x 변수에 저장합니다. 그 후 논리 NOT(!) 연산자는 x의 값(0이 아님)을 반전하고 0(0)을 반환하여 x의 결과를 인쇄합니다.

2nd실행:

 Enter the number: 0 The result of x: 1 

마찬가지로 사용자로부터 0을 입력하고 논리 NOT(!) 연산자를 사용하여 x 값을 0이 아닌 값인 1로 반전합니다.

예제 3: 논리 AND(&&), OR(||) 및 NOT(!) 연산자를 사용하여 윤년을 찾는 프로그램

C 언어의 논리 AND(&&), 논리 OR(||), 논리 NOT(!) 연산자를 이용하여 해당 연도가 윤년인지 여부를 확인하는 간단한 프로그램을 작성해 보겠습니다.

 #include #include int main () { int yr; // declare int type variable printf (&apos; Enter the year: &apos;); scanf (&apos;%d&apos;, &amp;yr); // use the if..else statement to check the condition /* &apos;&amp;&amp;&apos; (AND) operator to validate both operand, &apos;||&apos; (OR) operator check ny given expressions are true, &apos;!&apos; (NOT) check the result of (yr % 100 != 0). */ if ( (yr % 400 == 0) || (yr % 4 == 0 &amp;&amp; yr % 100 != 0)) { printf (&apos; %d is a leap year. 
&apos;, yr); } else { printf (&apos; %d is not a leap year. 
&apos;, yr); } return 0; } 

산출:

 Enter the year: 2020 2020 is a leap year. 

위 프로그램에서는 2020을 입력한 후 if...else 문을 정의하여 해당 연도를 확인합니다. 이 성명서에서 우리는 두 가지 조건을 정의했습니다.

  1. 주어진 연도를 400으로 나누면 0이 됩니다. 그런 다음 논리 OR 연산자를 사용하여 왼쪽 또는 오른쪽 피연산자 조건이 true인지 확인합니다.
  2. 두 번째 조건에서는 주어진 연도를 4와 100으로 나눕니다. 그러나 2020년을 4로 나누면 0과 같습니다. 마찬가지로 2020년을 100으로 나누는데 이 역시 0이 아닙니다. 따라서 둘 다 '2020년은 윤년'을 표시하는 조건이 true입니다.
  3. 하지만 2021년을 입력하면 '2021년은 윤년이 아닙니다'라는 결과가 출력됩니다.

2nd실행:

 Enter the year: 2021 2021 is not a leap year. 

예제 4: AND, OR, NOT 논리 연산자를 사용하여 다양한 조건을 확인하는 프로그램

C에서 AND, OR, NOT 논리 연산자를 사용하여 주어진 피연산자의 다중 조건을 보여주는 프로그램을 작성해 보겠습니다.

 /* program to check the various condition using the logical NOT operator in c. */ #include #include int main () { // declare and initialize variables int a = 20, b = 15; int n1 = 15, n2 = 17; // use logical &apos;AND&apos; and logical &apos;NOT&apos; operator if (a &gt; b &amp;&amp; a != 0) { printf (&apos; The AND (&amp;&amp;) operator said: Both conditions are true. 
 &apos;); } // use logical &apos;OR&apos; and logical &apos;NOT&apos; operator if (n1 &gt; n2 || n2 != 15) if ( ! (a &gt; b &amp;&amp; a != 0 )) { printf (&apos; The NOT (!) operator: Here both conditions are true. 
 &apos;); } else { printf (&apos; The NOT (!) operator: Here, both conditions are true. &apos; &apos; But, the status of the condition is reversed as false. 
&apos;); } return 0; } 

산출:

 The AND (&amp;&amp;) operator is said: Both conditions are true. The OR (||) operator is said: Only one condition is true. The NOT (!) operator: Here, both conditions are true. But, the status of the condition is reversed as false.