logo

Operador NO lògic (!) en C

Aquesta secció tractarà l'operador lògic NOT (!) en el llenguatge de programació C. Com ja sabem, l'operador lògic s'utilitza per realitzar l'operació lògica combinant dues o més condicions en les expressions donades. Si les condicions lògiques dels operands són certes, l'operador retorna valors booleans veritables o 1. En cas contrari, retorna un valor booleà fals o 0. Els operadors lògics es classifiquen en tres parts: Operadors AND lògic, OR lògic i NOT lògic.

Operador NO lògic (!) en C

El operador AND lògic s'utilitza per comprovar que les condicions de dos o més operands que queden són certes en una expressió determinada; l'operador AND retorna un valor veritable o diferent de zero (1). En cas contrari, retorna un valor fals o 0. Per tant, podem dir que l'operador AND lògic només pot funcionar en una expressió quan les condicions dels dos operands són certes, i si alguna condició no és certa, retorna 0. L'operador AND lògic es representa com el símbol '&&' doble.

Sintaxi:

 (A > b && b > c) 

El operador OR lògic s'utilitza per comprovar les condicions dels dos operands (A i B), i si un dels operands o expressions és cert, l'operador retorna un valor booleà veritable. De la mateixa manera, si cap de les expressions és certa, retorna un valor fals o zero. L'operador OR lògic es denota com el tub doble '||' símbol.

Sintaxi:

 (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)>

Aquí, el '!' El símbol representa l'operador lògic NOT, que inverteix el resultat de la condició donada.

La taula de veritat de l'operador lògic NOT:

A continuació es mostra la taula de veritat de l'operador no lògic a C

 condition !(condition) 1 0 0 1 

Exemple 1: programa per utilitzar l'operador lògic NOT a C

Creem un programa senzill per invertir la condició donada dels operands en el llenguatge de programació 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));>

Al programa anterior, utilitzem l'operador NOT (!) per invertir el resultat de les diferents expressions, com ara que la condició de la variable x és igual a 5, que és cert. Tot i així, l'operador NOT inverteix el resultat i retorna 0. De la mateixa manera, vam definir la condició (x!=5), però l'operador lògic va canviar el seu resultat i va retornar 1 i així successivament.

Exemple 2: Programa per introduir un número per dur a terme l'operador lògic NOT

Creem un programa senzill per obtenir el resultat invers d'un nombre enter mitjançant l'operador lògic NOT (!) en el llenguatge de programació C.

 /* 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; } 

Sortida:

 Enter the number: 7 The result of x: 0 

Al programa anterior, introduïm un nombre enter 7 de l'usuari i l'emmagatzemem a la variable x. Després d'això, l'operador lògic NOT (!) inverteix el valor de x (no zero) i retorna zero (0) per imprimir el resultat de x.

2ndexecució:

 Enter the number: 0 The result of x: 1 

De la mateixa manera, introduïm zero (0) de l'usuari i utilitzem l'operador lògic NOT (!) per revertir el valor de x a un valor diferent de zero, que és 1.

Exemple 3: programa per trobar l'any de traspàs mitjançant l'operador lògic AND (&&), OR (||) i NOT (!)

Escrivim un programa senzill per comprovar si l'any donat és un salt o no utilitzant l'operador AND lògic (&&), OR lògic (||) i l'operador NOT lògic (!) en llenguatge C.

 #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; } 

Sortida:

 Enter the year: 2020 2020 is a leap year. 

Al programa anterior, introduïm 2020 i després comprovem l'any donat definint la declaració if...else. En aquesta declaració, hem definit dues condicions;

java cast int a cadena
  1. L'any donat es divideix per 400, que és igual a 0. A continuació, utilitzem l'operador OR lògic per comprovar si la condició de l'operand esquerre o dret és certa.
  2. En la segona condició, l'any donat es divideix per 4 i 100. Però quan dividim el 2020 amb 4, que és igual a 0. De la mateixa manera, dividim l'any 2020 per 100, que tampoc és igual a 0. Així, tots dos són certes les condicions que mostren el '2020 és un any de traspàs'.
  3. Però quan entrem a l'any 2021, imprimeix el resultat donat '2021 no és un any de traspàs'.

2ndexecució:

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

Exemple 4: Programa per comprovar diferents condicions mitjançant l'operador lògic AND, OR i NOT

Escrivim un programa per demostrar les múltiples condicions dels operands donats utilitzant l'operador lògic AND, OR i NOT a C.

 /* 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; } 

Sortida:

 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.