You are on page 1of 34

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

()
PRESENTED BY : HIMANSHU SINGHAL SHIVAM GAUTAM LUCKY GARG

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.

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.

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

getch() is used to get a character from console but does not echo to the screen. Remarks: getch reads a single character directly from the keyboard. . Example Declaration: char ch. ch = getch(). Library: <CONIO. Return Value: This function return the character read from the keyboard.H> Declaration: int getch(void). without echoing to the screen.

The given value is not displayed on the screen and the compiler does not wait for another character to be typed.ch). During the program execution. and then get a value through getch() library function and store it in the variable ch. a single character is get or read through the getch().Example Program: void main() { char ch. } Program Explanation: Here declare the variable ch as char data type. . the given character is printed through the printf function. And then. ch = getch(). printf("Input Char Is :%c". And then print the value of variable ch.

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

H> Declaration: nt getche(void). Example Declaration: har ch. ch = getche(). and echoes to the screen. Remarks: etche reads a single character from the keyboard and echoes it to the current text window.unction : getche() etche() is used to get a character from console. Return Value: his function return the character read from the keyboard. Library: CONIO. . using direct video or BIOS.

and then get a value through getche() library function and store it in the variable ch. a single character is get or read through the getche(). printf("Input Char Is :%c". } Program Explanation: Here declare the variable ch as char data type.xample Program: void main() { char ch. ch = getche(). . And then print the value of variable ch. The given value is displayed on the screen and the compiler does not wait for another character to be typed. During the program execution.ch). Then after wards the character is printed through the printf function.

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

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

.unction : getchar() etchar() is used to get or read the input (i.e a single character) at run time. Example Declaration: har ch. ch = getchar(). eturn Value: his function return the character read from the keyboard. Library: Stdio.h> Declaration: nt getchar(void).

.xample Program: void main() { char ch.print the value of variable ch.} rogram Explanation: Here. and then get a value throughgetchar() library function and store it in the variable ch. printf("Input Char Is :%c". ch = getchar(). If you press the enter key/any other characters and then only the given character is printed through the printffunction.And then. The given value is displayed on the screen and the compiler wait for another character to be typed. a single character is get or read through thegetchar().declare the variable ch as char data type. During the program execution.ch).

displays it n then terminates the program. getche() accepts a character. getchar() reads a character n simply displays it on the screen while execution. .getch() after accepting a char terminates the program without displaying the character.

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

h> #include <conio. } .Usage example: /* Example Program 3*/ #include <stdio. return 0.h> int main( ) { putchar(getchar()). getch(). putchar('\n').

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.g. .Putch() Putch sends to the output device (e.

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

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

return 0. } . getch().h> int main() { putch(getch()).A usage example is: /* Example Program 1*/ #include <conio.

and places this string in the variable given as its argument. . gets return the start address for the memory area where read characters are stored. which is replaced by character '\0' (null). Upon return reading continues with: • Character '\n' (newline). gets returns the value zero. In this case. • End of file marker (CTRL/Z).gets and puts functions gets reads with echo from the standard input device an ASCII string.

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

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

h> #include <conio. puts(s). printf("\nPlease input a chareter string and press ENTER\n"). printf("\nThe character string is\n").h> int main() { char s[200]. } . getch().A usage example is: /* Example Program 2 */ #include <stdio. gets(s). return 0.

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

BITWISE OPERATOR SUMMARY .

Binary OR Operator copies a bit if it exists in eather operand. Binary XOR Operator copies the bit if it is set in one operand but not both. The left operands value is moved right by the number of bits specified by the right operand. Binary Left Shift Operator. A << 2 will give 240 which is 1111 0000 < > Binary Right Shift Operator. The left operands value is moved left by the number of bits specified by the right operand. Example (A & B) will give 12 which is 0000 1100 (A | B) will give 61 which is 0011 1101 (A ^ B) will give 49 which is 0011 0001 Binary Ones Complement Operator is unary (~A ) will give -60 which is 1100 0011 and has the efect of 'flipping' bits.There are following Bitwise operators supported by C language Assume if A = 60. A >> 2 will give 15 which is 0000 1111 . Now in binary format they will be as follows: A = 0011 1100 B = 0000 1101 ----------------A&B = 0000 1100 A|B = 0011 1101 A^B = 0011 0001 ~A = 1100 0011 erator Description Binary AND Operator copies a bit to the result if it exists in both operands. and B = 13.

In & operator is bitwise AND. defining AND on individual bits.BITWISE AND • This makes more sense if we apply this to a specific operator. . The following is a chart that defines &1.

The following is a chart that defines |1.BITWISE OR • The | operator is bitwise OR (it's a single vertical bar). defining OR on individual bits .

.BITWISE XOR • There's only one unary bitwise operator. • The following is a chart that defines ~1. and that's bitwise NOT. other than it's not the same operation as unary minus. • There's not that much to say about it. defining NOT on an individual bit. Bitwise NOT flips all of the bits.

The usual bitwise OR operator is inclusive OR. . defining XOR on individual bits. • The following is a chart that defines ^1.BITWISE XOR • The ^ operator is bitwise XOR. XOR is true only if exactly one of the two bits is true.

Same as: a = a << 3.BITWISE LEFT SHIFT UNSINGED a = 0x68ab. . /* shift left 3 bits */ Bits positions vacated by shift are filled with zeros 32 ... a <<= 3.

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 33 . ...