c character handling functions


Character Handling Functions
getch().
getche().
putchar().
I. getch() Function:
It gets a character from the console.
It does not wait until user presses enter key.
The pressed character is not Echoed.
Returns the pressed character.
Syntax:
int getch(void)
Program
#include<stdio.h>
void main()
{
char ch;
ch = getch();
printf("%c", ch);
}

II. getche() Function:
It gets a character from the console.
It will not wait until user presses enter key
The pressed character is Echoed.
Returns the pressed character.
It uses the direct Video or BIOS.
Syntax:
int getche(void)
Program
#include<stdio.h>
void main()
{
char ch;
ch = getche();
printf("%c", ch);
}
III. putchar ():
It place sthe character into the screen
Program
#include<stdio.h>
void main()
{
char ch;
ch = getchar();
putchar(ch);
}


IV. scanf() Function:
scanf() is a function which scans data of various types.
scanf() returns number of variables scanned.
It Expects a delimiter for termination.
It can be Nested with other Functions.
Example : printf(“%d”, scanf(“%d%d”, &x, &y));
Program
#include<stdio.h>
void main()
{
int x, y, z;
printf(“Please Enter the Values for x and y : “);
z = scanf(“%d%d”, &x, &y);
printf(“\nThe Value of x = %d and y = %d.\n” , x, y);
printf(“The Number of Variables Scanned are %d.”, z);
}
Some Experiment to Conduct:
Program
#include<stdio.h>
void main()
{
int x, y;
printf(“Please Enter the Values for x and y : “);
printf(“The Number of Variables Scanned are %d.”, scanf(“%d%d”, &x, &y));
}
Case 1:
Enter the two numbers one after the other by pressing the enter key each
time.
Example:
20 (press enter key)
50 (press enter key)
The Values are Scanned By scanf() Function and Returns The Number of
Scanned Values as 2 to the printf() Function.
Case 2:
Enter one number and press enter key and then press ctrl+z (^Z) and press
enter key.
Example:
20 (press enter key)
^Z (press enter key)
The Values are Scanned By scanf() Function and Returns The Number of
Scanned Values as 1 to the printf() Function.
Case 3:
Enter the ctrl+z and press enter key.
Example:
^z (press enter key)
The Value is Scanned By scanf() Function and Returns The Number of
Scanned Values as -1 to the printf() Function.
V. printf() Function: printf() function is a function which not only prints the
messages and data onto the screen, but also returns number of bytes
printed on to the console.
Example: printf(“%d”, printf(‘Hello”));
Innermost printf() prints Hello as output.
Outer Most printf() Prints the length of Hello as 5 bytes.
NOTE
Whenever a function ‘call’ exists in the condition part of the control structure
the value returned by the function is compared for analysis.

No comments: