How to input or read a Character, Word and a Sentence from user in C?
C is a procedural programming language. It was initially developed by Dennis Ritchie as a system programming language to write an operating system. The main features of the C language include low-level access to memory, a simple set of keywords, and a clean style, these features make C language suitable for system programmings like operating systems or compiler development. This article focuses on how to take a character, a string, and a sentence as input in C.
Reading a Character in C
Problem Statement#1: Write a C program to read a single character as input in C.
Syntax-
scanf("%c", &charVariable);
Approach-
- scanf() needs to know the memory location of a variable in order to store the input from the user.
- So, the ampersand will be used in front of the variable (here ch) to know the address of a variable.
- Here using %c format specifier, the compiler can understand that character type of data is in a variable when taking input using the scanf() function
C
#include <stdio.h>
int main()
{
char ch;
scanf ( "%c" , &ch);
printf ( "Output : %c" , ch);
return 0;
}
|
Reading a Word in C
Problem Statement#2: Write a C program to read words as input from the user.
Syntax-
scanf("%s", stringvariable);
Approach-
- First, initialize the char array of size ( greater than are equal to the length of word).
- Then, use %s format specifier to take the string using the scanf() function.
C
#include <stdio.h>
int main()
{
char word[100];
scanf ( "%s" , word);
printf ( "Output : %s" , word);
return 0;
}
|
Note:
An array name itself indicates its address. word == &word[0], these are both the same.It’s because the variable name word points to the first element of the array. So, there is no need to mention ampersand in scanf().
Reading a Sentence in C
Problem Statement#3: Write a C program to read sentences as input from the user.
Method 1-
- scanf() doesn’t store the white space character in a string variable.
- It only reads characters other than white spaces and stores them in the specified character array until it encounters a white-space character.
Syntax-
scanf("%[^\n]s", sen)
C
#include <stdio.h>
int main()
{
char sen[100];
scanf ( "%[^\n]s" , sen);
printf ( "Output : %s" , sen);
return 0;
}
|
scanf(“%[^\n]s”, sen) means to read a string including spaces until the next line is received or to read string until line break i.e. \n is encountered and store it on an array named “sen”.
- Here, %[ ] is the scanset specifier.
- scanf will process only those characters which are part of scanset.
- If the first character of the scanset is ‘^’, then the specifier will stop reading after the first occurrence of that character.
- ^\n stands for taking input until a newline isn’t encountered.
C
#include <stdio.h>
int main()
{
char sen[100];
scanf ( "%[^f]s" , sen);
printf ( "Output : %s" , sen);
return 0;
}
|
It’ll stop reading after the first occurrence of that character f (specified in the scanset).
Method 2- Using fgets
Note- gets() never checks the maximum limit of input characters. Hence they may cause undefined behavior and probably lead to buffer overflow error which eventually causes the program to crash. Hence, it is advisable not to use the gets function to read strings. To overcome the above limitation, fgets can be used.
Syntax-
char *fgets(char *str, int size, FILE *stream)
C
#include <stdio.h>
#define BUFFSIZE 25
int main()
{
char sen[BUFFSIZE];
fgets (sen, BUFFSIZE, stdin);
printf ( "Output : %s" , sen);
return 0;
}
|
Last Updated :
21 Sep, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...