c - Why does my program accept one integer too many and input one too few? -
i'd understand why program allows me input 3 integers when have defined size 2. , when returns array returns 2 numbers not 3 have entered.thanks help.
//c how program exercises 2.23 #include <stdio.h> #include <conio.h> #define size 2 int main (void){ int myarray[size]; int count; printf("please enter 5 integers\n"); (count=1;count<=size;count++){ scanf("%d\n",&myarray[count]); } (count=1;count<=size;count++){ printf("the values of myarray %d\n",myarray[count]); } getch(); return 0; }
your loops should be
for (count=0;count<size;count++)
array indexing 0
based in c.
since have whitespace chatacter (\n
) in scanf()
call, waits input non-whitespace character complete each call. remove \n
:
(count=0;count<size;count++){ scanf("%d",&myarray[count]); }
Comments
Post a Comment