Logical Operators
On this page (21sections)
Logical Operators
- AND,OR operators are used when we want to use two or more Conditions.
Types Of Logical Operators
- && Logical AND
- || Logical OR
- ! Logical NOT
Logical And (&&) Operator
Logical And Operator Definition
If both the operations are successful, then the condition becomes true.
Logical And Operator Syntax
expr1 && expr2
Logical And Operator Syntax Example
if( (a>10) && (a<20) )
printf(?A is in-between of 10 and 20?);
Logical And Operator Example Program
/* Example Program For Logical And operator In C Programming Language
little drops @ thiyagaraaj.com
Coded By:THIYAGARAAJ MP */
// Header Files
#include<stdio.h>
#include<conio.h>
//Main Function
int main()
{
// Variable Declaration
int num1 = 30;
int num2 = 40;
if(num1>=40 && num2>=40){
printf("Example of Logical And operator");
}
// Wait For Output Screen
getch();
//Main Function return Statement
return 0;
}
Sample Output:
Example of Logical And operator
Logical OR (||) Operator
Logical OR Operator Definition
If any one of the operations are successful, then the condition becomes true
Logical OR Operator Syntax
expr1 || expr2
Logical OR Operator Syntax Example
if( (a>10) || (a<20) )
printf(?A is greater than 10 or less than 20?);
Logical OR Operator Example Program
/* Example Program For Logical Or (||) operator In C Programming Language
little drops @ thiyagaraaj.com
Coded By:THIYAGARAAJ MP */
// Header Files
#include<stdio.h>
#include<conio.h>
//Main Function
int main()
{
// Variable Declaration
int num1 = 30;
int num2 = 40;
if(num1>=40 || num2>=40){
printf("Example of Logical Or operator");
}
// Wait For Output Screen
getch();
//Main Function return Statement
return 0;
}
Sample Output
Example of Logical OR operator
Logical NOT (!) Operator
Logical NOT Operator Definition
It is used to reverse the condition of result. That is, if a condition is true, Logical NOT operator reverses the result and produces output as false.
Logical NOT Operator Syntax
!expression
Logical NOT Operator Syntax Example
if( !(a>10)
printf(?A is less than 10 ?);
Logical Not Operator Example Program
/* Example Program For Logical Not (!) operator In C Programming Language
little drops @ thiyagaraaj.com
Coded By:THIYAGARAAJ MP */
// Header Files
#include<stdio.h>
#include<conio.h>
//Main Function
int main()
{
int number;
printf("Please enter an integer value: ");
scanf("%d", &number);
if(!(number>40)){
printf("Input check greater than 40 - TRUE");
}else{
printf("Input check greater than 40 - FALSE");
}
// Wait For Output Screen
getch();
//Main Function return Statement
return 0;
}
Sample Output
Please enter an integer value: 35
Input check greater than 40 - FALSE
Related Pages
Continue learning with these related tutorials and programs:
- C Tutorials — Browse all C Tutorials.
- C Operators — Overview — all C operator categories.
- Arithmetic Operator Example Program In C — More in c operators.
- Relational Operators — More in c operators.
Frequently Asked Questions
What does this C program do?
It is a C example program that demonstrates Logical Operators, including the complete source code and the expected sample output.
How do I compile and run this C program?
Save the code in a `.c` file, compile it with `gcc filename.c -o program`, then run it with `./program` (or `program.exe` on Windows).
What concepts does this example use?
This example uses conditional logic, reading user input and user-defined functions, illustrating a common pattern in C programming.