Interview Question


Country: United States




Comment hidden because of low score. Click to expand.
2
of 6 vote

@all,

It 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.

- Murali October 19, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- bharat October 19, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- Murali October 19, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

what is the result of this ?

void testplus()
{int indx=0, a[5];
a[indx]=++indx;
printf("%d",a[indx]);
}

- Anonymous December 25, 2013 | Flag
Comment hidden because of low score. Click to expand.
2
of 2 vote

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.

- Avi October 19, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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.

- Anonymous October 20, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Simple its 0

- Paras Patel October 19, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

please explain.... coz it will give the value at [1]....nd when a[1] have been initialized??

- bharat October 19, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

some garbage value..

- Anonymous October 19, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

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.

- Selmeczy, Péter October 19, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

it will give some garbage value as the array wasn't initialized. a[0] would be 0

- The Artist October 19, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

It will be a garbage value.

- Ajit A October 19, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

@bharat what you are saying is true here but not in the above case..its a garbage value
#include<stdio.h>
# include<conio.h>

int main()
{int indx=0, a[5]={13,15};

printf("%d",a[3]);
getch();
return 0;
}

- Anonymous October 19, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

yeah... some garbage value.....

a[0]=0 and
a[1]=some garbage value..

- PKT October 19, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Undefined behavior. Anything can happen. Lookup sequence point.

- Anonymous October 19, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

it will contain garbage value

- Anonymous October 19, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

it will contain value 0

- Kumar October 20, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

can you explain why 0?

- Murali October 20, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

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.

- peter tang October 20, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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>

- Anonymous October 20, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

You are right, K&R is clear about that.
You should have a separate post about it to upvote it.

- Anonymous October 21, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

default value for an array in C is '0'

- Anonymous October 30, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

0

- Bin December 04, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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...

- yashu December 14, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

sorry not 1 i got ans as 2 wen i initialized a[1] as 2

- yashu December 14, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

outpu is 0
bcoz initializing element of an array next all elements initialized to zero bydefault,

- haripal February 17, 2013 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More