473,405 Members | 2,344 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Interview question?

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,

Feb 1 '08 #1
20 1306
In article <sl*******************@nospam.invalid>, Padmat <no@spam.comwrote:
>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

Feb 1 '08 #2
On 1 Feb 2008 at 20:21, Padmat wrote:
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!

Feb 1 '08 #3
Padmat wrote:
>
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 */

--
pete
Feb 1 '08 #4
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>

--
Tomás Ó hÉilidhe
Feb 1 '08 #5
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).

--
Tomás Ó hÉilidhe
Feb 1 '08 #6
pete ha scritto:
Padmat wrote:
>void fn()
{
// add ur code only here so that i in main should print OTHER THAN 20
}

Is there any Leagal solution for this?
void fn(void)
{
printf("OTHER THAN ");
}
This wins hands down. Good catch! :)

R.D.
Feb 1 '08 #7
In article <Xn***************************@194.125.133.14>,
Tomás Ó hÉilidhe <to*@lavabit.comwrote:
>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
Ya think so?
(I don't)
>instead of inverted commas:
What is an inverted comma???
>#include <stdio.h>

--
Tomás Ó hÉilidhe

Feb 1 '08 #8
In article <47***********************@reader5.news.tin.it>,
Remo D. <rdentatowrote:
>pete ha scritto:
>Padmat wrote:
>>void fn()
{
// add ur code only here so that i in main should print OTHER THAN 20
}

Is there any Leagal solution for this?
>void fn(void)
{
printf("OTHER THAN ");
}

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.

Feb 1 '08 #9
Kenny McCormack ha scritto:
In article <47***********************@reader5.news.tin.it>,
Remo D. <rdentatowrote:
>pete ha scritto:
>>Padmat wrote:
void fn()
{
// add ur code only here so that i in main should print OTHER THAN 20
}

Is there any Leagal solution for this?
void fn(void)
{
printf("OTHER THAN ");
}
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.
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.
Feb 1 '08 #10
Remo D. wrote:
>>>Padmat wrote:
void fn()
{
// add ur code only here so that i in main should print OTHER THAN 20
}
>
>
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!

--
Ian Collins.
Feb 1 '08 #11
Remo D. wrote:
>
Kenny McCormack ha scritto:
In article <47***********************@reader5.news.tin.it>,
Remo D. <rdentatowrote:
pete ha scritto:
Padmat wrote:
void fn()
{
// add ur code only here so that i in main should print OTHER THAN 20
}

Is there any Leagal solution for this?
void fn(void)
{
printf("OTHER THAN ");
}
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.

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.

--
pete
Feb 1 '08 #12
Antoninus Twink wrote, On 01/02/08 21:20:
On 1 Feb 2008 at 20:21, Padmat wrote:
>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.
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:~$
--
Flash Gordon
Feb 2 '08 #13
On 2 Feb 2008 at 0:28, Flash Gordon wrote:
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 */
Did you change OFFSET appropriately for the version and optimization
level of your compiler, as specified in this comment?

Feb 2 '08 #14
Kenny McCormack:
>>As I'm sure you're already aware tho, you need angle brackets

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>
#include <some_library\gui.h>

>>instead of inverted commas:

What is an inverted comma???

You might know them better as quotes: " "

--
Tomás Ó hÉilidhe
Feb 2 '08 #15
Padmat wrote:
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);
}
--
Army1987 (Replace "NOSPAM" with "email")
Feb 2 '08 #16
Tomás Ó hÉilidhe wrote:
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>
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).
--
Army1987 (Replace "NOSPAM" with "email")
Feb 2 '08 #17
In article <Xn***************************@194.125.133.14>,
Tomás Ó hÉilidhe <to*@lavabit.comwrote:
>Kenny McCormack:
>>>As I'm sure you're already aware tho, you need angle brackets

Ya think so?
(I don't)


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.
>
>>>instead of inverted commas:

What is an inverted comma???


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.

Feb 2 '08 #18
Kenny McCormack wrote:
In article <47***********************@reader5.news.tin.it>,
Remo D. <rdentatowrote:
>>pete ha scritto:
>>Padmat wrote:
void fn()
{
// add ur code only here so that i in main should print OTHER THAN 20
}

Is there any Leagal solution for this?
>>void fn(void)
{
printf("OTHER THAN ");
}

This wins hands down. Good catch! :)
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).

--
Army1987 (Replace "NOSPAM" with "email")
Feb 2 '08 #19
Army1987 <ar******@NOSPAM.itwrites:
[...]
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).
Not quite. The standard says nothing about "./" or "/usr/include"
(you acknowledged the latter, but not the former). It's not even
clear that "./stdio.h" refers to "stdio.h" in the current directory;
there might not be such a thing as a "current directory", and if there
is, it might not be called ".", and "/" might not be a valid directory
delimiter.

``#include "stdio.h"'' causes the file identified by ``stdio.h''
(which may or may not be a file *named* "stdio.h") in a sequence of
implementation-defined places. If that search fails, the directive is
then reprocessed as if it said ``#include <stdio.h>''.

So ``#include "stdio.h"'' is very likely to work, but it can fail if
there happens to be a file named stdio.h, or rather identified by the
character sequence ``stdio.h'', in the implementation-defined search
path.

--
Keith Thompson (The_Other_Keith) <ks***@mib.org>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Feb 2 '08 #20
Keith Thompson wrote:
``#include "stdio.h"'' causes the file identified by ``stdio.h''
(which may or may not be a file *named* "stdio.h") in a sequence of
implementation-defined places. If that search fails, the directive is
then reprocessed as if it said ``#include <stdio.h>''.

So ``#include "stdio.h"'' is very likely to work, but it can fail if
there happens to be a file named stdio.h, or rather identified by the
character sequence ``stdio.h'', in the implementation-defined search
path.
If we want to be *really* pedantic, 7.1.2 says "If a file with the same name as one of the above < and delimited sequences, not
provided as part of the implementation, is placed in any of the standard places that are
searched for included source files, the behavior is undefined.", so
having a "file" with that "name" in that "place" is Bad even if
`#include <stdio.h>` is used.

--
Army1987 (Replace "NOSPAM" with "email")
Feb 3 '08 #21

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

54
by: Spammay Blockay | last post by:
I've been tasked with doing technical interviews at my company, and I have generally ask a range of OO, Java, and "good programming technique" concepts. However, one of my favorite exercises I...
10
by: Gopal Krish | last post by:
I was asked this question in an interview. How can you display the contents of an ASP page (from another web server) in a aspx page, ie, first half of the screen will display contents from asp...
0
by: Jobs | last post by:
Download the JAVA , .NET and SQL Server interview sheet and rate yourself. This will help you judge yourself are you really worth of attending interviews. If you own a company best way to judge if...
2
by: Jobs | last post by:
Download the JAVA , .NET and SQL Server interview with answers Download the JAVA , .NET and SQL Server interview sheet and rate yourself. This will help you judge yourself are you really worth of...
0
by: connectrajesh | last post by:
INTERVIEWINFO.NET http://www.interviewinfo.net FREE WEB SITE AND SERVICE FOR JOB SEEKERS /FRESH GRADUATES NO ADVERTISEMENT
18
by: Nobody | last post by:
I've been looking for a job for a while now, and have run into this interview question twice now... and have stupidly kind of blown it twice... (although I've gotten better)... time to finally...
2
by: freepdfforjobs | last post by:
Full eBook with 4000 C#, JAVA,.NET and SQL Server Interview questions http://www.questpond.com/SampleInterviewQuestionBook.zip Download the JAVA , .NET and SQL Server interview sheet and rate...
0
by: freesoftwarepdfs | last post by:
Ultimate list of Interview question website.....Do not miss it http://www.questpond.com http://msdotnetsupport.blogspot.com/2007/01/net-interview-questions-by-dutt-part-2.html...
0
by: Free PDF | last post by:
..NET , SQL Server interview questions websites.... http://www.questpond.com http://www.geocities.com/dotnetinterviews/...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.