Basic

C program gets() and puts() function

C program gets() and puts() function

In this tutorial, we will discuss a simple concept of the C program gets() and put() function

C program gets() and puts() 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.

 

gets() function

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

 

Puts() function

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

 

Hello world program in C programming language
input output function in the C language
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

3 weeks ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

1 month ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

1 month ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

1 month ago

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

2 months ago

Java program to check odd or even using recursion

Java program to check odd or even using recursion In this tutorial, we discuss a…

2 months ago