+ 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
13 Réponses
+ 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.
+ 6
//Try this code
#include <stdio.h>
int main() {
char c[35];
scanf("%s",c);
printf("%s",c);
return 0;
}
+ 4
getchar reads one character.
Char variable contains one character.
%c format specifier - for one character
+ 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?
+ 2
Read string with fgets(or scanf). Put it in char array, use %s format specifier
+ 2
getchar is used for one character
+ 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...
+ 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.
+ 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;
}
0
what should i do to get a word as a output?
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
0
Use printf("You entered: %s",a); to overcome.
0
#include <stdio.h>
int main() {
char a[10];
printf("enter a string ");
scanf("%s",a);
printf("%s",a);
return 0;
}