Interview Question


Country: United States




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

From the Standard: "The sizeof operator shall not be applied to an expression that has function type ...".
Hence the above code is not valid C code and should not compile.

- Anonymous September 02, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

A conforming implementation shall produce at least one diagnostic message for a violation of any syntax rule or constraint.

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

Disagree.
Constraints - syntactic and semantic restrictions by which the exposition of language elements is to be interpreted.
Undefined behavior - behavior, upon use of a nonportable or erroneous program construct, of erroneous data, or of indeterminately valued objecs, for which the standard imposes no requirements.
If a "shall" or "shall not" requirement that appears outside of a constraint is violated, the behavior is undefined. Undefined behavior is otherwise indicated in this standard by the words "undefined behavior" or by the omission of any explicit definition of behavior.

This is clearly a constraints violation not undefined behavior.

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

"The sizeof Operator
Constraints
The sizeof operator shall not be applied to an expression that has function type...
Semantics
The sizeof operator yields the size (in bytes)..."
That is verbatim from the Standard.
Also from the Standard: "An implementation may also successfully translate an invalid program."
But as previously stated a compliant compiler shall emit a diagnostic message.

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

C standard forbids it - when compiled with gcc -pedantic, it produces invalid application of ‘sizeof’ to a function type warning.

However gcc compiles it and returns 1 for sizeof(main), and it is not a size of function pointer.

It seems to be compiler-dependent.

- nishit September 07, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I tried running the code it prints an error, how did u get a 1?

- Anonymous February 03, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

rsn:-
fn return only 1 value .
value is float.
value stor in b.
b is int type.
so its output is 1.

- Ashrotriya September 03, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 3 vote

Sizeof is not executed if in a printf statement as it is true 1 is printed

not 100% sure

- Avi September 03, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

The value is 1 . Function pointers can take value of 1 byte (0 for Null and 1-255 as indexed into a table with physical address ) if the number of functions is less than 255 and 2 bytes if greater than that .

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

and adding to it sizeof(fn)=sizeof(void *)

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

What my understanding says is (fn) in most basic sense is a "Pointer" which holds "memory address" .... so sizeof(Any damn pointer varible) will be same and that is mostly machine or say complier dependent....but why it is giving "1" anybody ...my concepts are shaken..help pelase!!

- Mukesh September 07, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 2 vote

as fn is only function pointer.

- dev September 02, 2012 | 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