C logical operators

Created By: chatGPT

In C programming, logical operators are used to combine or modify conditional statements. There are three main logical operators: AND, OR, and NOT. These operators allow you to form complex boolean expressions that can control the flow of the program based on multiple conditions.
// Logical Operators in C

#include <stdio.h>

int main() {
    int a = 1;
    int b = 0;

    // AND operator
    if (a && b) {
        printf("Both are true\n");
    } else {
        printf("One or both are false\n");
    }

    // OR operator
    if (a || b) {
        printf("At least one is true\n");
    }

    // NOT operator
    if (!b) {
        printf("b is false\n");
    }

    return 0;
}
The AND operator (&&) returns true only if both operands are true. The OR operator (||) returns true if at least one operand is true. The NOT operator (!) negates the value of the operand. They can be used in if statements or loops to create more intricate decision-making logic.
// Using Logical Operators in Conditions

#include <stdio.h>

int main() {
    int x = 5;
    int y = 10;

    // Combining conditions with AND
    if (x < 10 && y > 5) {
        printf("Both conditions are true\n");
    }

    // Combining conditions with OR
    if (x > 10 || y < 15) {
        printf("At least one condition is true\n");
    }

    // Using NOT operator
    if (!(x > 10)) {
        printf("x is not greater than 10\n");
    }

    return 0;
}
When working with logical operators in C, it's essential to remember the short-circuit evaluation property. This means that in an && operation, if the first operand is false, the second operand is not evaluated because the overall expression cannot be true. Similarly, in an || operation, if the first operand is true, the second is not evaluated.
// Short-Circuit Evaluation Example

#include <stdio.h>

int main() {
    int a = 0;

    // Short-circuit with AND
    if (a != 0 && (5 / a) > 1) {
        printf("This won't execute due to short-circuiting\n");
    }

    // Short-circuit with OR
    if (a == 0 || (5 / a) > 1) {
        printf("This will execute, avoiding division by zero\n");
    }

    return 0;
}
Introduction And SetupVariablesData TypesIntFloatDoubleCharVoidUnsignedSignedConstantsEnumerationsArraysStringsStructsUnionsTypedefsPointersDynamic Memory AllocationMallocCallocReallocFreeFunctionsFunction DeclarationsFunction DefinitionsFunction CallsReturn StatementInline FunctionsRecursionHeader FilesPreprocessor DirectivesControl FlowIf StatementElse StatementElse If StatementSwitch StatementCase StatementDefault CaseLoopsFor LoopWhile LoopDo While LoopBreak StatementContinue StatementGoto StatementLabelsOperatorsArithmetic OperatorsRelational OperatorsLogical OperatorsBitwise OperatorsAssignment OperatorsConditional (ternary) OperatorComma OperatorSizeof OperatorData StructuresLinked ListsStacksQueuesTreesGraphsFunction PointersCallbacksMacrosCommentsSingle Line CommentsMulti Line CommentsSyntaxSyntax ErrorsCompilation ErrorsDebuggingStandard Input OutputPrintfScanfFile HandlingFopenFcloseFreadFwriteFprintfFgetsFputsError HandlingErrnoAssertionsExit FunctionExit CodesEvent HandlingSignal HandlingInterrupts