Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   A trick with if-else (http://www.velocityreviews.com/forums/t442788-a-trick-with-if-else.html)

Piyush Agarwal 05-16-2006 01:37 PM

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


Vladimir Oka 05-16-2006 01:45 PM

Re: A trick with if-else
 

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


Chris Dollin 05-16-2006 01:46 PM

Re: A trick with if-else
 
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/

Richard Heathfield 05-16-2006 01:48 PM

Re: A trick with if-else
 
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)

Kenneth Brody 05-16-2006 02:13 PM

Re: A trick with if-else
 
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:ThisIsASpamTrap@gmail.com>


John Devereux 05-16-2006 02:15 PM

Re: A trick with if-else
 
Chris Dollin <chris.dollin@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

Suman 05-16-2006 02:18 PM

Re: A trick with if-else
 

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


Vladimir Oka 05-16-2006 02:19 PM

Re: A trick with if-else
 

John Devereux wrote:
> Chris Dollin <chris.dollin@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.]


Vladimir Oka 05-16-2006 02:21 PM

Re: A trick with if-else
 

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.


John Devereux 05-16-2006 02:23 PM

Re: A trick with if-else
 
"Vladimir Oka" <novine@btopenworld.com> writes:

> John Devereux wrote:
>> Chris Dollin <chris.dollin@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


All times are GMT. The time now is 04:37 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.