Behaviour of getchar() in a while loop












2















I am running this c program in the terminal



#include <stdio.h>

int main() {
int result = 0;

while(result <= 0)
{
int result = (getchar() != EOF);
result = 2;
printf("x");
}

printf("outn");
}


After that I type in the word "hello" followed by a return. The result is that I get multiple 'x' characters.



Why doesn't this terminate after the first 'x'?










share|improve this question




















  • 1





    Tip: a well enabled compiler may report "warning: variable 'result' set but not used [-Wunused-but-set-variable]". Save time and enable all compiler warnings.

    – chux
    Nov 9 '18 at 16:12
















2















I am running this c program in the terminal



#include <stdio.h>

int main() {
int result = 0;

while(result <= 0)
{
int result = (getchar() != EOF);
result = 2;
printf("x");
}

printf("outn");
}


After that I type in the word "hello" followed by a return. The result is that I get multiple 'x' characters.



Why doesn't this terminate after the first 'x'?










share|improve this question




















  • 1





    Tip: a well enabled compiler may report "warning: variable 'result' set but not used [-Wunused-but-set-variable]". Save time and enable all compiler warnings.

    – chux
    Nov 9 '18 at 16:12














2












2








2








I am running this c program in the terminal



#include <stdio.h>

int main() {
int result = 0;

while(result <= 0)
{
int result = (getchar() != EOF);
result = 2;
printf("x");
}

printf("outn");
}


After that I type in the word "hello" followed by a return. The result is that I get multiple 'x' characters.



Why doesn't this terminate after the first 'x'?










share|improve this question
















I am running this c program in the terminal



#include <stdio.h>

int main() {
int result = 0;

while(result <= 0)
{
int result = (getchar() != EOF);
result = 2;
printf("x");
}

printf("outn");
}


After that I type in the word "hello" followed by a return. The result is that I get multiple 'x' characters.



Why doesn't this terminate after the first 'x'?







c getchar






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 9 '18 at 16:13









chux

83.6k872152




83.6k872152










asked Nov 9 '18 at 15:21









theo_vvvtheo_vvv

233




233








  • 1





    Tip: a well enabled compiler may report "warning: variable 'result' set but not used [-Wunused-but-set-variable]". Save time and enable all compiler warnings.

    – chux
    Nov 9 '18 at 16:12














  • 1





    Tip: a well enabled compiler may report "warning: variable 'result' set but not used [-Wunused-but-set-variable]". Save time and enable all compiler warnings.

    – chux
    Nov 9 '18 at 16:12








1




1





Tip: a well enabled compiler may report "warning: variable 'result' set but not used [-Wunused-but-set-variable]". Save time and enable all compiler warnings.

– chux
Nov 9 '18 at 16:12





Tip: a well enabled compiler may report "warning: variable 'result' set but not used [-Wunused-but-set-variable]". Save time and enable all compiler warnings.

– chux
Nov 9 '18 at 16:12












2 Answers
2






active

oldest

votes


















5














You're re-declaring (shadowing result) inside the while loop. The result that is used in while(result <= 0) is the one that is declared outside the loop.






share|improve this answer
























  • Thanks. A bit stupid, I was kind off blind for the obvious I guess.

    – theo_vvv
    Nov 9 '18 at 15:29






  • 1





    so to address the wording of the question title directly - the mystery had nothing to do with getchar()

    – Kevin Olree
    Nov 9 '18 at 17:30



















0














Well,



#include <stdio.h>

int main() {
int result = 0; /* here *OUTER* result gets the value 0 */

while(result <= 0) /* THIS MAKES THE While to execute forever */
{
int result = (getchar() != EOF); /* THIS VARIABLE IS ***NOT*** THE outside result variable */
result = 2; /* external block result is not visible here so this assign goes to the above inner result */
printf("x");
/* INNER result CEASES TO EXIST HERE */
}

printf("outn");
}


As you can deduct from the comments, the result variable that is compared in the while test is the outer one, while the inner one hides the outer one, no assignations can be made to it in the body of the loop, so the loop runs forever. You get an infinite string of xs printed on stdout.






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%2f53228536%2fbehaviour-of-getchar-in-a-while-loop%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    5














    You're re-declaring (shadowing result) inside the while loop. The result that is used in while(result <= 0) is the one that is declared outside the loop.






    share|improve this answer
























    • Thanks. A bit stupid, I was kind off blind for the obvious I guess.

      – theo_vvv
      Nov 9 '18 at 15:29






    • 1





      so to address the wording of the question title directly - the mystery had nothing to do with getchar()

      – Kevin Olree
      Nov 9 '18 at 17:30
















    5














    You're re-declaring (shadowing result) inside the while loop. The result that is used in while(result <= 0) is the one that is declared outside the loop.






    share|improve this answer
























    • Thanks. A bit stupid, I was kind off blind for the obvious I guess.

      – theo_vvv
      Nov 9 '18 at 15:29






    • 1





      so to address the wording of the question title directly - the mystery had nothing to do with getchar()

      – Kevin Olree
      Nov 9 '18 at 17:30














    5












    5








    5







    You're re-declaring (shadowing result) inside the while loop. The result that is used in while(result <= 0) is the one that is declared outside the loop.






    share|improve this answer













    You're re-declaring (shadowing result) inside the while loop. The result that is used in while(result <= 0) is the one that is declared outside the loop.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 9 '18 at 15:25









    ODYN-KonODYN-Kon

    2,4752825




    2,4752825













    • Thanks. A bit stupid, I was kind off blind for the obvious I guess.

      – theo_vvv
      Nov 9 '18 at 15:29






    • 1





      so to address the wording of the question title directly - the mystery had nothing to do with getchar()

      – Kevin Olree
      Nov 9 '18 at 17:30



















    • Thanks. A bit stupid, I was kind off blind for the obvious I guess.

      – theo_vvv
      Nov 9 '18 at 15:29






    • 1





      so to address the wording of the question title directly - the mystery had nothing to do with getchar()

      – Kevin Olree
      Nov 9 '18 at 17:30

















    Thanks. A bit stupid, I was kind off blind for the obvious I guess.

    – theo_vvv
    Nov 9 '18 at 15:29





    Thanks. A bit stupid, I was kind off blind for the obvious I guess.

    – theo_vvv
    Nov 9 '18 at 15:29




    1




    1





    so to address the wording of the question title directly - the mystery had nothing to do with getchar()

    – Kevin Olree
    Nov 9 '18 at 17:30





    so to address the wording of the question title directly - the mystery had nothing to do with getchar()

    – Kevin Olree
    Nov 9 '18 at 17:30













    0














    Well,



    #include <stdio.h>

    int main() {
    int result = 0; /* here *OUTER* result gets the value 0 */

    while(result <= 0) /* THIS MAKES THE While to execute forever */
    {
    int result = (getchar() != EOF); /* THIS VARIABLE IS ***NOT*** THE outside result variable */
    result = 2; /* external block result is not visible here so this assign goes to the above inner result */
    printf("x");
    /* INNER result CEASES TO EXIST HERE */
    }

    printf("outn");
    }


    As you can deduct from the comments, the result variable that is compared in the while test is the outer one, while the inner one hides the outer one, no assignations can be made to it in the body of the loop, so the loop runs forever. You get an infinite string of xs printed on stdout.






    share|improve this answer




























      0














      Well,



      #include <stdio.h>

      int main() {
      int result = 0; /* here *OUTER* result gets the value 0 */

      while(result <= 0) /* THIS MAKES THE While to execute forever */
      {
      int result = (getchar() != EOF); /* THIS VARIABLE IS ***NOT*** THE outside result variable */
      result = 2; /* external block result is not visible here so this assign goes to the above inner result */
      printf("x");
      /* INNER result CEASES TO EXIST HERE */
      }

      printf("outn");
      }


      As you can deduct from the comments, the result variable that is compared in the while test is the outer one, while the inner one hides the outer one, no assignations can be made to it in the body of the loop, so the loop runs forever. You get an infinite string of xs printed on stdout.






      share|improve this answer


























        0












        0








        0







        Well,



        #include <stdio.h>

        int main() {
        int result = 0; /* here *OUTER* result gets the value 0 */

        while(result <= 0) /* THIS MAKES THE While to execute forever */
        {
        int result = (getchar() != EOF); /* THIS VARIABLE IS ***NOT*** THE outside result variable */
        result = 2; /* external block result is not visible here so this assign goes to the above inner result */
        printf("x");
        /* INNER result CEASES TO EXIST HERE */
        }

        printf("outn");
        }


        As you can deduct from the comments, the result variable that is compared in the while test is the outer one, while the inner one hides the outer one, no assignations can be made to it in the body of the loop, so the loop runs forever. You get an infinite string of xs printed on stdout.






        share|improve this answer













        Well,



        #include <stdio.h>

        int main() {
        int result = 0; /* here *OUTER* result gets the value 0 */

        while(result <= 0) /* THIS MAKES THE While to execute forever */
        {
        int result = (getchar() != EOF); /* THIS VARIABLE IS ***NOT*** THE outside result variable */
        result = 2; /* external block result is not visible here so this assign goes to the above inner result */
        printf("x");
        /* INNER result CEASES TO EXIST HERE */
        }

        printf("outn");
        }


        As you can deduct from the comments, the result variable that is compared in the while test is the outer one, while the inner one hides the outer one, no assignations can be made to it in the body of the loop, so the loop runs forever. You get an infinite string of xs printed on stdout.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 15 '18 at 9:41









        Luis ColoradoLuis Colorado

        4,3601718




        4,3601718






























            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%2f53228536%2fbehaviour-of-getchar-in-a-while-loop%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

            Bressuire

            Vorschmack

            Quarantine