8 C Data input/output functions in best way

C Data input/output functions

Data input and output functions Character I/O format

getch()
getche()
getc()
getchar()
gets()
putchar()
putc()
puts().

getch()

getch() is available in “conio.h” header in C programming. This header is mostly used by MS-DOS compilers to provide console input/output. “conio.h” is not part of the C standard library or ISO C, also it is not defined by POSIX. getch() is a nonstandard function.

getch() reads a single character from a keyboard and returns it immediately. Buffer is not used by getch() hence it returns the character even before the “enter”.

It will hold the output screen console until any key pressed.
The Entered character will not be displayed on the console screen.
There is no buffer to store an input character.
It can be used to accept hidden inputs like password, PIN etc.

Syntax :
int getch(void)

It accepts nothing in parameters.
It will return the ASCII value of the key which has been pressed.

#include<stdio.h>
#include<conio.h>

int main()
{
printf("%c", getch());
return 0;
}
Input : v
Output: Output screen will get terminated immediately. If you use DOS shell in C then it will display a signal 'v'

getche()

Like getch(), getche() is also a nonstandard function. This is also available in conio.h header.
It also accepts a character from the keyboard but unlike getch() it will display the character on screen. There is no buffer in getche().

Syntax :
int getche(void);

#include <stdio.h> 
#include <conio.h>

int main()
{
printf("%c", getche());
return 0;
}
Input : v
Output: Output screen will get terminated immediately. If you use DOS shell in C then it will display a signal 'v'

getc()

It reads a single character from an input stream and returns the ASCII value of the key which has been pressed. It returns EOF on failure or when it encounters an error.
Once the character is read from the stream it will move forward the position indicator in the stream. It can read from any input stream like file or console.

Syntax:
Int getc(FILE *stream)

stream is the pointer to a FILE object.

#include <stdio.h> 
int main()
{
printf("%c", getc(stdin));
return(0);
}
Input : v
Output: v

getchar()

getchar() reads a single character from standard input (stdin); it is equal to getc(stdin).
It returns the character ASCII value of the key which has been pressed or EOF on failure or when it encounters an error.

Syntax :
int getchar(void);

#include <stdio.h> 
int main()
{
printf("%c", getchar());
return(0);
}
Input : v
Output: v

gets()

It reads a string from standard input (stdin) and stores it in a character array (string variable). It has a pointer as parameter which points to the character array in which the data string will be stored.

It will return str once it completes reading without any error. It will stop when it encounters the end of line or next line character.

Syntax :
char *gets(char *str);

str will point to a char array.

#include <stdio.h>

int main () {
char name[20];
printf("Enter a your name : ");
gets(name);
printf("Name : %s", name);
return(0);
}

Output:  Enter your name : ITVoyagers

Name : ITVoyagers

putchar()

As the name suggests it is used to write a single character on standard output (stdout). The character which is to be displayed is passed as a parameter in putchar().

Syntax :
int putchar(int char);

It will return an unsigned character on stdout. It will return EOF when error occurs.

#include <stdio.h>

int main () {
char al;
for(al = 'a' ; al <= 'z' ; al++) {
putchar(al);
}
return(0);
}

Output: abcdefghijklmnopqrstuvwxyz

putc()

It is used to write a single character in file, it will write a character in stream and it will move forward the indicator’s position.
It is a C library function.

It returns the character written as an unsigned char cast to an int or EOF when it encounters an error.

Syntax :
int putc( int char, FILE * stream );

char : it is a character which is to be written.
stream : it is a file pointer which states in which file character is to be written.

#include<stdio.h>

int main () {
FILE *itvfile;
int ch;
printf("Enter your name : ");
fp = fopen("itvfile.txt", "w");
for( ch = 0 ; ch <= 10; ch++ ) {
putc(ch, itvfile);
}
fclose(itvfile);

return(0);
}

Output: Enter your name : ITVoyagers

“ITVoyagers” will get store in itvfile.txt

puts()

It is used to display/write a string to a standard output (stdout). It does not include NULL characters.
It will append new line character after each and every output.

Non-negative value is returned on success and on error, it returns EOF.

Syntax :
int puts(const char *str)

#include <stdio.h>
#include <string.h>

int main () {
char str[] = "Welcome to world of IT";
puts("ITVoyagers");
puts(str);

return(0);
}

Output: ITVoyagers

Welcome to world of IT

CHECKOUT OTHER RELATED TOPICS

CHECKOUT OTHER QUIZZES

PRACTICALS IN C PROGRAM

Programs to understand the basic data types and I/O.
Programs on Operators and Expressions.
Programs on decision statements.
Programs on looping.
Programs on arrays.
Programs on functions.
Programs on structures and unions.
Programs on pointers.
Programs on string manipulations.
Practice Programs
1 – C program for leap year, easy and simple code
2 – Best C program to find sum of all digits
3 – Best C program to check Palindrome Number
4 – Best C program to check prime number
5 – Best C program to print Fibonacci series
6 – Best C program to reverse a number
7 – Best C program to reverse an array
8 – Best C program to find average of array
9 – Best C program to find frequency of array element
10 – Best C program to sort array in ascending order
11 – Best C program to find factorial using recursion
12 – Best C program to find sum of all numbers in array
13 – Best C program to find largest element in array
14 – Best C program to swap two numbers without third variable
15 – C Program to check if a number is even or odd
16 – C program to find Simple interest
17 – Best C program to add two matrices
18 – Best C program to count vowels and consonants in a string
19 – Best C program to find LCM
20 – Best C program to find HCF
21 – Best C program to swap two numbers using pointers
22 – Best C program to add two numbers using pointers
23 – Best C program to print Multiplication Table
24 – Best C program to check Armstrong number
25 – Best C program to print half pyramid of star(*)
26 – Best C program to print half pyramid of numbers
27 – Best C program to print reverse half pyramid of star
28 – Best C program to print reverse half pyramid of number
More coming soon…
We are aiming to explain all concepts of C in easiest terms as possible.
ITVoyagers-logo
ITVoyagers
Author

2 thoughts on “8 C Data input/output functions in best way”

Leave a Comment