Behaviour of getchar() in a while loop











up vote
2
down vote

favorite












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 at 16:12















up vote
2
down vote

favorite












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 at 16:12













up vote
2
down vote

favorite









up vote
2
down vote

favorite











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 at 16:13









chux

78.8k869145




78.8k869145










asked Nov 9 at 15:21









theo_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 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 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 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 at 16:12












2 Answers
2






active

oldest

votes

















up vote
5
down vote



accepted










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 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 at 17:30


















up vote
0
down vote













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',
    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








    up vote
    5
    down vote



    accepted










    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 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 at 17:30















    up vote
    5
    down vote



    accepted










    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 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 at 17:30













    up vote
    5
    down vote



    accepted







    up vote
    5
    down vote



    accepted






    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 at 15:25









    ODYN-Kon

    2,0531425




    2,0531425












    • Thanks. A bit stupid, I was kind off blind for the obvious I guess.
      – theo_vvv
      Nov 9 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 at 17:30


















    • Thanks. A bit stupid, I was kind off blind for the obvious I guess.
      – theo_vvv
      Nov 9 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 at 17:30
















    Thanks. A bit stupid, I was kind off blind for the obvious I guess.
    – theo_vvv
    Nov 9 at 15:29




    Thanks. A bit stupid, I was kind off blind for the obvious I guess.
    – theo_vvv
    Nov 9 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 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 at 17:30












    up vote
    0
    down vote













    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

























      up vote
      0
      down vote













      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























        up vote
        0
        down vote










        up vote
        0
        down vote









        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 at 9:41









        Luis Colorado

        3,9991717




        3,9991717






























             

            draft saved


            draft discarded



















































             


            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

            Schultheiß

            Verwaltungsgliederung Dänemarks

            Liste der Kulturdenkmale in Wilsdruff