Interview question?

P

Padmat

void main()
{
int i = 20;
void fn();
fn();

printf ("%d",i);
}

void fn()
{
// add ur code only here so that i in main should print OTHER THAN 20
}

Is there any Leagal solution for this?
Thanks,
 
M

Martin Ambuhl

Padmat said:
void main()
^^^^
main() returns an int.
If this is an interview question, walk out.
You do not want to work as a C programmer in anyplace that countenances
baby mistakes.
{
int i = 20;
void fn();
fn();
printf ("%d",i);
^^^^^^
No declaration in scope for this variadic question.
If this is an interview question, walk out.
You do not want to work as a C programmer in anyplace that countenances
baby mistakes.
}

void fn()
{
// add ur code only here so that i in main should print OTHER THAN 20
^^
Stupid adolescent spelling.
If this is an interview question, walk out.
}

Is there any Leagal solution for this?

Of course not. The identifier 'i' in main is local to main(). If your
interviewer wants you to play games with non-portable (even to the next
revision of your compiler) assumptions about how variables are stored,
walk out.
 
K

Kenny McCormack

void main()
{
int i = 20;
void fn();
fn();

printf ("%d",i);
}

void fn()
{
// add ur code only here so that i in main should print OTHER THAN 20
}

Is there any Leagal solution for this?
Thanks,

I've re-arranged your code a bit, to make it a bit more clc-compliant
(though no claim of full compliance is made - I'm sure the regs will
come up with something to nitpick). Note the missing include that would
have made your program UB.

% cat x.c
void fn()
{
// add ur code only here so that i in main should print OTHER THAN 20
}

#include "stdio.h"

int main()
{
int i = 20;
fn();

printf ("i = %d\n",i);
return 0;
}

% gcc -Wall -Werror x.c
% ./a.out
i = 13
%

HTH
 
A

Antoninus Twink

void main()
{
int i = 20;
void fn();
fn();

printf ("%d",i);
}

void fn()
{
// add ur code only here so that i in main should print OTHER THAN 20
}

Is there any Leagal solution for this?
Thanks,

Here is a solution for Linux/gcc-i386. I had to make i volatile to stop
gcc optimizing it away.


#include <stdio.h>

#define OFFSET 8 /* may need to change this depending on your compiler */

main()
{
volatile int i = 20;
void fn();
fn();

return printf ("%d\n",i);
}

void fn()
{
volatile int b;
*(&b + OFFSET)=42;
}

Happy hacking!
 
P

pete

Padmat said:
void main()
{
int i = 20;
void fn();
fn();

printf ("%d",i);
}

void fn()
{
// add ur code only here so that i in main should print OTHER THAN 20
}

Is there any Leagal solution for this?
Thanks,

/* BEGIN new.c */

#include <stdio.h>

void fn(void);

int main(void)
{
int i = 20;

fn();
printf("%d\n", i);
return 0;
}

void fn(void)
{
printf("OTHER THAN ");
}

/* END new.c */
 
T

Tomás Ó hÉilidhe

Kenny McCormack:

I've re-arranged your code a bit, to make it a bit more clc-compliant
(though no claim of full compliance is made - I'm sure the regs will
come up with something to nitpick). Note the missing include that
would have made your program UB.

<snip code>

Good stuff.

As I'm sure you're already aware tho, you need angle brackets instead of
inverted commas:

#include <stdio.h>
 
T

Tomás Ó hÉilidhe

Padmat:
void main()
{
int i = 20;
void fn();
fn();

printf ("%d",i);
}

void fn()
{
// add ur code only here so that i in main should print OTHER THAN 20
}

Is there any Leagal solution for this?


Nope, because other functions can't access the local automatic variables of
other functions (unless of course you supply them with a pointer to the
variable in question).
 
K

Kenny McCormack

pete ha scritto:


This wins hands down. Good catch! :)

R.D.

Yes. Very cool.

Of course, it leaves open the question as to exactly what the phrase
"i in main" means.
 
R

Remo D.

Kenny McCormack ha scritto:
Yes. Very cool.

Of course, it leaves open the question as to exactly what the phrase
"i in main" means.

I guess the guy who wrote that line had some problem with the keyboard.
First "ur", then "i in" :)

R.D.

BTW, I think the question was intended to start the conversation with
the candidate. Probably they were only interested in the candidate's
thinking process. Pete thought out of the box, Antoninus showed he would
do literally *anything* to solve the problem, others could have said
that there's no "legal" way since i is not visible in main, etc..

No "right" answer, just a teaser to see your reaction.
 
I

Ian Collins

BTW, I think the question was intended to start the conversation with
the candidate. Probably they were only interested in the candidate's
thinking process. Pete thought out of the box, Antoninus showed he would
do literally *anything* to solve the problem, others could have said
that there's no "legal" way since i is not visible in main, etc..

No "right" answer, just a teaser to see your reaction.

While some of us would have questioned the interviewer's ability to
write comprehensible English!
 
P

pete

Remo said:
Kenny McCormack ha scritto:

I guess the guy who wrote that
line had some problem with the keyboard.
First "ur", then "i in" :)

I interpreted "i in main"
as an Iyaric "I word" expression.
 
F

Flash Gordon

Antoninus Twink wrote, On 01/02/08 21:20:
Here is a solution for Linux/gcc-i386. I had to make i volatile to stop
gcc optimizing it away.

Doesn't work one all systems meeting that spec. For example, it does not
work on this one.
#include <stdio.h>

#define OFFSET 8 /* may need to change this depending on your compiler */

main()
{
volatile int i = 20;
void fn();
fn();

return printf ("%d\n",i);
}

void fn()
{
volatile int b;
*(&b + OFFSET)=42;
}


markg@brenda:~$ cat t.c
#include <stdio.h>

#define OFFSET 8 /* may need to change this depending on your compiler */

main()
{
volatile int i = 20;
void fn();
fn();

return printf ("%d\n",i);
}

void fn()
{
volatile int b;
*(&b + OFFSET)=42;
}
markg@brenda:~$ gcc t.c
markg@brenda:~$ ./a.out
20
markg@brenda:~$ uname -a
Linux brenda 2.6.22-14-generic #1 SMP Tue Dec 18 08:02:57 UTC 2007 i686
GNU/Linux
markg@brenda:~$
 
A

Antoninus Twink

Antoninus Twink wrote, On 01/02/08 21:20:

Doesn't work one all systems meeting that spec. For example, it does not
work on this one.

Did you change OFFSET appropriately for the version and optimization
level of your compiler, as specified in this comment?
 
T

Tomás Ó hÉilidhe

Kenny McCormack:
Ya think so?
(I don't)


For includes files within your own project:

#include "data.h"

For include files which are part of the implementation or that are shared:

#include <stdio.h>
What is an inverted comma???


You might know them better as quotes: " "
 
A

Army1987

Padmat said:
void main()
{
int i = 20;
void fn();
fn();

printf ("%d",i);
}

void fn()
{
// add ur code only here so that i in main should print OTHER THAN 20
extern void exit(int);
extern int putchar(int);
putchar('2');
putchar('1');
exit(0);
 
A

Army1987

Tomás Ó hÉilidhe said:
Kenny McCormack:



<snip code>

Good stuff.

As I'm sure you're already aware tho, you need angle brackets instead of
inverted commas:

#include <stdio.h>

With #include "stdio.h", if ./stdio.h cannot be found, it is treated as if
it were #include <stdio.h>, therefore including /usr/include/stdio.h (or
wherever the standard header is, and regardless of whether it's actually a
file).
 
K

Kenny McCormack

Kenny McCormack:



For includes files within your own project:

#include "data.h"

Really? Gee, I'd never have known if you hadn't been so kind as to
point it out.

By the way, how do you suppose I did get 13 as the output of my program?
For include files which are part of the implementation or that are shared:

#include <stdio.h>
#include <some_library\gui.h>

Really? Gee, I'd never have known if you hadn't been so kind as to
point it out.
You might know them better as quotes: " "

In much the same way as if I referred to a cat as an elephantoid, you
might ask me what an elephantoid is, and I would almost certainly reply
that you probably know it as a cat.
 
A

Army1987

Kenny said:
Of course, it leaves open the question as to exactly what the phrase
"i in main" means.

Of course, i in main is an int, so it cannot print anything at all. But it
is possible that the OP is not a native speaker of English and not very
fluent in it, so he didn't literally mean what he wrote. But, using the
most reasonable interpretation for that, well, in pete's program, "OTHER
THAN 20" *is* printed by the printf which prints the value of i in main
(provided that stdout is either line- or fully buffered), as fn just
writes into a stdio library buffer (it contains no newline character, so
it is not printed until a '\n' is putchar()red or stdout is fflush()ed).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,150
Latest member
MakersCBDReviews
Top