Interview Question for Developer Program Engineers


Country: India




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

What is the question?
Which company uses "void main" ?

think what happens printf tries to print a[3]

For illustrative purposes only:

#include <stdio.h>

        int main()
        {
            int i = 0, j = 0;
            char *a[10] = {"hi", "hello", "how"};

            for(j=3; j<10; j++) a[j]="";

            a[0] = "hey";

            for (i = 0; i < 10; i++)
	            printf("%s\n", a[i]);

            return 0;

        }

- S O U N D W A V E March 10, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Interesting question!
Passing NULL pointer to printf("%s",s) causes undefined behavior. So it may up to the compiler decision...
prinf() without "\n" (newline) puts data into buffer and waits for all data to be printed, while with "\n", it prints immediately (?).

But I really don't know why and why not segmentation fault occurs. Any expert here?

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

a[3] is a garbage valued (because it is coming off stack) pointer to a char

when it's passed to printf, printf will try to access characters a[3][0] then a[3][1] ... etc. until some a[3][k] is the '\0' character (signalling end of string).

if you are lucky and say a[3][0] just happens to be '\0', then there is a[4] garbage pointer to deal with on next iteration of the loop

- S O U N D W A V E March 10, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

DId you guys even read the question?

The question is about removing \n from printf("%s\n").

- Anonymous March 10, 2014 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Do your own homework.

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

+1. This smells awful like a problem faced when trying to do homework.

To other/newcomers to the careercup site, please don't encourage questions like these and pollute the Questions page.

Please redirect them to the Forum page, where such questions are allowed.

- Anonymous March 10, 2014 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Try compiling with "-fno-builtin-printf"

- Anon March 10, 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