How to input or read a Character, Word and a Sentence from user in C?
Last Updated :
21 Sep, 2021
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
// C program to implement
// the above approach
#include <stdio.h>
// Driver code
int main()
{
char ch;
// Read a char type variable,
// store in "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
// C Program to implement
// the above approach
#include <stdio.h>
// Driver code
int main()
{
char word[100];
// word is treated as a pointer
// to the first element of array
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
// C program to implement
// the above approach
#include <stdio.h>
// Driver code
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
// C program to implement
// the above approach
#include <stdio.h>
// Driver code
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
// C program to implement
// the above approach
#include <stdio.h>
#define BUFFSIZE 25
// Driver code
int main()
{
char sen[BUFFSIZE];
fgets(sen, BUFFSIZE, stdin);
printf("Output : %s", sen);
return 0;
}
Similar Reads
How to store words in an array in C? We all know how to store a word or String, how to store characters in an array, etc. This article will help you understand how to store words in an array in C. To store the words, a 2-D char array is required. In this 2-D array, each row will contain a word each. Hence the rows will denote the index
2 min read
How to read or input a string? In this article, we are going to learn how to print or output a string using different languages. Strings are considered a data type in general and are typically represented as arrays of bytes (or words) that store a sequence of characters. Strings are defined as an array of characters. Topics: How
3 min read
Read/Write Structure From/to a File in C For writing in the file, it is easy to write string or int to file using fprintf and putc, but you might have faced difficulty when writing contents of the struct. fwrite and fread make tasks easier when you want to write and read blocks of data.Writing Structure to a File using fwriteWe can use fwr
3 min read
How to Create Your Own scanf() in C? The scanf() function is used to take input from the console. It is defined in <stdio.h> header file and takes const char* and variable argument list as parameters. In this article, we will learn how to implement your own custom scanf() function in C language. For this, we need to have a firm g
3 min read
How to write long strings in Multi-lines C/C++? Image a situation where we want to use or print a long long string in C or C++, how to do this? In C/C++, we can break a string at any point in the middle using two double quotes in the middle. Below is a simple example to demonstrate the same. C #include<stdio.h> int main() { // We can put tw
2 min read
Write a C program that displays contents of a given file like 'more' utility in Linux Write a C program that displays contents of given line page by page. Given number of lines to show as 'n' at a time and a file name, the program should first show n lines, then wait for user to hit a key before showing next n lines and so on. We strongly recommend to minimize the browser and try thi
2 min read
Taking String input with space in C (4 Different Methods) We can take string input in C using scanf("%s", str). But, it accepts string only until it finds the first space. There are 4 methods by which the C program accepts a string with space in the form of user input.Let us have a character array (string) named str[]. So, we have declared a variable as ch
2 min read
C program to detect tokens in a C program As it is known that Lexical Analysis is the first phase of compiler also known as scanner. It converts the input program into a sequence of Tokens. A C program consists of various tokens and a token is either a keyword, an identifier, a constant, a string literal, or a symbol.For Example: 1) Keyword
5 min read
Inbuilt library functions for user Input | sscanf, scanf_s, fscanf_s, sscanf_s The C Programming Language provides various Inbuilt Library Functions for User Input. In this article, we will learn about sscanf, scanf_s, fscanf_s, sscanf_s Library Functions in C. 1. sscanf() Function in C sscanf() is used to read formatted input from the string. Both scanf() and sscanf() functio
6 min read