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

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

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

1 month ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

2 months ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

2 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

5 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

10 months ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

10 months ago