-4

I just started learning C from yesterday and I encountered an issue and I am not getting why it is happening.

The code is this

#include<stdio.h>
int a=0;
void main() {
    if (a=13) {
        printf("Number Is Equal\n");
    }
    else {
        printf("Not Equal\n");
    }
}

It should show not equal but it is still showing Number is equal and i tried other numbers too in place of int a; If i assign the value of int a=13 and then if i run the statement if(a=13) then it is true but if i do the same with 0 on both place then it shows not equal.

Manthan Tilva
  • 3,135
  • 2
  • 17
  • 41
Ayu
  • 9
  • 4
  • 3
    `=` is assignment. `a = 13` assigns 13 to `a` and the value of the expression is `13`, i.e. not 0. Did you want `==`? Probably a duplicate for this somewhere, although difficult to search when you don't know the terms. (Good compilers will warn you of an assignment within an `if` condition check, don't ignore these warnings.) – Bathsheba Aug 23 '19 at 11:58
  • `=` is the assignment operator - you’re assigning 13 to `a`, which evaluates as “true”. You need to use `==` to compare values - `if ( a == 13 )` – John Bode Aug 23 '19 at 11:59
  • Is there not a canonical dupe target for this kind of questions? Looks like this has been discussed a million times already... – ForceBru Aug 23 '19 at 12:01
  • I know about comparison operator but in this case i was not doing comparison bcz in comparison case the code is working as expected but i am not getting this thing that when i do int a=0 and if(a=0) then it fires the else statement that i don't expect and if i write int a=0 and if(a=1 or any number) then it fires if statement. – Ayu Aug 23 '19 at 12:01
  • @Ayu: Did you not read my first comment? The value of the expression a = 0 is 0, the value of a = 13 is 13. It's this property that allows you to write things like a = b = 13. – Bathsheba Aug 23 '19 at 12:03
  • @Bathsheba So are we assigning the new value which means 13 to a? – Ayu Aug 23 '19 at 12:04
  • 1
    @Ayu: Yup. That's the one. – Bathsheba Aug 23 '19 at 12:05
  • Okay but if we do a=0 in both place which means int a =0 and if(a=0) then why it is showing not equal? Please don't be annoyed or frustrated from my question bcz i am completely new to this. – Ayu Aug 23 '19 at 12:07
  • @Ayu: Seriously, now is the time for you to read this (and don't forget to purchase a copy) https://www.dipmat.univpm.it/~demeio/public/the_c_programming_language_2.pdf – Bathsheba Aug 23 '19 at 12:13
  • @Bathsheba ok I will read it and then I will post in comments if I got any questions – Ayu Aug 23 '19 at 12:16
  • @Ayu: Once you've read that (and completed the example exercises), you'll be answering questions! – Bathsheba Aug 23 '19 at 12:16
  • @Ayu - in a Boolean context, 0 means “false”. Again, `if (a=0)` *assigns* 0 to `a`, and the result of the expression is the value of `a` after the assignment - it’s the same result as writing `if (0)`, which means the `else` branch gets taken. – John Bode Aug 23 '19 at 12:33
  • Please note that the K&R book is hopelessly outdated and filled with errors. It is not something I'd recommend anyone to read, least of all newbies. – Lundin Aug 23 '19 at 14:05
  • Question has problem explained, contains a code snippet showing what OP has tried, code snippet is able to repeat the OP's problem, question with code snippet clearly lead to a solution -> PUT ON HOLD AS OFF-TOPIC. I don't get it... – Joshua Schlichting Aug 23 '19 at 16:50

3 Answers3

2

You're assigning 13 to a using the assignment operator, =. Use == for comparisons.

example:

int a=0;
void main()
{
    // Here, a is being checked to see if it is equal to the int 13.
    // a=13 would be assigning 13 to a, and then checking to see if
    // a is "truthy", or, not 0, which is why it was true for you every time.
    if (a == 13) {
        printf("Number Is Equal\n");
    }else{
        printf("Not Equal\n");
    }
}

NOTE: The C Standard specifies that main() will return type int, (e.g. int main(){return 0;}) when in a hosted environment. You can deviate from this when you're in a freestanding environment, at which point this becomes implementation defined. It is not likely that you are using a freestanding environment. The odds are that you are in a hosted environment where your use of type void for main would violate the C Standard.

Joshua Schlichting
  • 3,110
  • 6
  • 28
  • 54
  • I'm open to down votes with constructive criticism, but this is the correct answer. If I'm mistaken, I'd love to know why! – Joshua Schlichting Aug 23 '19 at 12:41
  • 1
    `void main()` is not standard C. – Bathsheba Aug 23 '19 at 13:15
  • 1
    It compiles, runs, it's what OP put into their question, and the solution here addresses the problem OP was experiencing. – Joshua Schlichting Aug 23 '19 at 13:28
  • 1
    And it sets a bad example. All code snippets presented in answers should be standard C. – Bathsheba Aug 23 '19 at 13:29
  • OP never specified if this code is ultimately being compiled for a freestanding or hosted environment. The C standard states that the start up function's name and type are implementation defined for freestanding environments. It's not likely that this is for a freestanding environment, but if I made this change, I'd be making assumptions about OP's environment. – Joshua Schlichting Aug 23 '19 at 13:50
  • 2
    Technically, I haven't deviated from the C standard, until OP specifies that they're in a hosted environment. Until they specify that, this shouldn't be an issue. Heck, if I put `int main` in my answer, someone would down vote for changing it, saying "that doesn't match OP! They could be in a freestanding environment!" It sounds like your issue is with the question, and not actually my answer. – Joshua Schlichting Aug 23 '19 at 13:56
  • @JoshuaSchlichting: Someone who literally just started learning the language the day before is *very likely* (in fact, almost certainly) running on a hosted implementation. It's best to assume hosted unless otherwise specified. – John Bode Aug 23 '19 at 14:00
  • https://stackoverflow.com/questions/4273507/what-are-the-different-valid-prototypes-of-main-function Yes I know that my favourite C book is full of `void main()` but ain't it about time we moved away from that? – Bathsheba Aug 23 '19 at 14:01
  • @Bathsheba No, your favourite book is far worse, it is full of `main()`. Implicit int is not valid C and empty parenthesis is obsolete style since 1990. `void main (void)` however, is a common implementation-defined form, used in pretty much all freestanding systems. See [this](https://stackoverflow.com/a/31263079/584518). – Lundin Aug 23 '19 at 14:09
  • @Bathsheba Yeah sure they were both very smart and talented but that doesn't make the book good per association. – Lundin Aug 23 '19 at 14:16
  • @JohnBode & @Bathsheba I agree, the OP is not likely to be in a freestanding environment. I could not resist pointing out that this is still technically correct though, since the environment wasn't specified (and the issue for the OP was really about the assignment operator, which the answer does cover). With that said, I have enjoyed the conversation, and I agree that this likely should be `int main()`. I've made an edit pointing this out with an explanation and I hope I have earned your up vote :). – Joshua Schlichting Aug 23 '19 at 14:45
  • 1
    Yup. Downvote converted to upvote. – Bathsheba Aug 23 '19 at 16:22
0

Here

int a=0;
if (a=13) { }

It should show not equal ?

No, if condition always gets true due to the assignment a=13 which is non zero, you need to use == instead.

Change this

if (a=13) { } /* = is assignment operator, condition remains always true */

to

if (a == 13)  {} /* use comparison operator == */
Achal
  • 11,821
  • 2
  • 15
  • 37
0

You are writing a=13, which assigns the value 13 to a. Since this is non-zero it is also considered true (zero would translate to false).

You should write a == 13 instead, using double equal signs, for comparisons.

Tamir Abutbul
  • 7,301
  • 7
  • 25
  • 53
Ole Wolf
  • 1,312
  • 13
  • 18