Output of Simple C Program ?
-
Hi all, #include void fun(void) { int a[2]; a[3] +=7; } void main() { int i = 5; printf("\t %d \n\t",i); fun(); i = 10; printf(" %d \n\t",i); } Output of above program is 5 5 Why output is like this? And it is giving Run-Time error when I use " a[2] +=7; " in the function. Why like this? Thanks in advance. Aniket.
-
Hi all, #include void fun(void) { int a[2]; a[3] +=7; } void main() { int i = 5; printf("\t %d \n\t",i); fun(); i = 10; printf(" %d \n\t",i); } Output of above program is 5 5 Why output is like this? And it is giving Run-Time error when I use " a[2] +=7; " in the function. Why like this? Thanks in advance. Aniket.
Aniket Salunkhe wrote:
int a[2];
You are declaring an array of size 2 (int a[2]). So it will have a zero based index i.e. 0 to 1. This means you cannot access a[2] as that does not belong to you. You only have right to modify positions from a[0] to a[1](inclusive). Modify anything beyond this range could lead to runtime errors and other crashes. Of course you can make the array bigger. :)
Nibu thomas Software Developer Faqs by Michael dunn
-
Aniket Salunkhe wrote:
int a[2];
You are declaring an array of size 2 (int a[2]). So it will have a zero based index i.e. 0 to 1. This means you cannot access a[2] as that does not belong to you. You only have right to modify positions from a[0] to a[1](inclusive). Modify anything beyond this range could lead to runtime errors and other crashes. Of course you can make the array bigger. :)
Nibu thomas Software Developer Faqs by Michael dunn
-
Hi all, #include void fun(void) { int a[2]; a[3] +=7; } void main() { int i = 5; printf("\t %d \n\t",i); fun(); i = 10; printf(" %d \n\t",i); } Output of above program is 5 5 Why output is like this? And it is giving Run-Time error when I use " a[2] +=7; " in the function. Why like this? Thanks in advance. Aniket.
what is it a[0]...//used without being defined int a[3]; a[0]=a[1]=a[2]=0; a[2] +=7; and then output is 5 and 10
-
Hi all, #include void fun(void) { int a[2]; a[3] +=7; } void main() { int i = 5; printf("\t %d \n\t",i); fun(); i = 10; printf(" %d \n\t",i); } Output of above program is 5 5 Why output is like this? And it is giving Run-Time error when I use " a[2] +=7; " in the function. Why like this? Thanks in advance. Aniket.
-
what is it a[0]...//used without being defined int a[3]; a[0]=a[1]=a[2]=0; a[2] +=7; and then output is 5 and 10
-
Hi, If I write same Program as, #include void main() { int i = 5; printf("\t %d \n\t",i); int a[2]; a[3] +=7; i = 10; printf(" %d \n\t",i); } Than Output is, 5 10 Why? So what is problem in that Program? Thanks, Aniket.
You are breaking the rules. Do not do this. You say you understand arrays and indexes...then why do you insist on using a[3]? a[3] is pointing to an area of memory that is being used by another part of the program. It might point to the actual memory being used by int i, it might be pointing to something else. It all depends on how the compiler allocates memory on the stack and in what order you have declared your variables and it's going to change everytime you edit your program. You are not supposed to be using a[3] so don't use it! if you create your array: int a[2]; then you can go use a[0] and a[1], and that's it. Period, exclamation point.