A trick with if-else

P

Piyush Agarwal

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"?
 
V

Vladimir Oka

Piyush 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"?

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;
}
 
C

Chris Dollin

Piyush said:
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.
 
R

Richard Heathfield

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.
 
K

Kenneth Brody

Piyush 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"?

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:[email protected]>
 
J

John Devereux

Chris Dollin said:
`main` is a function that returns `int`, not a non-function
of type `void` and value `bother, there was a syntax error`.


There isn't one.

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

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

Suman

Vladimir said:
Piyush Agarwal wrote:

[ snip code with UB ]
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;
}
 
V

Vladimir Oka

Suman said:
Vladimir said:
Piyush Agarwal wrote:

[ snip code with UB ]
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.
 
S

Suman

Vladimir said:
Suman said:
Vladimir said:
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.
 
V

Vladimir Oka

Suman said:
Vladimir said:
Suman said:
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) )

;-)
 
C

Chris Dollin

Vladimir said:
John said:
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.
 
C

Chris Dollin

Suman said:
Vladimir said:
Suman said:
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.
 
V

Vladimir Oka

Chris said:
Vladimir said:
John said:
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').
 
C

Chris Dollin

Vladimir said:
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.
 
V

Vladimir Oka

Chris said:
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. ;-)]
 
K

Keith Thompson

Richard Heathfield said:
Piyush Agarwal said:


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).
 
M

Martin Ambuhl

Piyush said:
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.
 
R

Richard Heathfield

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.
 

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,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top