Can you explain further your inputs and expected output? I'm not clear on what you expect.
As far as your code goes, you have a fixed length array of length 4, so you can't have more that 3 characters in it (with the null terminator).
You're not checking that you've not gone off the end of your array. It can only hold 3 characters, but your code allows you to enter an indeterminate amount.
You're not null terminating your string. The easiest way to handle this is to set the array to zero before starting.
You don't set the End of File condition by entering letters EOF. You have to terminate the stream. How you do that depends on the OS. But on Windows, you can send it with F6 then Enter I think; I'm not sure. On a Unix terminal it's Ctrl-D.
I want to store few characters in a character array.After i run the program. I enter the characters one by one. If i enter the first character as "w"(or any other character) then the program prints "0" and "1".After i enter the second character the program prints "2" "Enter EOF" and "3".
But according to me, the code is suppose to print only "0" for the first character. Then when i enter the second character it should print only "1", after entering the third character it should only print "2", and after entering the fourth and final character it should print "Enter Eof" (Telling the user that no more space is available)then print "3".
If i enter the first character as "w"(or any other character) then the program prints "0" and "1"
But according to me, the code is suppose to print only "0" for the first character
When you press w<Enter>, you've entered two characters: the character 'w' and the end of line character '\n'.
There are many ways to deal with it.
You could explicitly check for endline and continue:
1 2 3 4 5
int a; // NOT char! getchar() returns int for a reason
while((a=getchar())!=EOF)
{
if( a == '\n')
continue;
or you could use scanf instead
1 2 3 4
char a; // unlike getchar(), scanf can use a char
while(scanf(" %c", &a) == 1)
// ^ don't miss this space, it's very important
{