Interview Question for Software Engineer / Developers


Country: United States




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

There's an unusual behavior with the printing of *p .Following will be the output of *p :
Enter the pointer -------> Output in printf (*p)
1 -----------1
3 ----------- 3
5 ----------- 5
9 ----------- 9
10 ----------- 16
11 ----------- 17
14 ----------- 20
20 ----------- 32

- Shobhit July 01, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Got it . We're taking input in the hexadecimal format . But while printing , we're converting it into decimal format .

- Barney July 01, 2012 | Flag
Comment hidden because of low score. Click to expand.
2
of 2 vote

scanf("%p",p); will take the input in hex , if we change it to scanf("%d",p); the output will be as expected, number entered will be in pure decimal and hence no conversion , it is the format specifier which is actually doing this.

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

The behavior is undefined.The explanation goes as follows:
If you want to change the pointer p to point to another memory location, you must use &p in scanf.
However, if you write p, then you must supply the format specifier as %d, because by p, you are actually passing the memory location where the pointer p is pointing[i.e. i]. So, whatever you input that gets stored in i indirectly.
Note that in printf, if the format specifier is changed then the programmer is responsible for all kinds of side effects.

- Aashish July 01, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Hey *p does not print 10 because of the scanf statement. You are taking a user input for p and then printing the value in *p that is why

- coolguy July 01, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Thank God! Someone thinks like me. The question seems incomplete :)

- itscool July 01, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

The output is as desired in Dev C++

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

here throug you change the pointer of p which initially represent the 10 but after exection of scanf it has changed

- sailesh November 29, 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