Search code examples
carraysgetcharputchar

character array reads 3 inputs instead of 5 in c?


my code is given below

#include <stdio.h>
#include <ctype.h>

#define size 5

void main(){

    int i;
    char letter[size];
    for(i=0;i<size;i++)
        letter[i]=getchar();
    for(i=0;i<size;i++)
        putchar(toupper(letter[i]));
}

and the output is :

bitto@HP-ProBook-4430s:~$ gcc test.c
bitto@HP-ProBook-4430s:~$ ./a.out
a
s
d
A
S
D

why is this happening? it was to read 5 characters and convert them to upper case.


Solution

  • As EOF said, any input you have will be taking two chars as input because you are typing in A then Enter which C reads as a and \n so that is two chars, not one.