' \r ' vs ' \n ' in C
- by MCP
I'm writing a function that basically waits for the user to hit "enter" and then does something. What I've found that works when testing is the below:
#include <stdio.h>
int main()
{
int x = getc(stdin);
if (x == '\n') {
printf("carriage return");
printf("\n");
}
else {
printf("missed it");
printf("\n");
}
}
The question I have, and what I tried at first was to do: "if (x == '\r')" but in testing, the program didn't catch me hitting enter. The '\n' seems to correspond to me hitting enter from the console. Can someone explain the difference? Also, to verify, writing it as "if... == "\n"" would mean the character string literal? Ie the user would literally have to enter "\n" from the console, correct? Thanks!