TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C gets() and puts()

C gets() and puts() functions with programming examples for beginners and professionals covering concepts, control statements. Let's see a simple program to read and write string using gets() and puts() functions.

<< Back to C

C gets() and puts() functions

The gets() and puts() are declared in the header file stdio.h. Both the functions are involved in the input/output operations of the strings.

C gets() function

The gets() function enables the user to enter some characters followed by the enter key. All the characters entered by the user get stored in a character array. The null character is added to the array to make it a string. The gets() allows the user to enter the space-separated strings. It returns the string entered by the user.

Declaration

char[] gets(char[]);

Reading string using gets()

#include
void main ()
{
	char s[30];
	printf("Enter the string? ");
	gets(s);
	printf("You entered %s",s);
}

Output

Enter the string? 
TheDeveloperBlog is the best
You entered TheDeveloperBlog is the best

The gets() function is risky to use since it doesn't perform any array bound checking and keep reading the characters until the new line (enter) is encountered. It suffers from buffer overflow, which can be avoided by using fgets(). The fgets() makes sure that not more than the maximum limit of characters are read. Consider the following example.

#include
void main() 
{ 
   char str[20]; 
   printf("Enter the string? ");
   fgets(str, 20, stdin); 
   printf("%s", str); 
} 

Output

Enter the string? TheDeveloperBlog is the best website
TheDeveloperBlog is the b

C puts() function

The puts() function is very much similar to printf() function. The puts() function is used to print the string on the console which is previously read by using gets() or scanf() function. The puts() function returns an integer value representing the number of characters being printed on the console. Since, it prints an additional newline character with the string, which moves the cursor to the new line on the console, the integer value returned by puts() will always be equal to the number of characters present in the string plus 1.

Declaration

int puts(char[])

Let's see an example to read a string using gets() and print it on the console using puts().

#include
#include   
int main(){  
char name[50];  
printf("Enter your name: ");  
gets(name); //reads string from user  
printf("Your name is: ");  
puts(name);  //displays string  
return 0;  
}  

Output:

Enter your name: Sonoo Jaiswal
Your name is: Sonoo Jaiswal
Next TopicC String Functions


Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf