You are on page 1of 19

01

02

C Language
LECTURE 6
04
Today’s Agenda

01 Using the Function getch()

02 Using Escape Sequences

03
Types of Escape Sequence
Using the Function getch()

 What is getch() ?

1. getch() is a predefined function available in the header file conio.h

2. Whenever we call getch() in our program, the compiler does 2 things:

a. It pause the execution of the code.


b. It opens and halts the console window
Using Escape Sequences

 What is Escape Sequences ?

An escape sequence is a sequence of characters that does not represent


itself when used inside a character or string literal

But is translated into another character or a sequence of characters that may


be difficult or impossible to represent directly.
Using Escape Sequences

 Guess the Output :  Output :

#include <stdio.h> Hello UserWelcome To C


#include <conio.h>
void main()
{ If you want to print
clrscr(); Welcome to C in Next row
then you have to use "\n" in
printf(“Hello User”);
the printf()
printf(“Welcome To C”); function
getch();
}
Using Escape Sequences

 For Example 1:  Output :

#include <stdio.h> Hello User


#include <conio.h> Welcome To C
void main()
{
clrscr();
printf(“Hello User\n”);
printf(“Welcome To C”);
getch();
}
Using Escape Sequences

 For Example 2:  Output :

#include <stdio.h> Hello User


#include <conio.h> Welcome To C
void main()
{
clrscr();
printf(“Hello User\nWelcome To C”);
getch();
}
Types of Escape Sequence

 Types of Escape Sequence

1. \n :

a. Called as “new line character”.

b. Shifts the cursor at the beginning of next line


Types of Escape Sequence

 Types of Escape Sequence

2. \t :

a. Called as “tab character”.

b. Shift the cursor next TAB COLUMN


Using Escape Sequences

 For Example:  Output :

#include <stdio.h> Hello User Welcome To C


#include <conio.h>
void main()
{
clrscr();
printf(“Hello User\t”);
printf(“Welcome To C”);
getch();
}
Types of Escape Sequence

 Types of Escape Sequence

3. \r :

a. Called as “carriage return character”.

b. Shift the cursor at the beginning of current row.


Using Escape Sequences

 For Example:  Output :

#include <stdio.h> Welcome To C


#include <conio.h>
void main()
{
clrscr();
printf(“Hello User\r”);
printf(“Welcome To C”);
getch();
}
Types of Escape Sequence

 Types of Escape Sequence

4. \b :

a. Called as “Backspace character”.

b. Pulls back the cursor one character backwards


Using Escape Sequences

 For Example:  Output :

#include <stdio.h> HellUser


#include <conio.h>
void main()
{
clrscr();
printf(“Hello\bUser”);
getch();
}
Types of Escape Sequences

 Types of Escape Sequence

5. \a : Called as “alarm character”.  Output :

#include <stdio.h> HelloUser


#include <conio.h>
void main()
{
clrscr();
printf(“Hello\bUser”);
getch();
}
Types of Escape Sequences

1. This program will generate a beep sound after displaying Hello and
before displaying User.

2. The duration of this sound will be for 1 sec, if we want to increase this
duration we must use \a twice or thrice.
Types of Escape Sequences

3. Normally we use \a to generate alarm to the user if something


important is happening in the code.

For Example :

User has given wrong password and we want to alert the user about it.
Types of Escape Sequences

4. In modern operating system, \a does not work

so as an alternate to it we use a function called sound() available in the


header file called dos.h
End of Lecture 6
For any queries mail us @: scalive4u@gmail.com
Call us @ : 0755-4271659, 7879165533

Thank you

You might also like