Press Enter to Continue in C

39,711

Solution 1

printf("Press enter to continue\n");
char enter = 0;
while (enter != '\r' && enter != '\n') { enter = getchar(); }
printf("Thank you for pressing enter\n");

Solution 2

printf("Press Enter to Continue");
while( getchar() != '\n' );

A check for '\r' is nice for ultimate portability, but really only matters if you are targeting Mac OS v9 or older (OS-X, Unix & Windows all use either '\n' or, for windows, '\r\n')

Share:
39,711

Related videos on Youtube

Smashery
Author by

Smashery

Software Engineer. Coder for Trosnoth, a fun multiplayer game written in Python. #SOreadytohelp

Updated on September 11, 2020

Comments

  • Smashery
    Smashery over 2 years

    How can you do a "Press Enter to Continue" in C?

  • pmg
    pmg over 13 years
    enter should be an int. Being a char there's no way to differentiate between EOF or a real character.

Related