Write a C Program to take a string as input from the user. Basically, we use four methods to take input from the user.
1. Using scanf with %s format specifier
2. Using scanf with %c format specifier
3. Using gets function
4. Using fgets function
scanf()
and %s
format specifier#include <stdio.h>
void main()
{
char str[50];
printf("Enter the string: ");
scanf("%s", str);
printf("%s\n", str);
}
scanf()
and %c
format specifier#include <stdio.h>
void main()
{
char str[50];
printf("Enter the string: ");
scanf("%[^\n]c", str);
printf("%s", str);
}
gets()
function#include <stdio.h>
void main()
{
char str[50];
printf("Enter the string: ");
scanf("%[^\n]c", str);
printf("%s", str);
}
fgets()
function#include <stdio.h>
void main()
{
char str[30];
printf("Enter the string: ");
fgets(str, sizeof(str), stdin);
printf("%s", str);
}
Output
Enter the string: procoding procoding