In this tutorial, we will discuss a simple concept of the C program gets() and put() function
Both the functions are used to in the input and output operation of the Strings
The gets() functions are used to read string input from the keyboard and puts() function displays it.
These functions are declared in the stdio.h header file.
The gets() function is similar to scanf() function but it allows entering some characters by the user in string format with the double quotation. and stored in a character array format.
Declaration
Syntax of gets() function
char[] gets(char[]);
Program 1
let’s see an example to read a string using gets() function and print it on the screen using printf() function
#include <stdio.h> #include <stdlib.h> int main() { char ch[30]; printf("Enter the string: "); gets(ch); /reading string using gets() printf("you entered string here\n"); printf(ch);//display out put on the screen using printf() function return 0; }
When the above code is executed, it produces the following results
Enter the string: code4coding you entered string here code4coding
The puts() function is similar to printf() function. but puts() function is used to display only the string after reading by gets() function entered by user(gets similar to scanf() function)
Declaration
Syntax of puts() function
int puts(char[])
Program 2
let’s see an example to read a string using gets() function and print it on the screen using puts() function
#include <stdio.h> #include <string.h> int main() { char sentence[50]; printf("Enter your sentence\n"); gets(sentence);//read input from entered by the user printf("your sentance here\n\n"); puts(sentence); //display the sentence getch(); return 0; }
When the above code is executed, it produces the following results
Enter your sentence Code4coding is a programming portal your sentence here Code4coding is a programming portal
Suggested for you
Function in C language
Scanf() printf() function in C language
gets() and puts() function in C
User defined function in C Language
Function in C Language with example
Arithmetic function in C Language
C# inverted full pyramid star pattern In this article, we will discuss the concept of…
C# Full Pyramid star pattern program In this article, we will discuss the concept of…
Program to count vowels, consonants, words, characters and space in Java In this article, we…
How to print multiplication table using Array in C++ language In this post, we will…
C Program to multiplication table using Array In this tutorial , we will discuss about…
Java program to check odd or even using recursion In this tutorial, we discuss a…