Why does the return keep being true even through the bool is false?

The following code is the problem.

#include<stdio.h>
#include <stdbool.h>
int main(void)
{
    bool a = false;
    printf("a = %d\n", a);
    
    if(a = true)
        printf("%d",a);
    else
        printf("False");
    return 0;
}

The result is

a = 0
1

I get that due to bool a = false, the first return is a = 0.

However, I do not get why the if~else statement keeps printing a 1(which is the value of true).

Since a=0 indicates that the a is false, shouldn't it be printing False?

2 answers

  • answered 2021-04-21 16:28 user12372096

    == is used to compare values.

    = is used to assign a value to a variable.

  • answered 2021-04-21 16:28 Saifeddine Ben Salem

    It's simple . As you're confused between == and =

    == is to compare two variables

    = is to affect a value inside your variable

    To fix your code

    #include<stdio.h>
    #include <stdbool.h>
    int main(void)
    {
    bool a = false;
    printf("a = %d\n", a);
    
    if(a == true)
        printf("%d",a);
    else
        printf("False");
    return 0;
    }