Why do I get a compiler error, when applying the increment operator to a constant variable











up vote
1
down vote

favorite












If I declare a constant variable



int const n=100; cout<<n+1<<endl;


The console shows the value as 101



but when I write a code like this:



int const n=100;
n++;
cout<<n<<endl;


There is a compile time error:




main.cpp: In function 'int main()':
main.cpp:6:5: error: increment of read-only variable 'n'



Is the second case different from the first case?










share|improve this question









New contributor




Mudit Sharma is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • 1




    Why did you declare it constant if you were planning to change it?
    – Ayxan
    2 days ago










  • The exact error message should be given.
    – ederag
    2 days ago










  • @ederag It's pretty obvious what the exact error message would be in case of trying to increment a constant variable. OP asks for the difference of incrementing and creation of temporary values like n+1.
    – πάντα ῥεῖ
    2 days ago












  • @πάνταῥεῖ Sure. Just recalling good practices for a new user. Having the exact message makes the question easily reachable by searching for the error message. Remember that SO is not a forum. Good questions are useful not only to the original poster.
    – ederag
    2 days ago










  • @ederag Satisfied now?
    – πάντα ῥεῖ
    2 days ago















up vote
1
down vote

favorite












If I declare a constant variable



int const n=100; cout<<n+1<<endl;


The console shows the value as 101



but when I write a code like this:



int const n=100;
n++;
cout<<n<<endl;


There is a compile time error:




main.cpp: In function 'int main()':
main.cpp:6:5: error: increment of read-only variable 'n'



Is the second case different from the first case?










share|improve this question









New contributor




Mudit Sharma is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • 1




    Why did you declare it constant if you were planning to change it?
    – Ayxan
    2 days ago










  • The exact error message should be given.
    – ederag
    2 days ago










  • @ederag It's pretty obvious what the exact error message would be in case of trying to increment a constant variable. OP asks for the difference of incrementing and creation of temporary values like n+1.
    – πάντα ῥεῖ
    2 days ago












  • @πάνταῥεῖ Sure. Just recalling good practices for a new user. Having the exact message makes the question easily reachable by searching for the error message. Remember that SO is not a forum. Good questions are useful not only to the original poster.
    – ederag
    2 days ago










  • @ederag Satisfied now?
    – πάντα ῥεῖ
    2 days ago













up vote
1
down vote

favorite









up vote
1
down vote

favorite











If I declare a constant variable



int const n=100; cout<<n+1<<endl;


The console shows the value as 101



but when I write a code like this:



int const n=100;
n++;
cout<<n<<endl;


There is a compile time error:




main.cpp: In function 'int main()':
main.cpp:6:5: error: increment of read-only variable 'n'



Is the second case different from the first case?










share|improve this question









New contributor




Mudit Sharma is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











If I declare a constant variable



int const n=100; cout<<n+1<<endl;


The console shows the value as 101



but when I write a code like this:



int const n=100;
n++;
cout<<n<<endl;


There is a compile time error:




main.cpp: In function 'int main()':
main.cpp:6:5: error: increment of read-only variable 'n'



Is the second case different from the first case?







c++






share|improve this question









New contributor




Mudit Sharma is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Mudit Sharma is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 2 days ago









πάντα ῥεῖ

71.1k970132




71.1k970132






New contributor




Mudit Sharma is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 2 days ago









Mudit Sharma

142




142




New contributor




Mudit Sharma is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Mudit Sharma is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Mudit Sharma is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








  • 1




    Why did you declare it constant if you were planning to change it?
    – Ayxan
    2 days ago










  • The exact error message should be given.
    – ederag
    2 days ago










  • @ederag It's pretty obvious what the exact error message would be in case of trying to increment a constant variable. OP asks for the difference of incrementing and creation of temporary values like n+1.
    – πάντα ῥεῖ
    2 days ago












  • @πάνταῥεῖ Sure. Just recalling good practices for a new user. Having the exact message makes the question easily reachable by searching for the error message. Remember that SO is not a forum. Good questions are useful not only to the original poster.
    – ederag
    2 days ago










  • @ederag Satisfied now?
    – πάντα ῥεῖ
    2 days ago














  • 1




    Why did you declare it constant if you were planning to change it?
    – Ayxan
    2 days ago










  • The exact error message should be given.
    – ederag
    2 days ago










  • @ederag It's pretty obvious what the exact error message would be in case of trying to increment a constant variable. OP asks for the difference of incrementing and creation of temporary values like n+1.
    – πάντα ῥεῖ
    2 days ago












  • @πάνταῥεῖ Sure. Just recalling good practices for a new user. Having the exact message makes the question easily reachable by searching for the error message. Remember that SO is not a forum. Good questions are useful not only to the original poster.
    – ederag
    2 days ago










  • @ederag Satisfied now?
    – πάντα ῥεῖ
    2 days ago








1




1




Why did you declare it constant if you were planning to change it?
– Ayxan
2 days ago




Why did you declare it constant if you were planning to change it?
– Ayxan
2 days ago












The exact error message should be given.
– ederag
2 days ago




The exact error message should be given.
– ederag
2 days ago












@ederag It's pretty obvious what the exact error message would be in case of trying to increment a constant variable. OP asks for the difference of incrementing and creation of temporary values like n+1.
– πάντα ῥεῖ
2 days ago






@ederag It's pretty obvious what the exact error message would be in case of trying to increment a constant variable. OP asks for the difference of incrementing and creation of temporary values like n+1.
– πάντα ῥεῖ
2 days ago














@πάνταῥεῖ Sure. Just recalling good practices for a new user. Having the exact message makes the question easily reachable by searching for the error message. Remember that SO is not a forum. Good questions are useful not only to the original poster.
– ederag
2 days ago




@πάνταῥεῖ Sure. Just recalling good practices for a new user. Having the exact message makes the question easily reachable by searching for the error message. Remember that SO is not a forum. Good questions are useful not only to the original poster.
– ederag
2 days ago












@ederag Satisfied now?
– πάντα ῥεῖ
2 days ago




@ederag Satisfied now?
– πάντα ῥεῖ
2 days ago












2 Answers
2






active

oldest

votes

















up vote
2
down vote














Is the second case different from the first case?




Yes they are fundamentally different.



int const n=100; 
n++;


The increment operator obviously cannot applied for a const(ant) variable, because the const keyword prevents it to be changed after the initial definition. That's why the compiler error is issued.



In the other case, the variable itself isn't changed, but another temporary value is created when it's passed to the operator<<() of std::cout.






share|improve this answer






























    up vote
    1
    down vote













    In the first case the compiler is asked to compute the output of adding a constant to an integer. This causes no error.



    In the second case, the compiler is asked to change the value of a constant. This is illegal and results in a compiler error.






    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',
      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
      });


      }
      });






      Mudit Sharma is a new contributor. Be nice, and check out our Code of Conduct.










       

      draft saved


      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53239287%2fwhy-do-i-get-a-compiler-error-when-applying-the-increment-operator-to-a-constan%23new-answer', 'question_page');
      }
      );

      Post as a guest
































      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      2
      down vote














      Is the second case different from the first case?




      Yes they are fundamentally different.



      int const n=100; 
      n++;


      The increment operator obviously cannot applied for a const(ant) variable, because the const keyword prevents it to be changed after the initial definition. That's why the compiler error is issued.



      In the other case, the variable itself isn't changed, but another temporary value is created when it's passed to the operator<<() of std::cout.






      share|improve this answer



























        up vote
        2
        down vote














        Is the second case different from the first case?




        Yes they are fundamentally different.



        int const n=100; 
        n++;


        The increment operator obviously cannot applied for a const(ant) variable, because the const keyword prevents it to be changed after the initial definition. That's why the compiler error is issued.



        In the other case, the variable itself isn't changed, but another temporary value is created when it's passed to the operator<<() of std::cout.






        share|improve this answer

























          up vote
          2
          down vote










          up vote
          2
          down vote










          Is the second case different from the first case?




          Yes they are fundamentally different.



          int const n=100; 
          n++;


          The increment operator obviously cannot applied for a const(ant) variable, because the const keyword prevents it to be changed after the initial definition. That's why the compiler error is issued.



          In the other case, the variable itself isn't changed, but another temporary value is created when it's passed to the operator<<() of std::cout.






          share|improve this answer















          Is the second case different from the first case?




          Yes they are fundamentally different.



          int const n=100; 
          n++;


          The increment operator obviously cannot applied for a const(ant) variable, because the const keyword prevents it to be changed after the initial definition. That's why the compiler error is issued.



          In the other case, the variable itself isn't changed, but another temporary value is created when it's passed to the operator<<() of std::cout.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 2 days ago

























          answered 2 days ago









          πάντα ῥεῖ

          71.1k970132




          71.1k970132
























              up vote
              1
              down vote













              In the first case the compiler is asked to compute the output of adding a constant to an integer. This causes no error.



              In the second case, the compiler is asked to change the value of a constant. This is illegal and results in a compiler error.






              share|improve this answer

























                up vote
                1
                down vote













                In the first case the compiler is asked to compute the output of adding a constant to an integer. This causes no error.



                In the second case, the compiler is asked to change the value of a constant. This is illegal and results in a compiler error.






                share|improve this answer























                  up vote
                  1
                  down vote










                  up vote
                  1
                  down vote









                  In the first case the compiler is asked to compute the output of adding a constant to an integer. This causes no error.



                  In the second case, the compiler is asked to change the value of a constant. This is illegal and results in a compiler error.






                  share|improve this answer












                  In the first case the compiler is asked to compute the output of adding a constant to an integer. This causes no error.



                  In the second case, the compiler is asked to change the value of a constant. This is illegal and results in a compiler error.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 2 days ago









                  John Murray

                  591414




                  591414






















                      Mudit Sharma is a new contributor. Be nice, and check out our Code of Conduct.










                       

                      draft saved


                      draft discarded


















                      Mudit Sharma is a new contributor. Be nice, and check out our Code of Conduct.













                      Mudit Sharma is a new contributor. Be nice, and check out our Code of Conduct.












                      Mudit Sharma is a new contributor. Be nice, and check out our Code of Conduct.















                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53239287%2fwhy-do-i-get-a-compiler-error-when-applying-the-increment-operator-to-a-constan%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest




















































































                      Popular posts from this blog

                      Xamarin.iOS Cant Deploy on Iphone

                      Glorious Revolution

                      Dulmage-Mendelsohn matrix decomposition in Python