Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,848 questions

51,769 answers

573 users

What is the difference between variable == value and value == variable in C

1 Answer

0 votes
#include <stdio.h>

int main()
{
    int x = 8;

    if (x == 5) {
        puts("x == 5");
    }
    else {
        puts("x != 5");
    }

    if (x = 5) { // Error - Compiler say nothing, x set to 5
        puts("x == 5");
    }
    else {
        puts("x != 5");
    }

    /*if (5 = x) { // Error - Compiler say: error C2106: '=': left operand must be l-value
        puts("x == 5");
    }
    else {
        puts("x != 5");
    }*/

    if (5 == x) {
        puts("x == 5");
    }
    else {
        puts("x != 5");
    }

    return 0;
}



/*
run:

x != 5
x == 5
x == 5

*/

 



answered Jun 22, 2024 by avibootz

Related questions

1 answer 179 views
1 answer 86 views
1 answer 94 views
1 answer 107 views
1 answer 96 views
1 answer 73 views
...