0% found this document useful (0 votes)
541 views3 pages

C Gets and Puts Functions Updated

The gets() and puts() functions in C are used for input and output of strings. Gets() reads a string from the user including spaces into a character array until enter is pressed, while puts() prints the given string to the console and returns the number of characters printed. Both functions are declared in stdio.h. Gets() is risky due to the lack of bounds checking on the input while fgets() allows specifying the maximum number of characters to avoid buffer overflows.

Uploaded by

hamsdao King
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
541 views3 pages

C Gets and Puts Functions Updated

The gets() and puts() functions in C are used for input and output of strings. Gets() reads a string from the user including spaces into a character array until enter is pressed, while puts() prints the given string to the console and returns the number of characters printed. Both functions are declared in stdio.h. Gets() is risky due to the lack of bounds checking on the input while fgets() allows specifying the maximum number of characters to avoid buffer overflows.

Uploaded by

hamsdao King
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

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

1. char[] gets(char[]);

Reading string using gets()

1. #include<stdio.h>
2. void main ()
3. {
4. char s[30];
5. printf("Enter the string? ");
6. gets(s);
7. printf("You entered %s",s);
8. }

Output
Enter the string?
javatpoint is the best
You entered javatpoint 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.
Prime Ministers of India | List of Prime Minister of India (1947-2020)
1. #include<stdio.h>
2. void main()
3. {
4. char str[20];
5. printf("Enter the string? ");
6. fgets(str, 20, stdin);
7. printf("%s", str);
8. }

Output
Enter the string? javatpoint is the best website
javatpoint 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

1. int puts(char[])

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

1. #include<stdio.h>
2. #include <string.h>
3. int main(){
4. char name[50];
5. printf("Enter your name: ");
6. gets(name); //reads string from user
7. printf("Your name is: ");
8. puts(name); //displays string
9. return 0;
10. }

Output:
Enter your name: Sonoo Jaiswal
Your name is: Sonoo Jaiswal

Next Topic C String Functions

← PrevNext →

You might also like