Interview Question

  • -
    1
    of 1 vote
    8
    Answers

    int fun(char *a){
    printf("%d\n",sizeof(a));
    return 1; }
    int main()
    {
    char a[20];
    printf("%d\n",sizeof(fun(a)));
    }
    Explain the output.

    - Barney on July 02, 2012 in United States Report Duplicate | Flag
    C

Country: United States


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

Output will be 4. An expression is never evaluated inside sizeof operator.
So, the function will not be called. However, you must be surprising that how the compiler is able to output 4. Its because compiler sees the signature of the function. Change the return type to double & you will see the output 8.

- Aashish on July 02, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

dude the o/p is 4
just checked

- Avi on July 02, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Did you check my answer. I have also said 4.

- Aashish on July 02, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

couldn't get the last part

- Avi on July 02, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I explained the reason why only printf inside main() is working.
And how compiler is able to calculate size of return type of the function fun().

- Aashish on July 02, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@shondik : my question is that why is the function not called . Any reason or should i remember it as a rule ?

- Barney on July 03, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

or maybe it's because sizeof is a compile time operator

- Barney on July 03, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Think of it this way: all sizeof operations are resolved to a constant at compile time. After compilation, the function call inside the sizeof doesn't exist.

- eugene.yarovoi on July 08, 2012 | Flag


Add a Comment
Name:

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

Books

is a comprehensive book walking you through 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