The puts() and gets() Function

The puts() and gets() Function

You’ve already seen the puts() library function in some of the programs earlier in this book. The puts() function puts a string on-screen—hence its name. A pointer to the string to be displayed is the only argument puts() takes. Because a literal string evaluates as a pointer to a string, puts() can be used to display literal strings as well as string variables. The puts() function automatically inserts a new line character at the end of each string it displays, so each subsequent string displayed with puts() is on its own line.

puts and gets function in c AndroWep-Tutorials
puts and gets function in c AndroWep-Tutorials
/* Demonstrates displaying strings with puts(). */ 
#include <stdio.h> 
   char *message1 = “C”; 
   char *message2 = “is the”; 
   char *message3 = “best”; 
   char *message4 = “programming”; 
   char *message5 = “language!!”; 
int main( void ) 
{ 
    puts(message1); 
    puts(message2); 
    puts(message3); 
    puts(message4); 
    puts(message5); 
    return 0; 
 }

Output

C 
is the 
best 
programming 
language!!

Code Example

#include<stdio.h>
#include<conio.h>
int main()
{
	char data[100];
	printf("Enter a String for gets() :");

	gets(data);

	printf("Entered Data Is : will be with puts() :");

	puts(data);// Wait For Output Screen

    getch();//Main Function return Statement

    return 0;
}

The gets() Function

The gets() function gets a string, str, from the standard input device, usually the keyboard. The string consists of any characters entered until a newline character is read. At that point, a null is appended to the end of the string.
Then the gets() function returns a pointer to the string just read. If there is a problem getting the string, gets() returns null.

Code Example

/* gets() example */ 
#include <stdio.h> 
    char line[256]; 
    void main( void ) { 
       printf( “Enter a string:\n”); 
       gets( line );
       printf( “\nYou entered the following string:\n” ); 
       printf( “%s\n”, line ); 
} 

Why puts is used in C?

puts() function is a file handling function in C programming language which is used to write a line to the output screen. Please find below the description and syntax for above file handling function. puts() function is used to write a line to the output screen. In a C program, we use puts function as below.

Why do we use gets in C?

GETS in C Programming. The C gets function is used to scan or read a line of text from a standard input (stdin) device, and store it in the String variable. When it reads the newline character then the get function will be terminated.