Yahoo Interview Question


Country: United States




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

(1)first of all you must know how while loop works..

intialisation;

while(check condition)
{
   increment;
}

(2) scanf() always return no. of fields it reads. so in your first case ..

while(scanf("%d,",&a))
{
//store a as you wish to
printf("%d ",a);
}

here output will be 1 2 3 4 5 6 7 8 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9..........run time error.

so what happened is scanf always return no. of fields so up to 9 it was returning value 1.but after that there is no input so scanf returns wierd values like -124689234 etc..,,,but remember our variable a still has value 9 .so as simple as this it behave like this

- Anonymous August 14, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

how to make it behave in a normal manner so that when the input is given in the form of a string:

"1,2,3,4,5,6,7,8,9"

i can use the above method to store the numbers in an array

int ar[10];
ar[0]=1
ar[1]=2
ar[2]=3
:
:
: so on.......

- gdg August 14, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

- scanf() blocks when called the first time, but does not block after that
if scanf() is given invalid input, it does not advance the pointer in its internal buffer, hence the buffer remains what was earlier (9 in this case) , which keeps on getting printed.
- you can check if scanf() returned > 0 in the while condition. This will allow scanf() to function normally

- nharryp November 03, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

(1)first of all you must know how while loop works..

intialisation;

while(check condition)
{
   increment;
}

(2) scanf() always return no. of fields it reads. so in your first case ..

while(scanf("%d,",&a))
{
//store a as you wish to
printf("%d ",a);
}

here output will be 1 2 3 4 5 6 7 8 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9..........run time error.

so what happened is scanf always return no. of fields so up to 9 it was returning value 1.but after that there is no input so scanf returns wierd values like -124689234 etc..,,,but remember our variable a still has value 9 .so as simple as this it behave like this

- Anonymous August 14, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

(1)first of all you must know how while loop works..

intialisation;

while(check condition)
{
   increment;
}

(2) scanf() always return no. of fields it reads. so in your first case ..

while(scanf("%d,",&a))
{
//store a as you wish to
printf("%d ",a);
}

here output will be 1 2 3 4 5 6 7 8 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9..........run time error.

so what happened is scanf always return no. of fields so up to 9 it was returning value 1.but after that there is no input so scanf returns wierd values like -124689234 etc..,,,but remember our variable a still has value 9 .so as simple as this it behave like this

- Anonymous August 14, 2014 | 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