input output gets(),puts() in C programming Language.

c.gets() function in C:
gets() is a pre-defined function in C Library which is used to read a string or a text line. This function reads input until it encounters newline or End Of File(EOF) function and store the input in a defined string variable. The gets() function does not stop reading input until it encounter newline character.
in simple words we can say that the gets() function stops its reading a soon as newline character is encountered. gets() function reads whitespace as string and does not stop when any whitespace is encountered.
Syntax:gets( variable name );

The c program below illustrates the use of the gets() function

#include<stdio.h>
int main()
{
char name[10];
printf("Enter Your Favourite Programming: ");
gets(name);
printf("\n your favourite programming is %s",name);
return(0);
}
Output:
Enter Your Favourite Programming:
C Language
your favourite programming is C Programming
Program Explanation:
In the program given above we declared name variable of character type which can store 10 character i.e char name[10].
after that printf() function shows the message -->Enter Your Favourite Programming:
at this point compiler waits for the name to be input from the user or stdin.
next we entered the name C language . at this point the gets(name) reads the value entered and store it in name variable. Here the gets() function accepts white spaces also and contine reading character till EOF encountered.
Next at the end printf() shows the output->
your favourite programming is C Programming


d. puts() function in C
puts() function is the standard library function defined in stdio.h header file. This function in c programming is used to write a line or string to standard output stream(stdout) or console.
puts() function does not include null character. It prints the passed string along with a newline i.e.’\n’ and returns an integer value. The return value of this function depends on success of the program.

Syntax: int puts(const char* str);
Here in the given syntax , str is the constant string that is to be printed.
Parameters:
• str − This is the constant string is to be written on stdout.
Return Value:
On successful execution the function returns non-negative value. On error or unsuccessful execution, the function returns EOF

The c program below illustrates the use of the gets() function

// c program illustrate working of puts()
#include <stdio.h>
int main()
{
//string declaration and initialization
char name[] = "ANSI C";
puts(name); //displays the string to stdout
return 0;
}

Output:
ANSI C

e. getch() in C:
getch() function in C programming language is very usefil to get or read a character from the user. getch stands for 'get character'. This function also used to hold the output screen unles user press the key on the keyboard.
While this is not a part of the C standard, this is still a POSIX C function.This function takes in a single character from the standard input (stdin) keyboard, and returns an integer.
. Using getch() function, we can hide the input character provided by the users.
Syntax: int getch(void);
Parameters: The getch() function has void parameter means this function does not accept any parameter from the end user or standard input.

program to display the character entered by the user using the getch() function in C.

#include <stdio.h>
#include <conio.h>
int main()
{
printf(" You have entered the value is: %c", getch()); // print a character entered by the user
return 0;
}
Output:
You have entered the value is:: C

Previous Topic:-->> Simple Assignment Statement || Next topic:-->> Simple C Programs