bitwise operator and getch,getche,getchar etc

34
BITWISE OPERATOR AND GETCH(), GETCHE(),GETCHAR(),PUTCHAR() PRESENTED BY : HIMANSHU SINGHAL SHIVAM GAUTAM LUCKY GARG

Upload: lalit-kumar

Post on 03-Jan-2016

58 views

Category:

Documents


0 download

DESCRIPTION

presentation on bitwise operator and difference between gethch, getche, getchar etc...

TRANSCRIPT

Page 1: Bitwise Operator and Getch,Getche,Getchar Etc

BITWISE OPERATOR AND GETCH(), GETCHE(),GETCHAR(),PUTCHAR()

PRESENTED BY :HIMANSHU SINGHALSHIVAM GAUTAMLUCKY GARG

Page 2: Bitwise Operator and Getch,Getche,Getchar Etc

The frequently used C library functions for I/O operations are:1.For input: getch, getche,getchar ,gets, scanf, sscanf ;

2. For output: putch, puts, printf, sprintf.

Page 3: Bitwise Operator and Getch,Getche,Getchar Etc

INTRODUCTION

• Most of the program is ending with getch(),and so we think that getch() is used to display the output...but it is wrong. It is used to get a single character from the console.

Page 4: Bitwise Operator and Getch,Getche,Getchar Etc

Getch()

getch reads without echoing a character when a character is input from the keyboard. The key may have an ASCII code or a special function attached. In the first case, the function returns the ASCII code for that character.

Page 5: Bitwise Operator and Getch,Getche,Getchar Etc

getch() is used to get a character from console but does not echo to the screen.

Library:

<CONIO.H>

Declaration:

int getch(void);

Example Declaration:

char ch;

ch = getch();

Remarks:

getch reads a single character directly from the keyboard, without

echoing to the screen.

Return Value:

This function return the character read from the keyboard.

Page 6: Bitwise Operator and Getch,Getche,Getchar Etc

Example Program:

void main()

{

char ch;

ch = getch();

printf("Input Char Is :%c",ch);

}

Program Explanation:

Here declare the  variable ch as char data type, and then get a value through getch()

library function and store it in the variable ch. And then print the value of variable ch.

During the program execution, a single character is get or read through the getch(). The

given value is not displayed on the screen and the compiler does not wait for another

character to be typed. And then, the given character is printed through

the printf function.

Page 7: Bitwise Operator and Getch,Getche,Getchar Etc

Getche()

• getche has similar functionality. The difference is that the read character is echoed. When getch or getche are invoked, the system waits for a key to be pressed.

Page 8: Bitwise Operator and Getch,Getche,Getchar Etc

Function : getche()getche() is used to get a character from console, and echoes to the screen.

Library:<CONIO.H>

Declaration:int getche(void);

Example Declaration:char ch;

ch = getche();

Remarks:getche reads a single character from the keyboard and echoes it to the current

text window, using direct video or BIOS.

Return Value:This function return the character read from the keyboard.

Page 9: Bitwise Operator and Getch,Getche,Getchar Etc

Example Program:void main(){char ch;ch = getche();printf("Input Char Is :%c",ch); }

Program Explanation:Here declare the  variable ch as char data type, and then get a value through getche() library function and store it in the variable ch. And then print the value of variable ch.

During the program execution, a single character is get or read through the getche(). The given value is displayed on the screen and the compiler does not wait for another character to be typed. Then after wards the character is printed through the printf function.

Page 10: Bitwise Operator and Getch,Getche,Getchar Etc

Getchar()

This Function return a character that has been recently typed. After typing the appropriate character, the user is required to press enter key to shift the control to the next statement.

Page 11: Bitwise Operator and Getch,Getche,Getchar Etc

getchar

The getchar macro enables reading with echo of ASCII characters (no special keys allowed). The characters keyed in are placed in a buffer and not converted till the ENTER key is pressed. Upon return, the ASCII code of the first input character is returned. When invoked again the next input character is returned, also. When EOF is encountered, getchar returns the value –1.

Page 12: Bitwise Operator and Getch,Getche,Getchar Etc

Function : getchar()getchar() is used to get or read the input (i.e a single character) at run

time.Library:

<Stdio.h>

Declaration:int getchar(void);

Example Declaration:char ch;

ch = getchar();

Return Value:This function return the character read from the keyboard.

Page 13: Bitwise Operator and Getch,Getche,Getchar Etc

Example Program: void main()

{char ch;ch = getchar();printf("Input Char Is :%c",ch);}

Program Explanation:Here,declare the  variable ch as char data type, and then get a value throughgetchar() library function and store it in the variable ch.And then,print the value of variable ch.

During the program execution, a single character is get or read through thegetchar(). The given value is displayed on the screen and the compiler wait for another character to be typed. If you press the enter key/any other characters and then only the given character is printed through the printffunction.

Page 14: Bitwise Operator and Getch,Getche,Getchar Etc

• getch() after accepting a char terminates the program without displaying the character.• getche() accepts a character, displays it

n then terminates the program.• getchar() reads a character n simply

displays it on the screen while execution.

Page 15: Bitwise Operator and Getch,Getche,Getchar Etc

putchar

The putchar macro prints the character given as an argument.Macros putchar are defined in stdio.h as follows:• int getchar(void);• int putchar (int c);and are invoked in the same way as getch and putch.

Page 16: Bitwise Operator and Getch,Getche,Getchar Etc

Usage example:

/* Example Program 3*/#include <stdio.h>#include <conio.h>int main( ){

putchar(getchar());putchar('\n');getch();return 0;

}

Page 17: Bitwise Operator and Getch,Getche,Getchar Etc

Putch()

Putch sends to the output device (e.g. screen) a character which corresponds to the ASCII code used as its argument. Printable characters have ASCII codes in the range [32,126]. Codes outside this range cause various image patterns to be displayed.

Page 18: Bitwise Operator and Getch,Getche,Getchar Etc

Putch()

Example :Void main (){Char ch ;ch=‘y’;putchar(ch);getch();}

Page 19: Bitwise Operator and Getch,Getche,Getchar Etc

Note :

conio.h is a header file used in old MS-DOS compilers, to create text mode user interfaces, however, it is not partof the C programming language, the C standard library.This header declares several useful library functions for performing "console input and output" from a program. Most C compilers that target DOS, Windows 3.x, Phar Lap, DOSX, OS/2, or Win32 have this header and supply theconcomitant library functions in the default C library.

Page 20: Bitwise Operator and Getch,Getche,Getchar Etc

A usage example is:

/* Example Program 1*/#include <conio.h>int main(){

putch(getch());getch();return 0;

}

Page 21: Bitwise Operator and Getch,Getche,Getchar Etc

gets and puts functions

gets reads with echo from the standard input device an ASCII string, and places this string in thevariable given as its argument. Upon return reading continues with:• Character '\n' (newline), which is replaced by

character '\0' (null). In this case, gets return the start address for the memory area where read characters are stored.

• End of file marker (CTRL/Z). gets returns the value zero.

Page 22: Bitwise Operator and Getch,Getche,Getchar Etc

gets and puts functions

puts displays on the standard output device a character string corresponding to the ASCII codes ofthe characters stored at the address given as its argument. The null character ('\0') is interpreted asnewline ('\n'). This function returns the code of the last displayed character or the value –1 in case thatan error occurred.

Page 23: Bitwise Operator and Getch,Getche,Getchar Etc

gets and puts functions

Prototypes for these functions can be founding the header file stdio.h. They are:• char *gets (char *s);• int puts (const char *s);Note: Here *s denotes a pointer to an ASCII string (e.g. a string variable).

Page 24: Bitwise Operator and Getch,Getche,Getchar Etc

A usage example is:/* Example Program 2 */#include <stdio.h>#include <conio.h>int main(){

char s[200];printf("\nPlease input a chareter string and press ENTER\n");gets(s);printf("\nThe character string is\n");puts(s);getch();return 0;

}

Page 25: Bitwise Operator and Getch,Getche,Getchar Etc

BITWISE OPERATOR

•C provides an extensive bit manipulation operators for programmers who wants to communicate directly wit the hardware.•Bitwise operator works on bits and perform bit by bit operation.

Page 26: Bitwise Operator and Getch,Getche,Getchar Etc

BITWISE OPERATOR SUMMARY

Page 27: Bitwise Operator and Getch,Getche,Getchar Etc

Assume if A = 60; and B = 13; Now in binary format they will be as follows:A = 0011 1100B = 0000 1101-----------------A&B = 0000 1100A|B = 0011 1101A^B = 0011 0001~A = 1100 0011

Operator Description Example

& Binary AND Operator copies a bit to the result if it exists in both operands.

(A & B) will give 12 which is 0000 1100

| Binary OR Operator copies a bit if it exists in eather operand.

(A | B) will give 61 which is 0011 1101

^ Binary XOR Operator copies the bit if it is set in one operand but not both.

(A ^ B) will give 49 which is 0011 0001

~ Binary Ones Complement Operator is unary and has the efect of 'flipping' bits.

(~A ) will give -60 which is 1100 0011

<< Binary Left Shift Operator. The left operands value is moved left by the number of bits specified by the right operand.

A << 2 will give 240 which is 1111 0000

>> Binary Right Shift Operator. The left operands value is moved right by the number of bits specified by the right operand.

A >> 2 will give 15 which is 0000 1111

There are following Bitwise operators supported by C language

Page 28: Bitwise Operator and Getch,Getche,Getchar Etc

BITWISE AND• This makes more sense if we apply this to a specific

operator. In & operator is bitwise AND. The following is a chart that defines &1, defining AND on individual bits.

Page 29: Bitwise Operator and Getch,Getche,Getchar Etc

BITWISE OR• The | operator is bitwise OR (it's a single vertical

bar). The following is a chart that defines |1, defining OR on individual bits

Page 30: Bitwise Operator and Getch,Getche,Getchar Etc

BITWISE XOR• There's only one unary bitwise operator, and that's

bitwise NOT. Bitwise NOT flips all of the bits.• There's not that much to say about it, other than it's

not the same operation as unary minus.• The following is a chart that defines ~1, defining

NOT on an individual bit.

Page 31: Bitwise Operator and Getch,Getche,Getchar Etc

BITWISE XOR• The ^ operator is bitwise XOR. The usual bitwise OR

operator is inclusive OR. XOR is true only if exactly one of the two bits is true.

• The following is a chart that defines ^1, defining XOR on individual bits.

Page 32: Bitwise Operator and Getch,Getche,Getchar Etc

BITWISE LEFT SHIFT

32

UNSINGED a = 0x68ab; ... a <<= 3; /* shift left 3 bits */

Same as: a = a << 3;

Bits positions vacated by shift are filled with zeros

Page 33: Bitwise Operator and Getch,Getche,Getchar Etc

33

Right Shifts (Signed)UNSINGED a = 0x78ab; . . .a >>= 5; /* shift right 5 bits */

Bit positions vacated by shifting is filled with a copy of the highest (sign) bit for signed data type

Page 34: Bitwise Operator and Getch,Getche,Getchar Etc

Thank You