Reputation: 63
I have tried this :
switch(c)
case 13 : {printf("enter pressed");break;}
and this :
switch(c)
case '\n' : {printf("enter pressed");break;}
but It didn't work out
Upvotes: 0
Views: 47225
Reputation: 193
Try to use '\r'
instead.
The 'Enter' key represent Carriage Return that is the same as '\r'
.
Upvotes: 4
Reputation: 22089
This program reads from standard input and writes "enter pressed" whenever a newline occurs in the input:
#include <stdio.h>
int main()
{
int c;
for (;;) {
c = getc(stdin);
switch (c) {
case '\n':
printf("enter pressed\n");
break;
case EOF:
return 0;
}
}
}
I think this is what you are looking for.
You might be missing the trailing \n
in your printf
-call, causing the message to be buffered for output but maybe not flushed so it appears on the screen.
Upvotes: 0
Reputation: 883
"Enter" represents a new line character in C language. So you can use its ascii value i.e. 10 for its representation. Eg :
#include<stdio.h>
Try this code :
int main()
{
char ch = '\n';
printf("ch = %d\n", ch);
}
Later you can use the following code as a test for switching '/n'
int main()
{
char ch = '\n';
switch(ch)
{
case '\n' :
printf("Enter pressed\n");
break;
default :
//code
}
}
Upvotes: 0