getch returns 2 characters when I type one





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







4















When I use getch, it always appends the caracter read with a null character.



When I use the following code:



#include "stdafx.h"
#include <conio.h>

int main()
{
char c = 0;

while (c != 'x')
{
c = _getch();
printf("Char read: <%c> n", c);
}
}


It returns the follwing on the console, when I press the keys "asdx":



Char read: <a>
Char read: < >
Char read: <s>
Char read: < >
Char read: <d>
Char read: < >
Char read: <x>


This is compiled in VS 2017 in a plain new single file project, running on a windows 10 console window. I tried removing the _UNICODE and UNICODE define.










share|improve this question























  • How do you know it's a null character? BTW _getch() returns int.

    – n.m.
    Aug 10 '18 at 18:48








  • 2





    works as expected for me, have you tried printing the numerical value of c to see what it is?

    – Alan Birtles
    Aug 10 '18 at 18:48











  • I know it's a null because I traced it into the debugger. I tried with either int or char, I got the same result.

    – Vincent Hubert
    Aug 10 '18 at 18:52






  • 1





    This is apparently a problem in some versions of Windows SDK, see developercommunity.visualstudio.com/content/problem/247770/…

    – n.m.
    Aug 10 '18 at 18:55








  • 1





    @AlanBirtles: it works for me in Debug mode but fails in Release mode. Same behavior as OP.

    – Thomas Weller
    Aug 10 '18 at 18:55


















4















When I use getch, it always appends the caracter read with a null character.



When I use the following code:



#include "stdafx.h"
#include <conio.h>

int main()
{
char c = 0;

while (c != 'x')
{
c = _getch();
printf("Char read: <%c> n", c);
}
}


It returns the follwing on the console, when I press the keys "asdx":



Char read: <a>
Char read: < >
Char read: <s>
Char read: < >
Char read: <d>
Char read: < >
Char read: <x>


This is compiled in VS 2017 in a plain new single file project, running on a windows 10 console window. I tried removing the _UNICODE and UNICODE define.










share|improve this question























  • How do you know it's a null character? BTW _getch() returns int.

    – n.m.
    Aug 10 '18 at 18:48








  • 2





    works as expected for me, have you tried printing the numerical value of c to see what it is?

    – Alan Birtles
    Aug 10 '18 at 18:48











  • I know it's a null because I traced it into the debugger. I tried with either int or char, I got the same result.

    – Vincent Hubert
    Aug 10 '18 at 18:52






  • 1





    This is apparently a problem in some versions of Windows SDK, see developercommunity.visualstudio.com/content/problem/247770/…

    – n.m.
    Aug 10 '18 at 18:55








  • 1





    @AlanBirtles: it works for me in Debug mode but fails in Release mode. Same behavior as OP.

    – Thomas Weller
    Aug 10 '18 at 18:55














4












4








4








When I use getch, it always appends the caracter read with a null character.



When I use the following code:



#include "stdafx.h"
#include <conio.h>

int main()
{
char c = 0;

while (c != 'x')
{
c = _getch();
printf("Char read: <%c> n", c);
}
}


It returns the follwing on the console, when I press the keys "asdx":



Char read: <a>
Char read: < >
Char read: <s>
Char read: < >
Char read: <d>
Char read: < >
Char read: <x>


This is compiled in VS 2017 in a plain new single file project, running on a windows 10 console window. I tried removing the _UNICODE and UNICODE define.










share|improve this question














When I use getch, it always appends the caracter read with a null character.



When I use the following code:



#include "stdafx.h"
#include <conio.h>

int main()
{
char c = 0;

while (c != 'x')
{
c = _getch();
printf("Char read: <%c> n", c);
}
}


It returns the follwing on the console, when I press the keys "asdx":



Char read: <a>
Char read: < >
Char read: <s>
Char read: < >
Char read: <d>
Char read: < >
Char read: <x>


This is compiled in VS 2017 in a plain new single file project, running on a windows 10 console window. I tried removing the _UNICODE and UNICODE define.







c++ visual-studio-2017 console-application






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Aug 10 '18 at 18:40









Vincent HubertVincent Hubert

1,161819




1,161819













  • How do you know it's a null character? BTW _getch() returns int.

    – n.m.
    Aug 10 '18 at 18:48








  • 2





    works as expected for me, have you tried printing the numerical value of c to see what it is?

    – Alan Birtles
    Aug 10 '18 at 18:48











  • I know it's a null because I traced it into the debugger. I tried with either int or char, I got the same result.

    – Vincent Hubert
    Aug 10 '18 at 18:52






  • 1





    This is apparently a problem in some versions of Windows SDK, see developercommunity.visualstudio.com/content/problem/247770/…

    – n.m.
    Aug 10 '18 at 18:55








  • 1





    @AlanBirtles: it works for me in Debug mode but fails in Release mode. Same behavior as OP.

    – Thomas Weller
    Aug 10 '18 at 18:55



















  • How do you know it's a null character? BTW _getch() returns int.

    – n.m.
    Aug 10 '18 at 18:48








  • 2





    works as expected for me, have you tried printing the numerical value of c to see what it is?

    – Alan Birtles
    Aug 10 '18 at 18:48











  • I know it's a null because I traced it into the debugger. I tried with either int or char, I got the same result.

    – Vincent Hubert
    Aug 10 '18 at 18:52






  • 1





    This is apparently a problem in some versions of Windows SDK, see developercommunity.visualstudio.com/content/problem/247770/…

    – n.m.
    Aug 10 '18 at 18:55








  • 1





    @AlanBirtles: it works for me in Debug mode but fails in Release mode. Same behavior as OP.

    – Thomas Weller
    Aug 10 '18 at 18:55

















How do you know it's a null character? BTW _getch() returns int.

– n.m.
Aug 10 '18 at 18:48







How do you know it's a null character? BTW _getch() returns int.

– n.m.
Aug 10 '18 at 18:48






2




2





works as expected for me, have you tried printing the numerical value of c to see what it is?

– Alan Birtles
Aug 10 '18 at 18:48





works as expected for me, have you tried printing the numerical value of c to see what it is?

– Alan Birtles
Aug 10 '18 at 18:48













I know it's a null because I traced it into the debugger. I tried with either int or char, I got the same result.

– Vincent Hubert
Aug 10 '18 at 18:52





I know it's a null because I traced it into the debugger. I tried with either int or char, I got the same result.

– Vincent Hubert
Aug 10 '18 at 18:52




1




1





This is apparently a problem in some versions of Windows SDK, see developercommunity.visualstudio.com/content/problem/247770/…

– n.m.
Aug 10 '18 at 18:55







This is apparently a problem in some versions of Windows SDK, see developercommunity.visualstudio.com/content/problem/247770/…

– n.m.
Aug 10 '18 at 18:55






1




1





@AlanBirtles: it works for me in Debug mode but fails in Release mode. Same behavior as OP.

– Thomas Weller
Aug 10 '18 at 18:55





@AlanBirtles: it works for me in Debug mode but fails in Release mode. Same behavior as OP.

– Thomas Weller
Aug 10 '18 at 18:55












2 Answers
2






active

oldest

votes


















4














Well, crap!



It's a (rather new) bug in windows.



https://developercommunity.visualstudio.com/content/problem/247770/-getch-broken-in-vs-157.html




"When building a console application using the _getch() function
suddenly returns two times for each keypress"




got the following response from microsoft:




" Thanks for reporting this! This will be fixed on a future windows
update."




UPDATE:
As stated in the link above, setting the runtime to statically link with a previous version of the C runtime will fix the issue, but you need to make sure all your related projects (if you are building a library, for example) also use the same runtime. (I tested it)






share|improve this answer

































    2














    If you don't want to (or you can't) use a previous version of the Universal C Runtime, you can use _getwch instead of _getch, as the problem doesn't affect to _getwch function.



    Note that _getwch can return Unicode characters, so it may return distinct values than _getch for some keys. For example, for character, _getch returns 0x3f (?) while _getwch returns 0x20ac (Unicode value for €)






    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%2f51792349%2fgetch-returns-2-characters-when-i-type-one%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









      4














      Well, crap!



      It's a (rather new) bug in windows.



      https://developercommunity.visualstudio.com/content/problem/247770/-getch-broken-in-vs-157.html




      "When building a console application using the _getch() function
      suddenly returns two times for each keypress"




      got the following response from microsoft:




      " Thanks for reporting this! This will be fixed on a future windows
      update."




      UPDATE:
      As stated in the link above, setting the runtime to statically link with a previous version of the C runtime will fix the issue, but you need to make sure all your related projects (if you are building a library, for example) also use the same runtime. (I tested it)






      share|improve this answer






























        4














        Well, crap!



        It's a (rather new) bug in windows.



        https://developercommunity.visualstudio.com/content/problem/247770/-getch-broken-in-vs-157.html




        "When building a console application using the _getch() function
        suddenly returns two times for each keypress"




        got the following response from microsoft:




        " Thanks for reporting this! This will be fixed on a future windows
        update."




        UPDATE:
        As stated in the link above, setting the runtime to statically link with a previous version of the C runtime will fix the issue, but you need to make sure all your related projects (if you are building a library, for example) also use the same runtime. (I tested it)






        share|improve this answer




























          4












          4








          4







          Well, crap!



          It's a (rather new) bug in windows.



          https://developercommunity.visualstudio.com/content/problem/247770/-getch-broken-in-vs-157.html




          "When building a console application using the _getch() function
          suddenly returns two times for each keypress"




          got the following response from microsoft:




          " Thanks for reporting this! This will be fixed on a future windows
          update."




          UPDATE:
          As stated in the link above, setting the runtime to statically link with a previous version of the C runtime will fix the issue, but you need to make sure all your related projects (if you are building a library, for example) also use the same runtime. (I tested it)






          share|improve this answer















          Well, crap!



          It's a (rather new) bug in windows.



          https://developercommunity.visualstudio.com/content/problem/247770/-getch-broken-in-vs-157.html




          "When building a console application using the _getch() function
          suddenly returns two times for each keypress"




          got the following response from microsoft:




          " Thanks for reporting this! This will be fixed on a future windows
          update."




          UPDATE:
          As stated in the link above, setting the runtime to statically link with a previous version of the C runtime will fix the issue, but you need to make sure all your related projects (if you are building a library, for example) also use the same runtime. (I tested it)







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Aug 10 '18 at 19:29

























          answered Aug 10 '18 at 18:56









          Vincent HubertVincent Hubert

          1,161819




          1,161819

























              2














              If you don't want to (or you can't) use a previous version of the Universal C Runtime, you can use _getwch instead of _getch, as the problem doesn't affect to _getwch function.



              Note that _getwch can return Unicode characters, so it may return distinct values than _getch for some keys. For example, for character, _getch returns 0x3f (?) while _getwch returns 0x20ac (Unicode value for €)






              share|improve this answer




























                2














                If you don't want to (or you can't) use a previous version of the Universal C Runtime, you can use _getwch instead of _getch, as the problem doesn't affect to _getwch function.



                Note that _getwch can return Unicode characters, so it may return distinct values than _getch for some keys. For example, for character, _getch returns 0x3f (?) while _getwch returns 0x20ac (Unicode value for €)






                share|improve this answer


























                  2












                  2








                  2







                  If you don't want to (or you can't) use a previous version of the Universal C Runtime, you can use _getwch instead of _getch, as the problem doesn't affect to _getwch function.



                  Note that _getwch can return Unicode characters, so it may return distinct values than _getch for some keys. For example, for character, _getch returns 0x3f (?) while _getwch returns 0x20ac (Unicode value for €)






                  share|improve this answer













                  If you don't want to (or you can't) use a previous version of the Universal C Runtime, you can use _getwch instead of _getch, as the problem doesn't affect to _getwch function.



                  Note that _getwch can return Unicode characters, so it may return distinct values than _getch for some keys. For example, for character, _getch returns 0x3f (?) while _getwch returns 0x20ac (Unicode value for €)







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Oct 30 '18 at 12:53









                  gusitoguaygusitoguay

                  1106




                  1106






























                      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%2f51792349%2fgetch-returns-2-characters-when-i-type-one%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

                      Xamarin.iOS Cant Deploy on Iphone

                      Glorious Revolution

                      Dulmage-Mendelsohn matrix decomposition in Python