#include <stdio.h> int main() { char a = getchar(); printf("You entered: %c", a); return 0; } | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

#include <stdio.h> int main() { char a = getchar(); printf("You entered: %c", a); return 0; }

why my output is only one char of my given word

10th Mar 2020, 8:13 PM
sagor zia
sagor zia - avatar
13 Answers
+ 2
You have to declare an array of character type. So you can input and get output the whole sentence instead of declaring a variable of character type.
11th Mar 2020, 10:59 AM
Yash Kumar
Yash Kumar - avatar
+ 6
//Try this code #include <stdio.h> int main() { char c[35]; scanf("%s",c); printf("%s",c); return 0; }
11th Mar 2020, 2:03 AM
Ketul Patel
Ketul Patel - avatar
+ 4
getchar reads one character. Char variable contains one character. %c format specifier - for one character
10th Mar 2020, 8:17 PM
Igor Kostrikin
Igor Kostrikin - avatar
+ 4
Your program basically asks for a single character as an input and prints a single character as output. How do you expect to get multiple characters?
10th Mar 2020, 11:42 PM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar
+ 2
Read string with fgets(or scanf). Put it in char array, use %s format specifier
10th Mar 2020, 8:22 PM
Igor Kostrikin
Igor Kostrikin - avatar
+ 2
getchar is used for one character
12th Mar 2020, 5:42 AM
Ankita
Ankita - avatar
+ 1
To get word as input: Declare charecter array char a[20]; scanf("%s",a); //for taking a word input printf("%s", a); //for output word You can use fgets(a, 20,stdin); for a line words length of maximum 20 Edit : Pls tag language C also...
10th Mar 2020, 8:24 PM
Jayakrishna 🇮🇳
+ 1
There is mainly 2 rules to understand. 1) getchar() function only accept input of only 1 char. 2) One variable only store one value at time. According to your code "Variable Declaration statement " which Declare only one variable and similarly getchar () function accept only one input character. Hence Your Program Output is 9nly one char of given word.
12th Mar 2020, 5:45 PM
Shweta Patil.
Shweta Patil. - avatar
+ 1
Try this #include <stdio.h> int main( ){ char a[100]; printf("input text:\n"); gets(a); printf("you entered %s\n",a); return 0; }
12th Mar 2020, 7:17 PM
Ian
Ian - avatar
0
what should i do to get a word as a output?
10th Mar 2020, 8:19 PM
sagor zia
sagor zia - avatar
0
In C language we have 4 functions that takes single character information. getchar() and getc() Vs. getch() and getch() getchar() and getc() allow the user to input as many characters as he wants and terminate the input only when the [Enter) key is pressed, and then they assign the value of the first key pressed to the variable on the left of the assignment sign getche() and getch() on the other hand automatically terminate the input as soon as the user presses a key and assign the value of the key pressed to the variable on the left of the assignment sign. getchar() Vs. getc() getchar() is designed to take input only from the keyboard, while getcl) can take input from the keyboard as well as file The 'stdin' getc() specifies 'standard input device', i.e, the keyboard. getche() Vs. getch() getche() echoes the value of the key pressed on the VDU, while getch() does not
11th Mar 2020, 4:58 PM
Akbar Ali Quazi
Akbar Ali Quazi - avatar
0
Use printf("You entered: %s",a); to overcome.
11th Mar 2020, 10:08 PM
Prabhanshu Srivastava
Prabhanshu Srivastava - avatar
0
#include <stdio.h> int main() { char a[10]; printf("enter a string "); scanf("%s",a); printf("%s",a); return 0; }
12th Mar 2020, 11:48 AM
Sonam Gupta