Check The Programming Section
In C Prgramming, various ways we can take an input of multi word string using
scanf() with a terminating character '\n'
gets(), not preferred. Because it can take input a string more than the size of the buffer
fgets(), we can specify the size of buffer as a fixed number. So it never cross the boundary of the buffer and it is considered as safer than gets.
#include <stdio.h>
int main()
{
char info[5];
printf("Enter any line::");
scanf("%[^\n]s",info);
printf("%s",info);
return 0;
}
In this case, scanf() will read all the characters till it encounters a new line character. The problem with this way of taking input a multi line string can cross the size of the buffer. In the above program, the size of the info is 5. So, if we given an input have more than 5 characters then scanf() can overlapp some other memory of RAM. So this way of taking input is not suggested and sometimes we can get an error as buffer overflow.
As Learn Programming is given as input and number of character is more than the size of the array and printf() will prints the same inputted line and no new line character is appened at the end of the string.
#include <stdio.h>
int main()
{
char info[5];
printf("Enter any line::");
gets(info);
printf("%s",info);
return 0;
}
This approch of taking input a multi word string using gets() not prefereable. Because, like scanf() in the first approach, gets() can read more number of characters than the size of the buffer and sometimes can raise memory segmentation error. In the output screen it also prints a new line character.
#include <stdio.h>
#include <string.h>
int main(){
char info[20];
printf("Enter any line::");
fgets(info,20,stdin);
info[strlen(info)-1]='\0';
printf("%s",info);
return 0;
}
The format of the fgets() function is fgets(char *, size, file_stream) and the argumets are as follows:
char * - pointer to an character or name of the character array
size - it will specify the number of characters that we want to read. The value of the size can be equal or more than the value of the array size. So, it is suggested that, we never pass the value of size more than the size of the array.
file_stream - here we can use stdin as file stream to read the characters from standard input buffer till the encountered of a new line character
In the above program the source line info[strlen(info)-1]='\0' is optional. As fgets() append a new line charecter at the end of the string, so if you did not want that new line character you can use this line of code else ignore.
#include <stdio.h>
#include <string.h>
int main()
{
char info[5];
char *ptr=info;
printf("Enter any line::");
fgets(ptr,20,stdin);
info[strlen(info)-1]='\0';
printf("%s",info);
return 0;
}
In the above program ptr is declared as character pointer, pointing to the base address of the info character array. So, we can also pass a character pointer as the first argument of the fgets() to take input.
#include <stdio.h>
#include <stdlib.h>
int main()
{
char *ptr=(char *)malloc(20);
printf("Enter any line::");
fgets(ptr,20,stdin);
printf("%s",ptr);
return 0;
}