Click here to Skip to main content
15,891,864 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
C++
#include <stdio.h>
    void main()
    {
        int x = 0;
        if (x = 0)
            printf("Its zero\n");
        else
            printf("Its not zero\n");
    }
Posted
Updated 7-Oct-15 21:47pm
v2
Comments
Richard MacCutchan 8-Oct-15 3:48am    
Not what you expect. Go back to your study notes and read again assignments and expressions. Then run the code to see exactly what happens. Then correct it so it works correctly.
Afzaal Ahmad Zeeshan 8-Oct-15 4:00am    
Thanks for that correction that you commented on my answer. :-) That was something new to me.
Richard MacCutchan 8-Oct-15 4:06am    
What happened to my comment?
Afzaal Ahmad Zeeshan 8-Oct-15 4:07am    
Sorry, I had the answer deleted and then read your comment in the email notification. :-) So, I replied here.
F-ES Sitecore 8-Oct-15 3:57am    
I'll give you a clue...if you look at really really old code, rather than comparing their variable with a value (if x is 10), they used to compare the value with their variable (if 10 is x). It might make no sense but it was done to avoid the problem you have here. So give that way a go and see if you can work it out for yourself :)

The program doesn't work because you are using assignment operator where you need to use the equality check operator. That is why the results are opposite and not what you had expected, as Richard said above.

What happens is that if(x = 0) would resolve the expression to zero. In C/C++, integers are directly resolved to boolean, everything non-zero is a true value and zero is the false. Since this resolves to zero, the expression is false and control is transferred to the else block.

Re-write it in this manner,

C++
#include <stdio.h>
void main()
{
    int x = 0;
    if (!x) { // If zero, then true, otherwise false. 
        printf("Its zero\n");
    }
    else {
        printf("Its not zero\n");
    }
}


Also remember to add the { } around the statements, it is not a good practice as you may confuse yourself or add a semicolon by mistake, compiler will not complain but the logic would be wrong.

For more on C operators please refer to this tutorial: http://www.tutorialspoint.com/cprogramming/c_operators.htm[^]
 
Share this answer
 
v2
Comments
CPallini 8-Oct-15 4:05am    
5.
Afzaal Ahmad Zeeshan 8-Oct-15 4:06am    
Thank you, Carlo.
Member 12042196 8-Oct-15 4:23am    
the program is working properly and the output i am getting is "it's not zero". but according to what i have studied it must be "its a zero". i am getting bit confused because of this.
Afzaal Ahmad Zeeshan 8-Oct-15 4:24am    
Then use, if(x == 0) in the condition. Then the output would be "Its zero".
Member 12042196 8-Oct-15 4:31am    
but the questions is how is it possible that the answer is "its not zero"
Since you didn't specify the executable there would be an error while compiling your program.
For fine working of your program the declared variable(x) in your question may not be initialized.Because initialized variables have a defined value.In order to execute the if else statement in your question the input should be given by you and it can be succeeded by an input function(scanf). In-order to keep the stability use the header file conio.h and getch(); function.

kindly use the below code for your reference:

XML
#include<stdio.h>
#include<conio.h>
void main()
{
 int x;
 printf("please enter your number");
 scanf("%d",&x);
 if(x==0)
 printf("It is zero");
 else
 printf("It is non zero");
getch();
}
 
Share this answer
 
v4
Comments
Andreas Gieriet 8-Oct-15 5:43am    
This is crap, sorry.
Why the heck should the variable *not* be initialized?!
You are not answering the OP's question.
And conio.h is not needed at all, or am I missing something?
The obvious problem in the OP's question is that he does not know the difference between = and ==.
Regards
Andi
[no name] 8-Oct-15 6:11am    
Pardon me Sir who is OP.Sir, conio.h header file is necessary to display the output till user enters any key.Else the result will be disappeared within a fraction of second and I have mentioned correctly that the code has been given for his reference purpose only.Excuse me since English is not my first language.I misspelled may be to should be.
phil.o 8-Oct-15 6:29am    
OP is an abbreviation for Original Poster => this is the person who asked the question.
[no name] 8-Oct-15 9:20am    
Thank you Sir for your kind information.
Andreas Gieriet 8-Oct-15 7:05am    
getch() comes from stdio.h (see stdio.h).
conio.h is not a standard C header file and might come with some compilers on some systems (see conio.h.
The odd thing is that both header files declare a getch() function and it's a matter of the linker command arguments sequence on which implementation is finally linked into the program.
Regards
Andi
PS: If you start your program in a console, it will not disappear when completed. If you run it during development in Visual Studio, then it will/will not disappear, depending on if you run it in/ouitside the Debugger (F5 versus CTRL-F5).

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900