Interview Question
Country: United States
without compiling it I also took a[1] as a garbage value .. but when i compiled it it gave me a value of -1 , and when i checked for a[2] , a[3] .. it gave some garbage value .. and I dnt know why ?? thats why posted here .. if somebody can throw some light .. what i did is as follows
#include<stdio.h>
void main()
{int indx=0, a[5];
a[indx]=indx++;
printf(" a[indx]%d \n",a[indx]);
printf(" a[indx]%d \n",a[2]);
printf(" a[indx]%d \n",a[3]);
}
output:
a[indx]-1
a[indx]8653
a[indx]9351
Basically if the array is not initialized , it will initially contain some garbage value.
So in this case initially :
a[0] = garbage value
a[1] = garbage value
a[2] = garbage value
a[3] = garbage value
a[4] = garbage value
a[5] = garbage value
Then we are allocating
a[0] = 0 and increment the indx value.
Now the value of indx is 1.
we are trying to print the a[1] .
Guess what : it will be garbage only.
a[2] will also be garbage ,
a[3] will also be garbage.
When I have executed the above program I got the following o/p:
a[indx]-858993460
a[indx]-858993460
a[indx]-858993460
-858993460 is garbage value.
Let me know if you have any further clarifications
some garbage value
a[index]= index++; // here first 0 is put in a[0] then index++ is done
So when printf is called it will print value of a[1] which is some unknown number.
When the value of index in a[index] is done, before or after the update of index because of index++, is unspecified. Suppose you had a[index] = foo(index). Here index is a global variable, or you can do a[index] = foo(&index). If index is modified by foo, it is unspecified what value of index in a[index] is used, the one before or after the modification.
In this case it doesn't matter. You have undefined behavior, anything can happen.
It is either garbage or 0.
Depending on how this is evaluated: a[indx]=indx++;
The order is undefined, what is sure that after the statement indx is 1 (it was 0 before)
But if a[0] or a[1] is assigned to 0 (the old value of indx) - it is not defined.
So the a[1] - what is printed out - can be garbage or 0.
Garbage value.
This is because post-fix ++ operator is return-by-value.
int operator++();// post-fix ++ operator
int& operator++();// pre-fix ++ operator
In a[indx] = indx ++, indx is loaded to a register then done the part a[0] = 0; then calling indx.++(). And it is return-by-value, so literally indx may have different address in stack before executing this assginment.
Sad to see the many nonsense comments.
Quoting from K&R 2nd p 53: "In any expression involving side effects, there can be subtle dependencies on the order in which variables taking part in the expression are updated. One unhappy situation is typified by the statement
a[i] = i++;
The question is whether the subscript is the old value of i or the new. Compilers can interpret this in different ways, and generate different answers depending on their interpretation. The standard intentionally leaves most such matters unspecified."
And, let us not forget what the Standard says about sequence points (ANSI 3.3): "Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression. Furthermore, the prior value shall be accessed only to determine the value to be stored." With an added footnote: "This paragraph renders undefined statement expression such as
i = ++i + 1;
while allowing
i = i + 1;
<end-of quote>
it may vary from compiler to compiler,so wen i run with turbo C m getting a[1] as '0',in order to check it i just initialized a[1]=2,then am getting a[1] value as '1',again i removed a[1] value and compiled i got same '0',So in turbo C its taking the default value that is '0' as ans,in other compiler it may vary...
@all,
- Murali October 19, 2012It is garbage value only.
When you compile and execute the following program you will get the output as 0.
#include<stdio.h>
int main()
{
int a[5];
printf("The value of array is %d \n",a[1]);
return 0;
}
########################################> ./a.out
The value of array is 0.
To demo this as a garbage value I have executed on Big endian platform.
# ./a.out
The value of array is -17148080
In this case you will get the -17148080 which is a garbage value only.