ankitweblogic logo
W e b t u t o r i a l s

C Tutorial

Programing Approach

Algorithm

Flow Chart

Define C

Define Programming

C - Syntax

Compiler vs Interpreter

C - Variables

C - Keywords

C - Data Types

C - Comments

Turbo C - Shortcut keys

C - Operators

Hierarchy of Operators

Ex: Arithmetic Operator

C - Formatting Output

Constant Variable

C - Escape Sequence

C - if , switch

Ex: If condition

Ex: Inc / Dec Operator

C - loops

Ex: Loops

C - Nesting Of loops

Ex: Nested Loops

Jumping Statements

C - Arrays 1D

C - Arrays 2D

C - Sorting

ASCII Value

Character I/O Function

C - String Functions

Ex: Strings

Array of Strings

C - Math Functions

User-defined Function

Exercise Function

C - Types of Functions

Storage Class

C - Header Files

C - Preprocessor

C - Pointers

C - Structures

C - Typedef

C - File Handling

Ex: File Handling

Command Line Argument

MCQ

C - Character

Exercise 1: Program to declare and display character data type:

void main()
{
   char c, d;
   clrscr();
   c='A';
   printf("%c\n", c);
   d=65;
   printf("%c\n", d);
   getch();
}
A
A

Character Input functions

Character input functions is used to input a single character. All these functions are available in 'stdio.h' header file.

1. getch(): Use to input single character at a time. But it will not display input character. get stands for input, ch stands for character.
Syntax: char a = getch();

2. getche(): It is same as getch() function but it will display input character. get stands for input, ch stands for character, e stands for echo(display)
Syntax: char a = getche();
Both getch() and getche() functions will not required enter key to terminate.

3. getchar(): It will terminate by enter key after inputting a single character.
Syntax: char a = getchar();

Exercise 2: Input Character and display it.

#include<stdio.h>
#include<conio.h>
void main()
{
    char a;
    clrscr();
    printf("Enter character");
    a = getch();
    //a = getche();
    //a = getchar();
    
    printf("\nYou have pressed: %c",a);
    //putch(a);
    //putchar(a);
    getch();
}

Example 3: Program to explain difference between getch() and getche() function.

void main()
{
   char ch;
   clrscr();	
   printf("Enter a character: ");
   ch=getch();
   printf("\nch=%c",ch);
   printf("\nEnter a character: ");
   ch=getche();
   printf("\nch=%c",ch);
   getch();
}
Enter a character:
ch=a
Enter a character: a
ch=a

Character output functions

1. putch(): It will print a single character
Syntax: putch(a);

2. putchar(): print single character
Syntax: putchar(a);

Question on C - Character

  1. Print ASCII Value of input character.
  2. #include<stdio.h>
    #include<conio.h>
    
    void main()
    {
        char ch;
        clrscr();
        printf("Enter character");
        ch = getche();
        printf("\nASCII Value of %c id %d",ch, ch);
        getch();
    }
  3. Input character and find it is in uppercase, or in lowercase or a digit.
  4. #include<stdio.h>
    #include<conio.h>
    
    void main()
    {
        char ch;
        clrscr();
        while(1)
        {
            printf("\nEnter character (press 0 - exit)");
            ch = getche();
            if(ch=='0')
            {
               exit(1);
            }
            else
            if(ch>=65 && ch<=90)
            {
               printf("Capital letter");
            }
            else
            if(ch>=97 && ch<=122)
            {
               printf("Small letter");
            }
            else
            if(ch>=48 && ch<=57)
            {
               printf("Digit");
            }
            else
            {
               printf("Special character");
            }
            getch();
        }
    }
  5. Input character in lowercase and convert into uppercase.
  6. #include<stdio.h>
    #include<conio.h>
    
    void main()
    {
        char ch;
        clrscr();
        printf("Enter Character in Lowercase: ");
        ch = getche();
        ch = ch-32;
        printf("\nLetter in Uppercase is %c",ch);
        getch();
    }
  7. Input character in uppercase and convert into lowercase.
  8. Print all characters with its ASCII value from 0 to 255. Solution
  9. Find sum of 2 numbers and repeat the program until user exit. Solution