2

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.

StaticBeagle
  • 5,070
  • 2
  • 23
  • 34
  • 9
    poor neglected `'\n'`... – EOF Jun 30 '16 at 16:06
  • You need to read up a little on how terminals behave. See [this](http://stackoverflow.com/questions/421860/capture-characters-from-standard-input-without-waiting-for-enter-to-be-pressed) – JJF Jun 30 '16 at 16:14
  • @EOF is certainly right. I expanded on your comment in an answer. – Eli Sadoff Jun 30 '16 at 16:17

1 Answers1

3

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.

Eli Sadoff
  • 7,173
  • 6
  • 33
  • 61
  • this explains it . typing in the characters without spaces in the same line solves the problem. –  Jun 30 '16 at 16:18