Saturday, October 30, 2010

Ans for 'C' confusion.

Actually when we declare an array of a[5], then legally the processor allocates 20 bytes in main memory. This 20 bytes are completely in use of the program where we have declared the array. Further more, if our main memory has extra space  immediately after this 20 bytes then it allocates them to the program. Suppose if we insert 10 elements in an array of size 5 and f memory is available and free then it will be allowed and if memory is not available then the program will show error or else our program may be terminated.

If more explanation is needed, then do post a comment.. This topic will be explained in detail then after..

Thank you.
Shivang Desai.

Friday, October 22, 2010

'C' confusion

#include<stdio.h>
#include<conio.h>
void main()
{
          int a[5];
          int i;
         clrscr()
          for(i=0;i<10;i++)
          {
                  printf("enter the number:");
                  scanf("%d",a[i]);
          }
          for(i=0;i<10;i++)
          {
                  printf("the number at a[%d] is %d",i,a[i]);
           }
           getch();
}
Q- The array 'a' declared here is only of size 5, which means that it should accept only 5 numbers. But if we iterate it in a loop then it accepts the number of values more than or less than its original size. As example,consider the above program of C, in which array 'a[5]' should only accept 5 values but in loop of 0 to 9, it accepts 10 values.
What is the reason behind this?? if it accepts value according to the iteration of loop then what is the need to declare its size??