Unformatted Console I/O function

Unformatted Console i/o Function


The unformatted console input and output function can deal with a single character and a string of characters.

Normally we use the scanf() function for input but when we use the scanf() function we need to do one thing that is pressing the enter key to evaluate the success full input. Its weakness of scanf() Function.

Now to read a single character we have a function called getch() and getche(), these functions not need to press the enter key because these functions return the character that most recently typed. The getche() function echoes the characters that you typed to the screen 'e' in getche() is referred for echos. getch() just return the characters that typed without echoing iron screen.

getchar() is read a single character from the standard input stream and return the corresponding integer value ( it typically ASCII value of reading character) on success. It returns EOF (End of file) on failure. It is the same as getche() but requires enter key after the typing of character.

Example

#include<stdio.h>

int main()

{

printf("%c",getch());


printf("\n now the getch is over\n");


printf("%c",getchar());  //need enter key


printf("\n now the getchar is over\n");


printf("%c",getche());


printf("\nNow the getche is over");


return 0;


}

Output:

i

now the getch is over

a

a

now the getchar is over

mm

now the getche is over

Putch()


putch() is a single character output function, it displays any alphanumeric character to the standard output device, putch print only one character at a time.

Syntax

putch(variable name);

putch(int char);

Example

main()

{

char c;


c=getchar();


putch(c);


}

Output:

i

i

Note: you don't need to include stdio.h file to use putch() or putchar() any function we show earlier for character because these are non-standard functions.

Putchar()


It is also a single-character output function at a time. Its display any alphanumeric character s to the standard output device

Syntax

putchar(variable_name);

putchar(int char);

Example:

main()

{

char c=getch();


putchar(c);


putchar('A');


putchar("\n");


putchar(65)  //ASCII value of A


putchar("\n");


putchar('Wow');


}

Output:

a

A
A
W

above program gives warning that multi-character constant but it runs and gives output as the first letter.


gets() and puts()


gets() and puts() are function to receive and print a string especially the gets() is for receiving a string from keyboard and puts() is to print a string on the screen.

Now why we need gets() function we saw the limitations of the scanf() function that it can take input as string as soon as first blank or enter key is not found as the solution of this we can use gets() to input string with blank space and tabs. It terminated when an enter key is pressed by the user. gets() gets a newline(\n) terminated string as input it replace the \n with a \0 (null character)

Puts() function is the same as printf but while we using puts() we can only print the strings However we can use the escape sequences in strings.

Example:

main()

{

char name[20];


puts("Enter your num");


gets(name);


puts("Your name is:");


puts(name);


}

Output:

enter your name:

Kung fu panda

your name is:

kung fu panda