Exploring the Most Memorable Characters in C: A Definitive Guide

A character denotes any alphabet, digit or special symbol used to represent numbers, and special characters allowed in C.

Handling Characters in C

  • getchar(): This function receives a character from the keyboard. After entering the character, we should press “Enter” button. The characters being entered will be displayed on the screen. This function is defined in <stdio.h>
  • getche(): This function receives a character from the keyboard. After entering the character, “Enter” button need not be pressed. The character being entered will be displayed on the screen. This is defined in <conio.h>.
  • getch(): This function receives a character from the keyboard. After typing the character, we should not press “Enter” button, and the character will not be shown on the screen. This is defined in <conio.h>.
  • scanf(): This function receives a character from the keyboard when used with “%c” format string. After typing the character, we should press “Enter” button and the character we entered will be displayed on the screen. This function is defined in <stdio.h>.
  • putchar(): This function displays a character on the screen. This function is defined in <stdio.h>.
  • printf(): This function also displays the character on the screen when used with “%c” format string. This function is available in <stdio.h>.

Program to display character using printf() and scanf() function.

#include<stdio.h>
void main()
{
  char ch;
  printf("\nEnter a character:");
  scanf("%c", &ch);
  printf("\n You entered: %c", ch);
}

Output:

Enter a character: A <<press Enter>>
You entered: A

When a character is typed on the keyboard, it releases an integer number internally which is called ASCII(American (American code for information Interchange). For example, if we press ‘A’ on keyboard, it will release the ASCII code 65, Similarly, when ‘a’ is pressed, it will release ASCII code 97.ASCII codes from 65 to 90 represent characters from A to Z.ASCII codes from 97 to 122 represent characters from a to z.ASCII codes from 48 to 57 represent characters from 0 to 9.

Character testing functions:

The header file <ctype.h> contains several functions which are useful to knowing the type of a character. This header file also contains functions to convert a character into uppercase or lowercase.

functionTests whether the characterExample
isalpha()is alphabetic character or notA to Z, a to z
isalnum()os alphanumeric or notA to Z, a to z, 0 to 9
islower()is a lowercase letter or nota to z
isupper()is a uppercase letter or notA to Z
isdigit()is a numeric digit or not0 to 9
isxdigit()is a hexadecimal digit or not0 to 9, A to F, a to f
ispunct()is a punctuation or not. , ‘ ” ; : etc
isspace()is a space character or notSpacebar, Tab, Enter key
iscntrl()is a control or notctrl-A, ctrl-B, del
isascii()is a ASCII character or notcharacters with ASCII codes from 0 to 127
tolower()to convert a character into uppercaseA into a
toupper()to convert a character into lowercasea into A
Table: Functions to test with type of a character.

Suppose, we want to know a given character ‘ch’ is a capital letter or not, we can use isupper() function as:

    if(isupper(ch))
      printf("\n It is uppercase letter");

In above code, in the condition, we are calling isupper() function and passing ‘ch’ to it. isupper() function tests ‘ch’ and returns a non zero value(true) if ‘ch’ represents a capital letter, otherwise it returns zero. So, if the condition becomes true, then the printf() is executed and “it is upper case letter” will be diplayed.

Naveed Tawargeri
 

Hi, I'm Naveed Tawargeri, and I'm the owner and creator of this blog. I'm a Software Developer with a passion for Programming.