Codeforcoding

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
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
Exit mobile version