What is happening here while post/pre decrementing a char variable in C [duplicate]











up vote
0
down vote

favorite













This question already has an answer here:




  • What is the difference between ++i and i++?

    20 answers




I was solving some multiple choice C codes from a book. Two of the questions involve pre decrementing, post decrementing a char variable initialised at 0. The output for both these is very different. I dont understand whats going on there.



Code 1



char i=0;
do
{
printf("%d ",i);
}while(i--);
return 0;


The output for this snippet is 0.



Code 2



char i=0;
do
{
printf("%d ",i);
}while(--i);
return 0;


The output is for this one is

0,-1,-2,.....-128,127,126,......1 .



Can anyone explain why this is happening?










share|improve this question













marked as duplicate by Mitch Wheat, n.m., Lundin c
Users with the  c badge can single-handedly close c questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 at 7:29


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















    up vote
    0
    down vote

    favorite













    This question already has an answer here:




    • What is the difference between ++i and i++?

      20 answers




    I was solving some multiple choice C codes from a book. Two of the questions involve pre decrementing, post decrementing a char variable initialised at 0. The output for both these is very different. I dont understand whats going on there.



    Code 1



    char i=0;
    do
    {
    printf("%d ",i);
    }while(i--);
    return 0;


    The output for this snippet is 0.



    Code 2



    char i=0;
    do
    {
    printf("%d ",i);
    }while(--i);
    return 0;


    The output is for this one is

    0,-1,-2,.....-128,127,126,......1 .



    Can anyone explain why this is happening?










    share|improve this question













    marked as duplicate by Mitch Wheat, n.m., Lundin c
    Users with the  c badge can single-handedly close c questions as duplicates and reopen them as needed.

    StackExchange.ready(function() {
    if (StackExchange.options.isMobile) return;

    $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
    var $hover = $(this).addClass('hover-bound'),
    $msg = $hover.siblings('.dupe-hammer-message');

    $hover.hover(
    function() {
    $hover.showInfoMessage('', {
    messageElement: $msg.clone().show(),
    transient: false,
    position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
    dismissable: false,
    relativeToBody: true
    });
    },
    function() {
    StackExchange.helpers.removeMessages();
    }
    );
    });
    });
    Nov 22 at 7:29


    This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

















      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite












      This question already has an answer here:




      • What is the difference between ++i and i++?

        20 answers




      I was solving some multiple choice C codes from a book. Two of the questions involve pre decrementing, post decrementing a char variable initialised at 0. The output for both these is very different. I dont understand whats going on there.



      Code 1



      char i=0;
      do
      {
      printf("%d ",i);
      }while(i--);
      return 0;


      The output for this snippet is 0.



      Code 2



      char i=0;
      do
      {
      printf("%d ",i);
      }while(--i);
      return 0;


      The output is for this one is

      0,-1,-2,.....-128,127,126,......1 .



      Can anyone explain why this is happening?










      share|improve this question














      This question already has an answer here:




      • What is the difference between ++i and i++?

        20 answers




      I was solving some multiple choice C codes from a book. Two of the questions involve pre decrementing, post decrementing a char variable initialised at 0. The output for both these is very different. I dont understand whats going on there.



      Code 1



      char i=0;
      do
      {
      printf("%d ",i);
      }while(i--);
      return 0;


      The output for this snippet is 0.



      Code 2



      char i=0;
      do
      {
      printf("%d ",i);
      }while(--i);
      return 0;


      The output is for this one is

      0,-1,-2,.....-128,127,126,......1 .



      Can anyone explain why this is happening?





      This question already has an answer here:




      • What is the difference between ++i and i++?

        20 answers








      c char






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 22 at 6:47









      Kanchana Gore

      11




      11




      marked as duplicate by Mitch Wheat, n.m., Lundin c
      Users with the  c badge can single-handedly close c questions as duplicates and reopen them as needed.

      StackExchange.ready(function() {
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function() {
      $hover.showInfoMessage('', {
      messageElement: $msg.clone().show(),
      transient: false,
      position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
      dismissable: false,
      relativeToBody: true
      });
      },
      function() {
      StackExchange.helpers.removeMessages();
      }
      );
      });
      });
      Nov 22 at 7:29


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






      marked as duplicate by Mitch Wheat, n.m., Lundin c
      Users with the  c badge can single-handedly close c questions as duplicates and reopen them as needed.

      StackExchange.ready(function() {
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function() {
      $hover.showInfoMessage('', {
      messageElement: $msg.clone().show(),
      transient: false,
      position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
      dismissable: false,
      relativeToBody: true
      });
      },
      function() {
      StackExchange.helpers.removeMessages();
      }
      );
      });
      });
      Nov 22 at 7:29


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


























          4 Answers
          4






          active

          oldest

          votes

















          up vote
          2
          down vote













          At both code while loop checking i==0 or not. If i!=0it will keep going on.



          At first code value of i initially 0. So after printing 0 it checks i==0 or not. if i==0 it will break the loop or keep going on by decrementing i. So in code 1 post decrementing used. check value first then decrements the value.



          At second code value of i initially 0. So after printing 0 it decrements i then it checks if i==0 or not. it is pre decrement. decrement value first then check.



          Here, i is char which size is 1 byte and range -128 to 127. So after decrementing value 0 to -1 it keep decrementing until it goes to 0 and exit the loop by printing 0,-1,...,-128,127...1 .






          share|improve this answer






























            up vote
            1
            down vote













            Code 1



            char i=0;
            do
            {
            printf("%d ",i); // print o
            }while(i--); //check i = 0, means false, loop ends, then increment i
            return 0;


            Code 2



            char i=0;
            do
            {
            printf("%d ",i); //print 0
            }while(--i); //decrement i, check i=-1, means true, next cycle, loop until i = 0 which means false
            return 0;





            share|improve this answer




























              up vote
              1
              down vote













              Both i-- and --i are expressions. An expression is (part of) a statement that can yield a value. As per definition, the pre-increment version increments first, then yields the value. For the post-increment version it is the other way round.



              This is completely independent of whether the expression is used in a while statement or elsewhere. However, when using such expressions, you need to be aware of operator precendence.






              share|improve this answer




























                up vote
                1
                down vote














                • Initial value of i is 0.

                • In Code 1, first while check happens in which the value of i (= 0) is used and then i is decremented because it is postfix decrement. So it exits while after printing 0.

                • In Code 2, because it is prefix decrement, i is decremented first and its value (= -1) is used when the while check is performed. Here it exits after printing the entire range of values a signed char can hold because it becomes 0 at the end only.






                share|improve this answer






























                  4 Answers
                  4






                  active

                  oldest

                  votes








                  4 Answers
                  4






                  active

                  oldest

                  votes









                  active

                  oldest

                  votes






                  active

                  oldest

                  votes








                  up vote
                  2
                  down vote













                  At both code while loop checking i==0 or not. If i!=0it will keep going on.



                  At first code value of i initially 0. So after printing 0 it checks i==0 or not. if i==0 it will break the loop or keep going on by decrementing i. So in code 1 post decrementing used. check value first then decrements the value.



                  At second code value of i initially 0. So after printing 0 it decrements i then it checks if i==0 or not. it is pre decrement. decrement value first then check.



                  Here, i is char which size is 1 byte and range -128 to 127. So after decrementing value 0 to -1 it keep decrementing until it goes to 0 and exit the loop by printing 0,-1,...,-128,127...1 .






                  share|improve this answer



























                    up vote
                    2
                    down vote













                    At both code while loop checking i==0 or not. If i!=0it will keep going on.



                    At first code value of i initially 0. So after printing 0 it checks i==0 or not. if i==0 it will break the loop or keep going on by decrementing i. So in code 1 post decrementing used. check value first then decrements the value.



                    At second code value of i initially 0. So after printing 0 it decrements i then it checks if i==0 or not. it is pre decrement. decrement value first then check.



                    Here, i is char which size is 1 byte and range -128 to 127. So after decrementing value 0 to -1 it keep decrementing until it goes to 0 and exit the loop by printing 0,-1,...,-128,127...1 .






                    share|improve this answer

























                      up vote
                      2
                      down vote










                      up vote
                      2
                      down vote









                      At both code while loop checking i==0 or not. If i!=0it will keep going on.



                      At first code value of i initially 0. So after printing 0 it checks i==0 or not. if i==0 it will break the loop or keep going on by decrementing i. So in code 1 post decrementing used. check value first then decrements the value.



                      At second code value of i initially 0. So after printing 0 it decrements i then it checks if i==0 or not. it is pre decrement. decrement value first then check.



                      Here, i is char which size is 1 byte and range -128 to 127. So after decrementing value 0 to -1 it keep decrementing until it goes to 0 and exit the loop by printing 0,-1,...,-128,127...1 .






                      share|improve this answer














                      At both code while loop checking i==0 or not. If i!=0it will keep going on.



                      At first code value of i initially 0. So after printing 0 it checks i==0 or not. if i==0 it will break the loop or keep going on by decrementing i. So in code 1 post decrementing used. check value first then decrements the value.



                      At second code value of i initially 0. So after printing 0 it decrements i then it checks if i==0 or not. it is pre decrement. decrement value first then check.



                      Here, i is char which size is 1 byte and range -128 to 127. So after decrementing value 0 to -1 it keep decrementing until it goes to 0 and exit the loop by printing 0,-1,...,-128,127...1 .







                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Nov 22 at 7:35

























                      answered Nov 22 at 7:10









                      Sayed Sohan

                      659




                      659
























                          up vote
                          1
                          down vote













                          Code 1



                          char i=0;
                          do
                          {
                          printf("%d ",i); // print o
                          }while(i--); //check i = 0, means false, loop ends, then increment i
                          return 0;


                          Code 2



                          char i=0;
                          do
                          {
                          printf("%d ",i); //print 0
                          }while(--i); //decrement i, check i=-1, means true, next cycle, loop until i = 0 which means false
                          return 0;





                          share|improve this answer

























                            up vote
                            1
                            down vote













                            Code 1



                            char i=0;
                            do
                            {
                            printf("%d ",i); // print o
                            }while(i--); //check i = 0, means false, loop ends, then increment i
                            return 0;


                            Code 2



                            char i=0;
                            do
                            {
                            printf("%d ",i); //print 0
                            }while(--i); //decrement i, check i=-1, means true, next cycle, loop until i = 0 which means false
                            return 0;





                            share|improve this answer























                              up vote
                              1
                              down vote










                              up vote
                              1
                              down vote









                              Code 1



                              char i=0;
                              do
                              {
                              printf("%d ",i); // print o
                              }while(i--); //check i = 0, means false, loop ends, then increment i
                              return 0;


                              Code 2



                              char i=0;
                              do
                              {
                              printf("%d ",i); //print 0
                              }while(--i); //decrement i, check i=-1, means true, next cycle, loop until i = 0 which means false
                              return 0;





                              share|improve this answer












                              Code 1



                              char i=0;
                              do
                              {
                              printf("%d ",i); // print o
                              }while(i--); //check i = 0, means false, loop ends, then increment i
                              return 0;


                              Code 2



                              char i=0;
                              do
                              {
                              printf("%d ",i); //print 0
                              }while(--i); //decrement i, check i=-1, means true, next cycle, loop until i = 0 which means false
                              return 0;






                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Nov 22 at 7:21









                              Mike

                              1,9831521




                              1,9831521






















                                  up vote
                                  1
                                  down vote













                                  Both i-- and --i are expressions. An expression is (part of) a statement that can yield a value. As per definition, the pre-increment version increments first, then yields the value. For the post-increment version it is the other way round.



                                  This is completely independent of whether the expression is used in a while statement or elsewhere. However, when using such expressions, you need to be aware of operator precendence.






                                  share|improve this answer

























                                    up vote
                                    1
                                    down vote













                                    Both i-- and --i are expressions. An expression is (part of) a statement that can yield a value. As per definition, the pre-increment version increments first, then yields the value. For the post-increment version it is the other way round.



                                    This is completely independent of whether the expression is used in a while statement or elsewhere. However, when using such expressions, you need to be aware of operator precendence.






                                    share|improve this answer























                                      up vote
                                      1
                                      down vote










                                      up vote
                                      1
                                      down vote









                                      Both i-- and --i are expressions. An expression is (part of) a statement that can yield a value. As per definition, the pre-increment version increments first, then yields the value. For the post-increment version it is the other way round.



                                      This is completely independent of whether the expression is used in a while statement or elsewhere. However, when using such expressions, you need to be aware of operator precendence.






                                      share|improve this answer












                                      Both i-- and --i are expressions. An expression is (part of) a statement that can yield a value. As per definition, the pre-increment version increments first, then yields the value. For the post-increment version it is the other way round.



                                      This is completely independent of whether the expression is used in a while statement or elsewhere. However, when using such expressions, you need to be aware of operator precendence.







                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Nov 22 at 7:23









                                      GermanNerd

                                      447111




                                      447111






















                                          up vote
                                          1
                                          down vote














                                          • Initial value of i is 0.

                                          • In Code 1, first while check happens in which the value of i (= 0) is used and then i is decremented because it is postfix decrement. So it exits while after printing 0.

                                          • In Code 2, because it is prefix decrement, i is decremented first and its value (= -1) is used when the while check is performed. Here it exits after printing the entire range of values a signed char can hold because it becomes 0 at the end only.






                                          share|improve this answer



























                                            up vote
                                            1
                                            down vote














                                            • Initial value of i is 0.

                                            • In Code 1, first while check happens in which the value of i (= 0) is used and then i is decremented because it is postfix decrement. So it exits while after printing 0.

                                            • In Code 2, because it is prefix decrement, i is decremented first and its value (= -1) is used when the while check is performed. Here it exits after printing the entire range of values a signed char can hold because it becomes 0 at the end only.






                                            share|improve this answer

























                                              up vote
                                              1
                                              down vote










                                              up vote
                                              1
                                              down vote










                                              • Initial value of i is 0.

                                              • In Code 1, first while check happens in which the value of i (= 0) is used and then i is decremented because it is postfix decrement. So it exits while after printing 0.

                                              • In Code 2, because it is prefix decrement, i is decremented first and its value (= -1) is used when the while check is performed. Here it exits after printing the entire range of values a signed char can hold because it becomes 0 at the end only.






                                              share|improve this answer















                                              • Initial value of i is 0.

                                              • In Code 1, first while check happens in which the value of i (= 0) is used and then i is decremented because it is postfix decrement. So it exits while after printing 0.

                                              • In Code 2, because it is prefix decrement, i is decremented first and its value (= -1) is used when the while check is performed. Here it exits after printing the entire range of values a signed char can hold because it becomes 0 at the end only.







                                              share|improve this answer














                                              share|improve this answer



                                              share|improve this answer








                                              edited Nov 22 at 7:35

























                                              answered Nov 22 at 7:03









                                              P.W

                                              10.5k2742




                                              10.5k2742















                                                  Popular posts from this blog

                                                  Basket-ball féminin

                                                  Different font size/position of beamer's navigation symbols template's content depending on regular/plain...

                                                  I want to find a topological embedding $f : X rightarrow Y$ and $g: Y rightarrow X$, yet $X$ is not...