Quick links
Latest articles
Internship
Members
New...
Algorithms
Discrete Mathematics
Big data
Languages
C
C++
C++ STL
Java
Data Structure
C#.Net
Android
Kotlin
SQL
Web
PHP
Python
JavaScript
CSS
Ajax
Node.js
Web prog.
Programs
C
C++
DS
Java
C#
Python
Aptitude
C
C++
Java
DBMS
Interview
C
Embedded C
Java
SEO
HR
CS Subjects
CS Basics
O.S.
Networks
DBMS
Embedded Systems
Cloud Computing
Machine learning
CS Organizations
Linux
DOS
More...
Articles
Puzzles
News/Updates

Home » C programs

How to identify ENTER KEY is pressed in C programming language?



C program to read enter key, How to identify ENTER KEY is pressed in C programming language? Here, we will learn how we can identify whether an ENTER KEY is pressed or not in C programming language? In this post, we will learn about the different methods to identify it.
Submitted by IncludeHelp, on May 05, 2018

Here, we will learn how to get and identify that ENTER KEY is pressed?

We will read character by character in an infinite loop and print the input characters until ENTER KEY is not pressed.

Within the infinite loop there will be a condition to check whether input character is ENTER KEY or not, if ENTER KEY is pressed, loop will be terminated otherwise input character will be printed and ask for Input any character again?

Condition to check ENTER KEY

There are basically two methods to check input key is ENTER KEY of not

  • By checking ASCII Code
  • By checking new line character '\n'

By checking ASCII Code

The ASCII Code of ENTER KEY is 10 in Decimal or 0x0A in Hexadecimal.

Let suppose input character is storing in variable ch, then condition will be as below

    if(ch==0x0A)
    {
	    //your code....
    }
    if(ch==10)
    {
	    //your code....
    }

By checking New line character '\n'

We can also check that Input character is new line or not by comparing input character is equal to '\n' or not.

    if(ch=='\n')
    {
	    //your code....
    }

Let’s consider the following program

#include <stdio.h>

int main()
{
	char ch;
	//infinite loop
	while(1)
	{
		printf("Enter any character: ");
		//read a single character
		ch=fgetc(stdin);
		
		if(ch==0x0A)
		{
			printf("ENTER KEY is pressed.\n");
			break;
		}
		else
		{
			printf("%c is pressed.\n",ch);
		}
		//read dummy character to clear
		//input buffer, which inserts after character input
		ch=getchar();
	}
	return 0;
}

Output

Enter any character: A
A is pressed. 
Enter any character: B
B is pressed. 
Enter any character: 9
9 is pressed. 
Enter any character:
ENTER KEY is pressed.





Quick links:
C FAQ(s) C Advance programs C/C++ Tips & Tricks Puzzles JavaScript CSS Python Linux Commands PHP Android Articles More...

Featured post:
Introduction to Linux (Its modes, Safety, Most popular Applications)
Linux Best Distribution Software (Distros) of 2018

Was this page helpful? Please share with your friends...

Are you a blogger? Join our Blogging forum.

Comments and Discussions



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com (2015-2018), Some rights reserved.