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












0
















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 '18 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.























    0
















    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 '18 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.





















      0












      0








      0









      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 '18 at 6:47









      Kanchana GoreKanchana 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 '18 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 '18 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


















          2














          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

































            1














            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































              1














              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































                1















                • 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









                  2














                  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






























                    2














                    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




























                      2












                      2








                      2







                      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 '18 at 7:35

























                      answered Nov 22 '18 at 7:10









                      Sayed SohanSayed Sohan

                      659




                      659

























                          1














                          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




























                            1














                            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


























                              1












                              1








                              1







                              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 '18 at 7:21









                              MikeMike

                              2,0051722




                              2,0051722























                                  1














                                  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




























                                    1














                                    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


























                                      1












                                      1








                                      1







                                      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 '18 at 7:23









                                      GermanNerdGermanNerd

                                      492111




                                      492111























                                          1















                                          • 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






























                                            1















                                            • 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




























                                              1












                                              1








                                              1








                                              • 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 '18 at 7:35

























                                              answered Nov 22 '18 at 7:03









                                              P.WP.W

                                              12.8k3945




                                              12.8k3945















                                                  Popular posts from this blog

                                                  Create new schema in PostgreSQL using DBeaver

                                                  Deepest pit of an array with Javascript: test on Codility

                                                  Costa Masnaga