473,466 Members | 3,167 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

A trick with if-else

void main
{
int x;
if(x)
printf("Hello");
else
printf("World");
}
What value can be given to x such that the output is "Hello
World"?

May 16 '06 #1
24 2796

Piyush Agarwal wrote:
void main
{
int x;
if(x)
printf("Hello");
else
printf("World");
}
What value can be given to x such that the output is "Hello
World"?


I think you misrepresented the trick question.

With `x` an `int` it will never work.

However, if you `#define x` appropriatelly, you can get the desired
effect. E.g.:

#define x (!printf("Hello "))

will do the trick. Obviously, once you correct your code to something
that actually resembles C:

#include <stdio.h>

#define x (!printf("Hello, "))

int main(void)
{
if(x)
printf("Hello");
else
printf("World");

printf("\n");

return 0;
}

May 16 '06 #2
Piyush Agarwal wrote:
void main
{
`main` is a function that returns `int`, not a non-function
of type `void` and value `bother, there was a syntax error`.
int x;
if(x)
printf("Hello");
else
printf("World");
}
What value can be given to x such that the output is "Hello
World"?


There isn't one.

If you didn't mean /value/, but /initialiser/, then easy-peasy.

--
Chris "but the if becomes irrelevant" Dollin
"People are part of the design. It's dangerous to forget that." /Star Cops/
May 16 '06 #3
Piyush Agarwal said:
void main
{
int x;
if(x)
printf("Hello");
else
printf("World");
}
What value can be given to x such that the output is "Hello
World"?


The output of the program is undefined for no fewer than three different
reasons. So it might well say "Hello World" as written. Or, of course, it
might not.

Until you understand what the three reasons are, I recommend that you spend
less time with puzzle questions and more time with a good C book.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
May 16 '06 #4
Piyush Agarwal wrote:

void main
{
int x;
if(x)
printf("Hello");
else
printf("World");
}
What value can be given to x such that the output is "Hello
World"?


Given that you're on Google, why not just search the c.l.c archives for
the thread on the almost exact same question from a few weeks ago?

Perhaps you should give us your professor's name, as I'm sure we'd all
like to know who gives out such homework assignments.

Of course, given your syntax error on "void main" (and numerous other
things), your version of the "problem" is unsolvable.

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:Th*************@gmail.com>

May 16 '06 #5
Chris Dollin <ch**********@hp.com> writes:
Piyush Agarwal wrote:
void main
{


`main` is a function that returns `int`, not a non-function
of type `void` and value `bother, there was a syntax error`.
int x;
if(x)
printf("Hello");
else
printf("World");
}
What value can be given to x such that the output is "Hello
World"?


There isn't one.

If you didn't mean /value/, but /initialiser/, then easy-peasy.


OK, I'll bite.... What initialiser?

--

John Devereux
May 16 '06 #6

Vladimir Oka wrote:
Piyush Agarwal wrote:


[ snip code with UB ]
What value can be given to x such that the output is "Hello
World"?


I think you misrepresented the trick question.

With `x` an `int` it will never work.


#include <stdio.h>

int main(void)
{
int x = 0;
if ( (printf("Hello, "), x) )
printf("Hello");
else
printf("World\n");
return 0;
}

May 16 '06 #7

John Devereux wrote:
Chris Dollin <ch**********@hp.com> writes:
Piyush Agarwal wrote:
void main
{


`main` is a function that returns `int`, not a non-function
of type `void` and value `bother, there was a syntax error`.
int x;
if(x)
printf("Hello");
else
printf("World");
}
What value can be given to x such that the output is "Hello
World"?


There isn't one.

If you didn't mean /value/, but /initialiser/, then easy-peasy.


OK, I'll bite.... What initialiser?


You can do:

int x = !printf("Hello ");

[The "solution" I overlooked in my earlier post.]

May 16 '06 #8

Suman wrote:
Vladimir Oka wrote:
Piyush Agarwal wrote:


[ snip code with UB ]
What value can be given to x such that the output is "Hello
World"?


I think you misrepresented the trick question.

With `x` an `int` it will never work.


#include <stdio.h>

int main(void)
{
int x = 0;
if ( (printf("Hello, "), x) )
printf("Hello");
else
printf("World\n");
return 0;
}


I don't think you were allowed to change the `if` statement. Have a
look at my other post as well.

May 16 '06 #9
"Vladimir Oka" <no****@btopenworld.com> writes:
John Devereux wrote:
Chris Dollin <ch**********@hp.com> writes:
> Piyush Agarwal wrote:
>
>> void main
>> {
>
> `main` is a function that returns `int`, not a non-function
> of type `void` and value `bother, there was a syntax error`.
>
>> int x;
>> if(x)
>> printf("Hello");
>> else
>> printf("World");
>> }
>> What value can be given to x such that the output is "Hello
>> World"?
>
> There isn't one.
>
> If you didn't mean /value/, but /initialiser/, then easy-peasy.


OK, I'll bite.... What initialiser?


You can do:

int x = !printf("Hello ");

[The "solution" I overlooked in my earlier post.]


Aha! :)

--

John Devereux
May 16 '06 #10

Vladimir Oka wrote:
Suman wrote:
Vladimir Oka wrote:
Piyush Agarwal wrote:


[ snip code with UB ]
> What value can be given to x such that the output is "Hello
> World"?

I think you misrepresented the trick question.

With `x` an `int` it will never work.


#include <stdio.h>

int main(void)
{
int x = 0;
if ( (printf("Hello, "), x) )
printf("Hello");
else
printf("World\n");
return 0;
}


I don't think you were allowed to change the `if` statement. Have a
look at my other post as well.


Show me where this constraint was mentioned ;)
Also your _other_ post came in a minute too late, I'd
already posted my reply before it came to being.

May 16 '06 #11

Suman wrote:
Vladimir Oka wrote:
Suman wrote:
Vladimir Oka wrote:
> Piyush Agarwal wrote:

[ snip code with UB ]

> > What value can be given to x such that the output is "Hello
> > World"?
>
> I think you misrepresented the trick question.
>
> With `x` an `int` it will never work.

#include <stdio.h>

int main(void)
{
int x = 0;
if ( (printf("Hello, "), x) )
printf("Hello");
else
printf("World\n");
return 0;
}


I don't think you were allowed to change the `if` statement. Have a
look at my other post as well.


Show me where this constraint was mentioned ;)
Also your _other_ post came in a minute too late, I'd
already posted my reply before it came to being.


Well, it wasn't mentioned in so many words. I thought it was implied.
Still, you could even do someting like:

if ( x = !printf("Hello ") )

or even better:

if ( x = (printf("Hello ") != 6) )

;-)

May 16 '06 #12
Vladimir Oka wrote:

John Devereux wrote:
Chris Dollin <ch**********@hp.com> writes:
> Piyush Agarwal wrote:
>
>> void main
>> {
>
> `main` is a function that returns `int`, not a non-function
> of type `void` and value `bother, there was a syntax error`.
>
>> int x;
>> if(x)
>> printf("Hello");
>> else
>> printf("World");
>> }
>> What value can be given to x such that the output is "Hello
>> World"?
>
> There isn't one.
>
> If you didn't mean /value/, but /initialiser/, then easy-peasy.


OK, I'll bite.... What initialiser?


You can do:

int x = !printf("Hello ");

[The "solution" I overlooked in my earlier post.]


S'not portable - there's no final newline after the World.

int x = (printf( "Hello, World\n" ), exit( 0 ), 17);

will do fine.

--
Chris "Eyeball" Dollin
"People are part of the design. It's dangerous to forget that." /Star Cops/
May 16 '06 #13
Suman wrote:

Vladimir Oka wrote:
Suman wrote:
> Vladimir Oka wrote:
> > Piyush Agarwal wrote:
>
> [ snip code with UB ]
>
> > > What value can be given to x such that the output is "Hello
> > > World"?
> >
> > I think you misrepresented the trick question.
> >
> > With `x` an `int` it will never work.
>
> #include <stdio.h>
>
> int main(void)
> {
> int x = 0;
> if ( (printf("Hello, "), x) )
> printf("Hello");
> else
> printf("World\n");
> return 0;
> }


I don't think you were allowed to change the `if` statement. Have a
look at my other post as well.


Show me where this constraint was mentioned ;)


Oh, in /that/ case:

int main() { printf( "Hello, World\n" ); }

will do.

--
Chris "Eyeball" Dollin
"People are part of the design. It's dangerous to forget that." /Star Cops/
May 16 '06 #14

Chris Dollin wrote:
Vladimir Oka wrote:

John Devereux wrote:
Chris Dollin <ch**********@hp.com> writes:

> Piyush Agarwal wrote:
>
>> void main
>> {
>
> `main` is a function that returns `int`, not a non-function
> of type `void` and value `bother, there was a syntax error`.
>
>> int x;
>> if(x)
>> printf("Hello");
>> else
>> printf("World");
>> }
>> What value can be given to x such that the output is "Hello
>> World"?
>
> There isn't one.
>
> If you didn't mean /value/, but /initialiser/, then easy-peasy.

OK, I'll bite.... What initialiser?


You can do:

int x = !printf("Hello ");

[The "solution" I overlooked in my earlier post.]


S'not portable - there's no final newline after the World.

int x = (printf( "Hello, World\n" ), exit( 0 ), 17);

will do fine.


Yep. Using 42 instead of 17 would have been even better. ;-)

I was however building on the code I supplied in my original reply to
the OP (which did include the terminating '\n').

May 16 '06 #15
Vladimir Oka wrote:
Chris Dollin wrote:
S'not portable - there's no final newline after the World.

int x = (printf( "Hello, World\n" ), exit( 0 ), 17);

will do fine.


Yep. Using 42 instead of 17 would have been even better. ;-)


No, it wouldn't - I didn't wish to allude to tHGttG. Instead I alluded
to something else.

--
Chris "Veraly, these references are a drag." Dollin
"People are part of the design. It's dangerous to forget that." /Star Cops/
May 16 '06 #16

Chris Dollin wrote:
Vladimir Oka wrote:
Chris Dollin wrote:
S'not portable - there's no final newline after the World.

int x = (printf( "Hello, World\n" ), exit( 0 ), 17);

will do fine.


Yep. Using 42 instead of 17 would have been even better. ;-)


No, it wouldn't - I didn't wish to allude to tHGttG. Instead I alluded
to something else.


You got me there. I dont get the allusion.

[17 *is*, more often than not, my staple "random" value, but I doubt
you know me that well. ;-)]

May 16 '06 #17
Richard Heathfield <in*****@invalid.invalid> writes:
Piyush Agarwal said:
void main
{
int x;
if(x)
printf("Hello");
else
printf("World");
}
What value can be given to x such that the output is "Hello
World"?


The output of the program is undefined for no fewer than three different
reasons. So it might well say "Hello World" as written. Or, of course, it
might not.

Until you understand what the three reasons are, I recommend that you spend
less time with puzzle questions and more time with a good C book.


I see four reasons (one of which is unlikely to cause mere undefined
behavior).

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
May 16 '06 #18
Piyush Agarwal wrote:
void main ^^^^ dead akready: main returns an int {
int x;
if(x) ^^^ dead again: x is not initialized printf("Hello"); ^^^^^^ dead again; a variadic function with no declaration in
scope else
printf("World"); For pre-C99 compilers (and with any decent coding standards even
with C99), dead again without a returned value. }
What value can be given to x such that the output is "Hello
World"?


We answered this already, several times over. Please check the archives
before posting. Although it is not relevant to your question (but is
to your horrid code), check the FAQ before posting as well.

May 16 '06 #19
Keith Thompson said:
I see four reasons (one of which is unlikely to cause mere undefined
behavior).


Believe it or not, the syntax error completely passed me by.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
May 16 '06 #20
Suman wrote:

Vladimir Oka wrote:
Suman wrote:
Vladimir Oka wrote:
> Piyush Agarwal wrote:

[ snip code with UB ]

> > What value can be given to x such that the output is "Hello
> > World"?
>
> I think you misrepresented the trick question.
>
> With `x` an `int` it will never work.
[... snip code with rewritten main() ...]
I don't think you were allowed to change the `if` statement. Have a
look at my other post as well.


Show me where this constraint was mentioned ;)


The original post said:

What value can be given to x such that the output is "Hello World"?

All you're allowed to do is give x a value.

If you're allowed to modify the code, why not just:

#include <stdio.h>
int main(void)
{
printf("Hello World\n");
}

[...]

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:Th*************@gmail.com>
May 16 '06 #21
Vladimir Oka wrote:

Chris Dollin wrote:
Vladimir Oka wrote:
Chris Dollin wrote:
> S'not portable - there's no final newline after the World.
>
> int x = (printf( "Hello, World\n" ), exit( 0 ), 17);
>
> will do fine.

Yep. Using 42 instead of 17 would have been even better. ;-)


No, it wouldn't - I didn't wish to allude to tHGttG. Instead I alluded
to something else.


You got me there. I dont get the allusion.

[17 *is*, more often than not, my staple "random" value, but I doubt
you know me that well. ;-)]


But everyone knows that 17 is the "least random" number. Use 37 instead.

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:Th*************@gmail.com>

May 16 '06 #22
Kenneth Brody opined:
Vladimir Oka wrote:

Chris Dollin wrote:
> Vladimir Oka wrote:
>
> > Chris Dollin wrote:
> >> S'not portable - there's no final newline after the World.
> >>
> >> int x = (printf( "Hello, World\n" ), exit( 0 ), 17);
> >>
> >> will do fine.
> >
> > Yep. Using 42 instead of 17 would have been even better. ;-)
>
> No, it wouldn't - I didn't wish to allude to tHGttG. Instead I
> alluded to something else.


You got me there. I dont get the allusion.

[17 *is*, more often than not, my staple "random" value, but I doubt
you know me that well. ;-)]


But everyone knows that 17 is the "least random" number. Use 37
instead.


Well, occasionally I use 57 instead.

(Probably has something to do with how it sounds in my native language.
Interestingly, although I fully think in English when I use it, I
still always think numbers in my native language. I don't understand
it, really.)

--
Killing is stupid; useless!
-- McCoy, "A Private Little War", stardate 4211.8

<http://clc-wiki.net/wiki/Introduction_to_comp.lang.c>

May 16 '06 #23
Vladimir Oka wrote:
Well, occasionally I use 57 instead.

(Probably has something to do with how it sounds in
my native language.
Interestingly, although I fully think in English when I use it, I
still always think numbers in my native language. I don't understand
it, really.)


Results 1 - 10 of about 76 for "57 card carrying communists".

Results 1 - 10 of about 176,000 for "57 varieties".

http://www.snopes.com/business/hidden/heinz57.asp

--
pete
May 16 '06 #24
Vladimir Oka wrote:
Chris Dollin wrote:
Vladimir Oka wrote:
> Chris Dollin wrote:
>> S'not portable - there's no final newline after the World.
>>
>> int x = (printf( "Hello, World\n" ), exit( 0 ), 17);
>>
>> will do fine.
>
> Yep. Using 42 instead of 17 would have been even better. ;-)


No, it wouldn't - I didn't wish to allude to tHGttG. Instead I alluded
to something else.


You got me there. I dont get the allusion.


There was of course a clue in my signature. Don't worry - the allusion is
obscure; I just wanted a change.

--
Chris "anyway, I don't paint." Dollin
"Everyone generalises from a single example. I know I do." - Vlad Taltos
May 17 '06 #25

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

Similar topics

6
by: Wonjae Lee | last post by:
I read the comment of http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/277753. (Title : Find and replace string in all files in a directory) "perl -p -i -e 's/change this/..to this/g'"...
134
by: Joseph Garvin | last post by:
As someone who learned C first, when I came to Python everytime I read about a new feature it was like, "Whoa! I can do that?!" Slicing, dir(), getattr/setattr, the % operator, all of this was very...
17
by: Eternally | last post by:
Hey folks, I'm writing a simple C++ application which runs continuously. I'm running the program from the command line in Cygwin from XP, and it's cross-compatible with Linux. While running...
29
by: John Rivers | last post by:
Hello, What good reason there is for not allowing methods in ASPX pages I can't imagine, but here is how to get around that limitation: (START) <body MS_POSITIONING="FlowLayout"> <form...
4
by: Boni | last post by:
Dear all, Is there some trick to put controls on a particular control but in other thread. Example: A control is a part of bigger application and application thread freezes somtimes. So a...
2
by: Peter Oliphant | last post by:
Sometimes it's hard to get straight when passing or storing or returning an instance of a class whether you are still dealing with the original object or a copy. For example, the '=' operator used...
15
by: Zhang Weiwu | last post by:
http://www.w3.org/MarkUp/2004/xhtml-faq provided a trick to serve xhtml webpage to IE as application/xml I used that trick and now every one of my xhtml webpage have following first 4 starting...
3
by: DR | last post by:
I heard there is some trick to referencing statics in C# CLR stored procedure without having to mark the assembly as unsafe. Does anyone know this? This is usefull as the case of needing a little...
21
by: array7 | last post by:
I have been reading a lot of archived threads about "page-break- inside: avoid" not supported by IE5, IE6 IE55, IE7 and Firefox (Mozilla), which creates a really unpleasant-looking printout with...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
1
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.