NVIDIA Interview Question






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

It will give a syntax error as "x undeclared" .. The preprocessor replaces all the statements followed by the #define statements within the scope..

This will work without any error-
void main()
{
#define x 1
funct();
}

funct()
{
printf("%d",x);
}

whereas this will give an error-
void main()
{
funct();
printf("%d",x);
}

funct()
{
#define x 1
}

- Teja October 24, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I second Teja. This may be a case of compiler time error. 'x undeclared ' may be the error

- amit October 25, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

They are defined in global var or static var area and have global scope.

- Anonymous October 23, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#define is a preprocessor. it doesn't take any area inside the program memory. Before compilation it replace with the actual value inside the code.

- Anonymous October 23, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yes it will print 1 even if you call printf before 'funct' call

- Anonymous October 23, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

i dont agree with tejas if variables defined by #define r placed in global area then it does not matter where they r declared they should be visible

- deestiny October 25, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Yes deestiny. my explanation for the code is wrong .. however, output is correct..
The correct reason is.. preprocessor scans the code from beginning and replaces all the statements followed by a #define statement with the corresponding value..
g()
{
#define b 6
}
int main()
{
g();
f();
printf("%d",b);
return 0;
}
f()
{
#define b 4
}
Now.. the b value in main() is 6..
If you remove the call to method g(), then also output will be 6, as preprocessor doesnt care the execution sequence.. It just scans the files executing the preprocessor statements..

- Teja October 25, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

yes tejas u r right .

- vikas August 18, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

For the above snippet,it would give a compilation error stating that x was undeclared.However if you reorder the code.

void funct()
{
#define x 1
}

void main()
{
funct();
printf("%d",x);
}

this would print 1.

- Ano October 26, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The subquestion to be answered is where does b is stored?
--> I think it is not stored in Data Segment or BSS. All the instances of b in code segment are replaced by 6. And hence b is not actually stored.

- S3 November 01, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Answer is compilation error.
so how does it work and where is it stored. pre-processor parses and does textual replacement of the value. So the value is nothing but some constant, it is stored in global/static area and the type used will be based on the value.

to some of the people ho had doubt abut why it prints 4 at one place and 6 at other place bcoz pre-processor would have substituted so and when it comes to compilation stage the values are already substituted and these values will be then stored in global/static area.

- Anonymous November 08, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Answer is 1.
I compiled and ran the program.

- Avinash November 09, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

macro.c: In function ‘main’:
On Ubuntu9.10
gcc marco.c -o marco
macro.c:5: error: ‘x’ undeclared (first use in this function)
macro.c:5: error: (Each undeclared identifier is reported only once
macro.c:5: error: for each function it appears in.)
macro.c:3: warning: return type of ‘main’ is not ‘int’

- kaka December 25, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I executed it too.
Throws an error if f() called after printf..
prints 1 if f() called and defined before.. tried in both c and c++

- Anonymous March 14, 2011 | 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