I have written a small script to detect the full value from the user input with the getchar()
function in C. As getchar()
only returns the first character i tried to loop through it... The code I have tried myself is:
#include <stdio.h>
int main()
{
char a = getchar();
int b = strlen(a);
for(i=0; i<b; i++) {
printf("%c", a[i]);
}
return 0;
}
But this code does not give me the full value of the user input.
Point 1: In your code,
a
is not of array type. you cannot use array subscript operator on that.Point 2: In your code,
strlen(a);
is wrong.strlen()
calculates the length of a string, i.e, a null terminatedchar
array. You need to pass a pointer to a string tostrlen()
.Point 3:
getchar()
does not loop for itself. You need to putgetchar()
inside a loop to keep on reading the input.Point 4:
getchar()
retruns anint
. You should change the variable type accordingly.Point 5: The recommended signature of
main()
isint main(void)
.Keeping the above points in mind,we can write a pesudo-code, which will look something like
See here LIVE DEMO