
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
fgets() and gets() in C
fgets()
The function fgets() is used to read the string till the new line character. It checks array bound and it is safe too.
Here is the syntax of fgets() in C language,
char *fgets(char *string, int value, FILE *stream)
Here,
string − This is a pointer to the array of char.
value − The number of characters to be read.
stream − This is a pointer to a file object.
Here is an example of fgets() in C language,
Example
#include <stdio.h> #define FUNC 8 int main() { char b[FUNC]; fgets(b, FUNC, stdin); printf("The string is: %s
", b); return 0; }
Output
The input string is “Hello World!” in stdin stream.
The string is: Hello W
In the above program, an array of char type is declared. The function fgets() reads the characters till the given number from STDIN stream.
char b[FUNC]; fgets(b, FUNC, stdin);
gets()
The function gets() is used to read the string from standard input device. It does not check array bound and it is insecure too.
Here is the syntax of gets() in C language,
char *gets(char *string);
Here,
string − This is a pointer to the array of char.
Here is an example of gets() in C language,
Example
#include <stdio.h> #include <string.h> int main() { char s[100]; int i; printf("
Enter a string : "); gets(s); for (i = 0; s[i]!='\0'; i++) { if(s[i] >= 'a' && s[i] <= 'z') { s[i] = s[i] - 32; } } printf("
String in Upper Case = %s", s); return 0; }
Output
Enter a string : hello world! String in Upper Case = HELLO WORLD!
In the above program, the string s of char array is converted into upper case string. The function gets() is used to read the string from the stdin stream.
char s[100]; int i; printf("
Enter a string : "); gets(s);
- Related Articles
- Problem with scanf() when there is fgets()/gets()/scanf() after it in C
- fgets() function in PHP
- fgets() and fread() - What is the difference in PHP?
- Difference between scanf() and gets() in C
- Divide ₹7000 Among A B and C Such That A Gets 50% Of What B Gets And B Gets 50% Of What C Gets
- Gets an ICollection containing the values in the Hashtable in C#
- How and why soil gets acidic?
- Gets or sets the value in HybridDictionary with specified key in C#
- Gets or sets the element at the specified index in StringCollection in C#
- Gets or sets the value at the specified key in StringDictionary in C#
- Gets or Sets the element at the specified index in the List in C#
- Why we can't use arrow operator in gets and puts?
- A student placed a clean iron nail in blue coloured copper sulphate solution for a considerable time. He observes that :iron nail gets green coatingiron nail gets brown coatingiron nail gets no coatingiron nail gets blue coating
- Gets or sets the value of the bit at a specific position in the BitArray in C#
- Great Service gets Great Rewards
