[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c

is it possible?

Tinku

9/1/2008 6:28:00 AM

please forgive me if it is a stupid question because i dont
understand
I saw a question in C -

if (____)
printf("welcome");
else
printf("to C world");

what should be the condition in if(___) for following output

welcome to C world




17 Answers

vippstar

9/1/2008 6:45:00 AM

0

On Sep 1, 9:27 am, Tinku <sumit15...@gmail.com> wrote:
> please forgive me if it is a stupid question because i dont
> understand
> I saw a question in C -
>
> if (____)
> printf("welcome");
> else
> printf("to C world");
>
> what should be the condition in if(___) for following output
>
> welcome to C world

printf("welcome "), 0

Martin Ambuhl

9/1/2008 9:22:00 AM

0

Tinku wrote:

> if (____)
> printf("welcome");
> else
> printf("to C world");

> what should be the condition in if(___) for following output
> welcome to C world

Here's one solution. Note the '\n' needed at the end of the last line
of output.

#include <stdio.h>

int main(void)
{
if (printf("welcome "), 0)
printf("welcome");
else
printf("to C world");
putchar('\n'); /* needed to be sure that there _is_
any output */
return 0;
}

[output]
welcome to C world

August Karlstrom

9/1/2008 10:45:00 AM

0

Tinku wrote:
> please forgive me if it is a stupid question because i dont
> understand
> I saw a question in C -
>
> if (____)
> printf("welcome");
> else
> printf("to C world");
>
> what should be the condition in if(___) for following output
>
> welcome to C world

#define ____ (printf("welcome ") && 0)


August

viza

9/1/2008 10:59:00 AM

0

Hi

On Sun, 31 Aug 2008 23:27:48 -0700, Tinku wrote:

> please forgive me if it is a stupid question because i dont understand
> I saw a question in C -
>
> if (____)
> printf("welcome");
> else
> printf("to C world");
>
> what should be the condition in if(___) for following output
>
> welcome to C world

You have had several answers, all correct, but none very useful, as is to
be expected in comp.lang.c.

In answer to your question, it is not possible make both the if and the
else happen, without using goto or something like that, which can get
confusing.

The answers from vippstar, Martin & August use a comma operator to make
the evaluation of the ___ have a side effect of printing "welcome ", and
then evaluate as false, causing "to C world" to be printed.

Remember that when joining strings together, you usually need to give one
of them a space character where they meet.

HTH
viza

vippstar

9/1/2008 11:54:00 AM

0

On Sep 1, 1:45 pm, August Karlstrom <fusionf...@gmail.com> wrote:
> Tinku wrote:
> > please forgive me if it is a stupid question because i dont
> > understand
> > I saw a question in C -
>
> > if (____)
> > printf("welcome");
> > else
> > printf("to C world");
>
> > what should be the condition in if(___) for following output
>
> > welcome to C world
>
> #define ____ (printf("welcome ") && 0)

Identifiers starting with two underscores are reserved for the
implementation, your code invokes undefined behavior.

vippstar

9/1/2008 11:56:00 AM

0

On Sep 1, 1:58 pm, viza <tom.v...@gm-il.com.obviouschange.invalid>
wrote:
> Hi
>
> On Sun, 31 Aug 2008 23:27:48 -0700, Tinku wrote:
> > please forgive me if it is a stupid question because i dont understand
> > I saw a question in C -
>
> > if (____)
> > printf("welcome");
> > else
> > printf("to C world");
>
> > what should be the condition in if(___) for following output
>
> > welcome to C world
>
> You have had several answers, all correct, but none very useful, as is to
> be expected in comp.lang.c.

s/in comp.lang.c/with such vague specification.

> In answer to your question, it is not possible make both the if and the
> else happen, without using goto or something like that, which can get
> confusing.
>
> The answers from vippstar, Martin & August use a comma operator to make
> the evaluation of the ___ have a side effect of printing "welcome ", and
> then evaluate as false, causing "to C world" to be printed.

August did not use the comma operator.

> Remember that when joining strings together, you usually need to give one
> of them a space character where they meet.

Why?

James Kuyper

9/1/2008 11:59:00 AM

0

viza wrote:
> Hi
>
> On Sun, 31 Aug 2008 23:27:48 -0700, Tinku wrote:
>
>> please forgive me if it is a stupid question because i dont understand
>> I saw a question in C -
>>
>> if (____)
>> printf("welcome");
>> else
>> printf("to C world");
>>
>> what should be the condition in if(___) for following output
>>
>> welcome to C world
>
> You have had several answers, all correct, but none very useful, as is to
> be expected in comp.lang.c.

I think you've made the mistake of interpreting this as a serious
question. It's just a trick question, with a trick solution. "Useful"
wasn't the issue.

Richard Heathfield

9/1/2008 12:06:00 PM

0

vippstar@gmail.com said:

> On Sep 1, 1:58 pm, viza <tom.v...@gm-il.com.obviouschange.invalid>
> wrote:

<snip>

>> Remember that when joining strings together, you usually need to give
>> one of them a space character where they meet.
>
> Why?

#include <stdio.h>

#define foo "this" "is" "why"

int main(void)
{
puts(foo);
return 0;
}

--
Richard Heathfield <http://www.cpax....
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/goog...
"Usenet is a strange place" - dmr 29 July 1999

August Karlstrom

9/1/2008 5:08:00 PM

0

vippstar@gmail.com wrote:
> On Sep 1, 1:45 pm, August Karlstrom <fusionf...@gmail.com> wrote:
>> #define ____ (printf("welcome ") && 0)
>
> Identifiers starting with two underscores are reserved for the
> implementation,

Then the problem may have no solution, depending on how you interpret
the question from the original poster.

> your code invokes undefined behavior.

Which additional options do GCC need in order to tell me this?

Terminal session:

$ cat test.c
#include <stdio.h>

#define ____ (printf("welcome ") && 0)

int main(void)
{
if (____)
printf("welcome");
else
printf("to C world");
puts("");

return 0;
}

$ gcc -ansi -pedantic -Wall test.c
$ ./a.out
welcome to C world


August

Flash Gordon

9/1/2008 6:09:00 PM

0

August Karlstrom wrote, On 01/09/08 18:07:
> vippstar@gmail.com wrote:
>> On Sep 1, 1:45 pm, August Karlstrom <fusionf...@gmail.com> wrote:
>>> #define ____ (printf("welcome ") && 0)
>>
>> Identifiers starting with two underscores are reserved for the
>> implementation,
>
> Then the problem may have no solution, depending on how you interpret
> the question from the original poster.

I would have interpreted it as the ____ being a place-holder. Or even
the original being a printed piece where you use a pen to write your
answer in the place marked by the ____

>> your code invokes undefined behavior.
>
> Which additional options do GCC need in order to tell me this?

<snip>

There is no requirement for a compiler to produce a diagnostic for
undefined behaviour, and in general it is not possible. It would be
possible in this specific case, but I'm not aware of any option to make
gcc warn you about it.
--
Flash Gordon