What happens when a variable is assigned to zero in an `if` condition?












3















It would be helpful if anybody can explain this.



int main()
{
int a=0;
if(a=0)
printf("a is zerot");
else
printf("a is not zerot");
printf("Value of a is %dn",a);
return 0;
}


output of this is



a is not zero   Value of a is 0 









share|improve this question




















  • 6





    A line of your code is in error: if(a=0) should be if(a==0).

    – STLDev
    Jul 9 '13 at 5:36













  • You used the assignment operator not the comparison operator ==

    – Mr Moose
    Jul 9 '13 at 5:37






  • 3





    The title says "zero is assigned again" so presumably the second assignment is intentional. But I'm voting to close for general lack of clarity and research.

    – Jim Balter
    Jul 9 '13 at 5:43






  • 1





    @AndreyT The problem is that the text of the messages is backwards ... it would be right if they were inverted.

    – Jim Balter
    Jul 9 '13 at 5:45






  • 1





    @Ani: Er... Value of zero if treated as false in logical context (under if, for example). For this reason else branch of your if is taken. Why you wrote "a is not zero" in that branch is really a question to you. It is you, who for some strange reason arranged these messages "backwards". That's all there is to it.

    – AnT
    Jul 9 '13 at 6:27


















3















It would be helpful if anybody can explain this.



int main()
{
int a=0;
if(a=0)
printf("a is zerot");
else
printf("a is not zerot");
printf("Value of a is %dn",a);
return 0;
}


output of this is



a is not zero   Value of a is 0 









share|improve this question




















  • 6





    A line of your code is in error: if(a=0) should be if(a==0).

    – STLDev
    Jul 9 '13 at 5:36













  • You used the assignment operator not the comparison operator ==

    – Mr Moose
    Jul 9 '13 at 5:37






  • 3





    The title says "zero is assigned again" so presumably the second assignment is intentional. But I'm voting to close for general lack of clarity and research.

    – Jim Balter
    Jul 9 '13 at 5:43






  • 1





    @AndreyT The problem is that the text of the messages is backwards ... it would be right if they were inverted.

    – Jim Balter
    Jul 9 '13 at 5:45






  • 1





    @Ani: Er... Value of zero if treated as false in logical context (under if, for example). For this reason else branch of your if is taken. Why you wrote "a is not zero" in that branch is really a question to you. It is you, who for some strange reason arranged these messages "backwards". That's all there is to it.

    – AnT
    Jul 9 '13 at 6:27
















3












3








3


2






It would be helpful if anybody can explain this.



int main()
{
int a=0;
if(a=0)
printf("a is zerot");
else
printf("a is not zerot");
printf("Value of a is %dn",a);
return 0;
}


output of this is



a is not zero   Value of a is 0 









share|improve this question
















It would be helpful if anybody can explain this.



int main()
{
int a=0;
if(a=0)
printf("a is zerot");
else
printf("a is not zerot");
printf("Value of a is %dn",a);
return 0;
}


output of this is



a is not zero   Value of a is 0 






c






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 20:35









ealfonso

3,04722240




3,04722240










asked Jul 9 '13 at 5:35









AniAni

522313




522313








  • 6





    A line of your code is in error: if(a=0) should be if(a==0).

    – STLDev
    Jul 9 '13 at 5:36













  • You used the assignment operator not the comparison operator ==

    – Mr Moose
    Jul 9 '13 at 5:37






  • 3





    The title says "zero is assigned again" so presumably the second assignment is intentional. But I'm voting to close for general lack of clarity and research.

    – Jim Balter
    Jul 9 '13 at 5:43






  • 1





    @AndreyT The problem is that the text of the messages is backwards ... it would be right if they were inverted.

    – Jim Balter
    Jul 9 '13 at 5:45






  • 1





    @Ani: Er... Value of zero if treated as false in logical context (under if, for example). For this reason else branch of your if is taken. Why you wrote "a is not zero" in that branch is really a question to you. It is you, who for some strange reason arranged these messages "backwards". That's all there is to it.

    – AnT
    Jul 9 '13 at 6:27
















  • 6





    A line of your code is in error: if(a=0) should be if(a==0).

    – STLDev
    Jul 9 '13 at 5:36













  • You used the assignment operator not the comparison operator ==

    – Mr Moose
    Jul 9 '13 at 5:37






  • 3





    The title says "zero is assigned again" so presumably the second assignment is intentional. But I'm voting to close for general lack of clarity and research.

    – Jim Balter
    Jul 9 '13 at 5:43






  • 1





    @AndreyT The problem is that the text of the messages is backwards ... it would be right if they were inverted.

    – Jim Balter
    Jul 9 '13 at 5:45






  • 1





    @Ani: Er... Value of zero if treated as false in logical context (under if, for example). For this reason else branch of your if is taken. Why you wrote "a is not zero" in that branch is really a question to you. It is you, who for some strange reason arranged these messages "backwards". That's all there is to it.

    – AnT
    Jul 9 '13 at 6:27










6




6





A line of your code is in error: if(a=0) should be if(a==0).

– STLDev
Jul 9 '13 at 5:36







A line of your code is in error: if(a=0) should be if(a==0).

– STLDev
Jul 9 '13 at 5:36















You used the assignment operator not the comparison operator ==

– Mr Moose
Jul 9 '13 at 5:37





You used the assignment operator not the comparison operator ==

– Mr Moose
Jul 9 '13 at 5:37




3




3





The title says "zero is assigned again" so presumably the second assignment is intentional. But I'm voting to close for general lack of clarity and research.

– Jim Balter
Jul 9 '13 at 5:43





The title says "zero is assigned again" so presumably the second assignment is intentional. But I'm voting to close for general lack of clarity and research.

– Jim Balter
Jul 9 '13 at 5:43




1




1





@AndreyT The problem is that the text of the messages is backwards ... it would be right if they were inverted.

– Jim Balter
Jul 9 '13 at 5:45





@AndreyT The problem is that the text of the messages is backwards ... it would be right if they were inverted.

– Jim Balter
Jul 9 '13 at 5:45




1




1





@Ani: Er... Value of zero if treated as false in logical context (under if, for example). For this reason else branch of your if is taken. Why you wrote "a is not zero" in that branch is really a question to you. It is you, who for some strange reason arranged these messages "backwards". That's all there is to it.

– AnT
Jul 9 '13 at 6:27







@Ani: Er... Value of zero if treated as false in logical context (under if, for example). For this reason else branch of your if is taken. Why you wrote "a is not zero" in that branch is really a question to you. It is you, who for some strange reason arranged these messages "backwards". That's all there is to it.

– AnT
Jul 9 '13 at 6:27














4 Answers
4






active

oldest

votes


















29














The result of the assignment is the value of the expression.



Therefore:



if (a = 0)


is the same as:



if (0)


which is the same as:



if (false)


which will force the else path.






share|improve this answer































    7














    if(a=0)
    printf("a is zerot");
    else
    printf("a is not zerot");


    These messages are precisely backwards. The statement after the if is executed if the condition isn't 0, and the statement after the else is executed if the condition is 0, so this should be



    if(a=0)
    printf("a is not zerot");
    else
    printf("a is zerot");


    Or, equivalently but more clearly,



    a = 0;
    if(a)
    printf("a is not zerot");
    else
    printf("a is zerot");


    Which, together with



    printf("Value of a is %dn",a);


    would print



    a is zero   Value of a is 0 


    as expected.






    share|improve this answer





















    • 1





      @PHIfounder I know, it's strange how many folks at SO -- programmers -- are inattentive to details. Thanks for the upvote. I think I would have gotten more if I had dashed something off instead of taking the time to get it right ... I was still writing when the accepted answer was posted.

      – Jim Balter
      Jul 9 '13 at 5:59



















    0














    if(a=0) is assignement of 0 in variable a. if you want to compare a against zero you need to write like below
    if(a==0)



    your condition is simple assignment which is making a as zero so condition getting false and you are getting prints from else part.






    share|improve this answer


























    • The OP doesn't want ==. The OP has made extremely clear that assignment is intended.

      – Jim Balter
      Jul 9 '13 at 5:50











    • @JimBalter ya i know that,That is why i have given reason behind failing of condition and why prints from else part.I suggested comparison also just to be on safe side.

      – Dayal rai
      Jul 9 '13 at 5:52





















    0














    If () function accepts true or false value as an argument.



    So whatever you put inside the bracket has no significance to if() function but of the fact what value it has.



    '0' in any case is considered as false value, so when you pass 0 as an argument like:



    if(0)
    {
    ---statments---
    }


    The statement part of will not get executed, and the system will directly jump to else part.



    In the case you mentioned you assigned 0 to your variable and passed it as an argument to if().
    Note that if() only accepts 0 or non 0 value. So, it doesn't matter what assignment you made. if() will recieve the value of your variable 'a' as an argument and act accordingly.



    In this case, since the value of a is 0, the if part will not get executed and the system will jump to else.






    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f17540692%2fwhat-happens-when-a-variable-is-assigned-to-zero-in-an-if-condition%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      4 Answers
      4






      active

      oldest

      votes








      4 Answers
      4






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      29














      The result of the assignment is the value of the expression.



      Therefore:



      if (a = 0)


      is the same as:



      if (0)


      which is the same as:



      if (false)


      which will force the else path.






      share|improve this answer




























        29














        The result of the assignment is the value of the expression.



        Therefore:



        if (a = 0)


        is the same as:



        if (0)


        which is the same as:



        if (false)


        which will force the else path.






        share|improve this answer


























          29












          29








          29







          The result of the assignment is the value of the expression.



          Therefore:



          if (a = 0)


          is the same as:



          if (0)


          which is the same as:



          if (false)


          which will force the else path.






          share|improve this answer













          The result of the assignment is the value of the expression.



          Therefore:



          if (a = 0)


          is the same as:



          if (0)


          which is the same as:



          if (false)


          which will force the else path.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jul 9 '13 at 5:37









          trojanfoetrojanfoe

          106k15172210




          106k15172210

























              7














              if(a=0)
              printf("a is zerot");
              else
              printf("a is not zerot");


              These messages are precisely backwards. The statement after the if is executed if the condition isn't 0, and the statement after the else is executed if the condition is 0, so this should be



              if(a=0)
              printf("a is not zerot");
              else
              printf("a is zerot");


              Or, equivalently but more clearly,



              a = 0;
              if(a)
              printf("a is not zerot");
              else
              printf("a is zerot");


              Which, together with



              printf("Value of a is %dn",a);


              would print



              a is zero   Value of a is 0 


              as expected.






              share|improve this answer





















              • 1





                @PHIfounder I know, it's strange how many folks at SO -- programmers -- are inattentive to details. Thanks for the upvote. I think I would have gotten more if I had dashed something off instead of taking the time to get it right ... I was still writing when the accepted answer was posted.

                – Jim Balter
                Jul 9 '13 at 5:59
















              7














              if(a=0)
              printf("a is zerot");
              else
              printf("a is not zerot");


              These messages are precisely backwards. The statement after the if is executed if the condition isn't 0, and the statement after the else is executed if the condition is 0, so this should be



              if(a=0)
              printf("a is not zerot");
              else
              printf("a is zerot");


              Or, equivalently but more clearly,



              a = 0;
              if(a)
              printf("a is not zerot");
              else
              printf("a is zerot");


              Which, together with



              printf("Value of a is %dn",a);


              would print



              a is zero   Value of a is 0 


              as expected.






              share|improve this answer





















              • 1





                @PHIfounder I know, it's strange how many folks at SO -- programmers -- are inattentive to details. Thanks for the upvote. I think I would have gotten more if I had dashed something off instead of taking the time to get it right ... I was still writing when the accepted answer was posted.

                – Jim Balter
                Jul 9 '13 at 5:59














              7












              7








              7







              if(a=0)
              printf("a is zerot");
              else
              printf("a is not zerot");


              These messages are precisely backwards. The statement after the if is executed if the condition isn't 0, and the statement after the else is executed if the condition is 0, so this should be



              if(a=0)
              printf("a is not zerot");
              else
              printf("a is zerot");


              Or, equivalently but more clearly,



              a = 0;
              if(a)
              printf("a is not zerot");
              else
              printf("a is zerot");


              Which, together with



              printf("Value of a is %dn",a);


              would print



              a is zero   Value of a is 0 


              as expected.






              share|improve this answer















              if(a=0)
              printf("a is zerot");
              else
              printf("a is not zerot");


              These messages are precisely backwards. The statement after the if is executed if the condition isn't 0, and the statement after the else is executed if the condition is 0, so this should be



              if(a=0)
              printf("a is not zerot");
              else
              printf("a is zerot");


              Or, equivalently but more clearly,



              a = 0;
              if(a)
              printf("a is not zerot");
              else
              printf("a is zerot");


              Which, together with



              printf("Value of a is %dn",a);


              would print



              a is zero   Value of a is 0 


              as expected.







              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Nov 26 '18 at 20:29

























              answered Jul 9 '13 at 5:47









              Jim BalterJim Balter

              13.5k12954




              13.5k12954








              • 1





                @PHIfounder I know, it's strange how many folks at SO -- programmers -- are inattentive to details. Thanks for the upvote. I think I would have gotten more if I had dashed something off instead of taking the time to get it right ... I was still writing when the accepted answer was posted.

                – Jim Balter
                Jul 9 '13 at 5:59














              • 1





                @PHIfounder I know, it's strange how many folks at SO -- programmers -- are inattentive to details. Thanks for the upvote. I think I would have gotten more if I had dashed something off instead of taking the time to get it right ... I was still writing when the accepted answer was posted.

                – Jim Balter
                Jul 9 '13 at 5:59








              1




              1





              @PHIfounder I know, it's strange how many folks at SO -- programmers -- are inattentive to details. Thanks for the upvote. I think I would have gotten more if I had dashed something off instead of taking the time to get it right ... I was still writing when the accepted answer was posted.

              – Jim Balter
              Jul 9 '13 at 5:59





              @PHIfounder I know, it's strange how many folks at SO -- programmers -- are inattentive to details. Thanks for the upvote. I think I would have gotten more if I had dashed something off instead of taking the time to get it right ... I was still writing when the accepted answer was posted.

              – Jim Balter
              Jul 9 '13 at 5:59











              0














              if(a=0) is assignement of 0 in variable a. if you want to compare a against zero you need to write like below
              if(a==0)



              your condition is simple assignment which is making a as zero so condition getting false and you are getting prints from else part.






              share|improve this answer


























              • The OP doesn't want ==. The OP has made extremely clear that assignment is intended.

                – Jim Balter
                Jul 9 '13 at 5:50











              • @JimBalter ya i know that,That is why i have given reason behind failing of condition and why prints from else part.I suggested comparison also just to be on safe side.

                – Dayal rai
                Jul 9 '13 at 5:52


















              0














              if(a=0) is assignement of 0 in variable a. if you want to compare a against zero you need to write like below
              if(a==0)



              your condition is simple assignment which is making a as zero so condition getting false and you are getting prints from else part.






              share|improve this answer


























              • The OP doesn't want ==. The OP has made extremely clear that assignment is intended.

                – Jim Balter
                Jul 9 '13 at 5:50











              • @JimBalter ya i know that,That is why i have given reason behind failing of condition and why prints from else part.I suggested comparison also just to be on safe side.

                – Dayal rai
                Jul 9 '13 at 5:52
















              0












              0








              0







              if(a=0) is assignement of 0 in variable a. if you want to compare a against zero you need to write like below
              if(a==0)



              your condition is simple assignment which is making a as zero so condition getting false and you are getting prints from else part.






              share|improve this answer















              if(a=0) is assignement of 0 in variable a. if you want to compare a against zero you need to write like below
              if(a==0)



              your condition is simple assignment which is making a as zero so condition getting false and you are getting prints from else part.







              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Jul 9 '13 at 5:49

























              answered Jul 9 '13 at 5:37









              Dayal raiDayal rai

              5,7391627




              5,7391627













              • The OP doesn't want ==. The OP has made extremely clear that assignment is intended.

                – Jim Balter
                Jul 9 '13 at 5:50











              • @JimBalter ya i know that,That is why i have given reason behind failing of condition and why prints from else part.I suggested comparison also just to be on safe side.

                – Dayal rai
                Jul 9 '13 at 5:52





















              • The OP doesn't want ==. The OP has made extremely clear that assignment is intended.

                – Jim Balter
                Jul 9 '13 at 5:50











              • @JimBalter ya i know that,That is why i have given reason behind failing of condition and why prints from else part.I suggested comparison also just to be on safe side.

                – Dayal rai
                Jul 9 '13 at 5:52



















              The OP doesn't want ==. The OP has made extremely clear that assignment is intended.

              – Jim Balter
              Jul 9 '13 at 5:50





              The OP doesn't want ==. The OP has made extremely clear that assignment is intended.

              – Jim Balter
              Jul 9 '13 at 5:50













              @JimBalter ya i know that,That is why i have given reason behind failing of condition and why prints from else part.I suggested comparison also just to be on safe side.

              – Dayal rai
              Jul 9 '13 at 5:52







              @JimBalter ya i know that,That is why i have given reason behind failing of condition and why prints from else part.I suggested comparison also just to be on safe side.

              – Dayal rai
              Jul 9 '13 at 5:52













              0














              If () function accepts true or false value as an argument.



              So whatever you put inside the bracket has no significance to if() function but of the fact what value it has.



              '0' in any case is considered as false value, so when you pass 0 as an argument like:



              if(0)
              {
              ---statments---
              }


              The statement part of will not get executed, and the system will directly jump to else part.



              In the case you mentioned you assigned 0 to your variable and passed it as an argument to if().
              Note that if() only accepts 0 or non 0 value. So, it doesn't matter what assignment you made. if() will recieve the value of your variable 'a' as an argument and act accordingly.



              In this case, since the value of a is 0, the if part will not get executed and the system will jump to else.






              share|improve this answer




























                0














                If () function accepts true or false value as an argument.



                So whatever you put inside the bracket has no significance to if() function but of the fact what value it has.



                '0' in any case is considered as false value, so when you pass 0 as an argument like:



                if(0)
                {
                ---statments---
                }


                The statement part of will not get executed, and the system will directly jump to else part.



                In the case you mentioned you assigned 0 to your variable and passed it as an argument to if().
                Note that if() only accepts 0 or non 0 value. So, it doesn't matter what assignment you made. if() will recieve the value of your variable 'a' as an argument and act accordingly.



                In this case, since the value of a is 0, the if part will not get executed and the system will jump to else.






                share|improve this answer


























                  0












                  0








                  0







                  If () function accepts true or false value as an argument.



                  So whatever you put inside the bracket has no significance to if() function but of the fact what value it has.



                  '0' in any case is considered as false value, so when you pass 0 as an argument like:



                  if(0)
                  {
                  ---statments---
                  }


                  The statement part of will not get executed, and the system will directly jump to else part.



                  In the case you mentioned you assigned 0 to your variable and passed it as an argument to if().
                  Note that if() only accepts 0 or non 0 value. So, it doesn't matter what assignment you made. if() will recieve the value of your variable 'a' as an argument and act accordingly.



                  In this case, since the value of a is 0, the if part will not get executed and the system will jump to else.






                  share|improve this answer













                  If () function accepts true or false value as an argument.



                  So whatever you put inside the bracket has no significance to if() function but of the fact what value it has.



                  '0' in any case is considered as false value, so when you pass 0 as an argument like:



                  if(0)
                  {
                  ---statments---
                  }


                  The statement part of will not get executed, and the system will directly jump to else part.



                  In the case you mentioned you assigned 0 to your variable and passed it as an argument to if().
                  Note that if() only accepts 0 or non 0 value. So, it doesn't matter what assignment you made. if() will recieve the value of your variable 'a' as an argument and act accordingly.



                  In this case, since the value of a is 0, the if part will not get executed and the system will jump to else.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jul 9 '13 at 13:46









                  Chetan BhasinChetan Bhasin

                  2,2551633




                  2,2551633






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f17540692%2fwhat-happens-when-a-variable-is-assigned-to-zero-in-an-if-condition%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Florida Star v. B. J. F.

                      Danny Elfman

                      Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues