
September 1st, 2008, 07:35 AM
|
|
|
is it possible?
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
|

September 1st, 2008, 07:45 AM
|
|
|
Re: is it possible?
On Sep 1, 9:27 am, Tinku <sumit15...@gmail.comwrote:
Quote:
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
|

September 1st, 2008, 10:25 AM
|
|
|
Re: is it possible?
Tinku wrote:
Quote:
if (____)
printf("welcome");
else
printf("to C world");
|
Quote:
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
|

September 1st, 2008, 11:55 AM
|
|
|
Re: is it possible?
Tinku wrote:
Quote:
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
|

September 1st, 2008, 12:05 PM
|
|
|
Re: is it possible?
Hi
On Sun, 31 Aug 2008 23:27:48 -0700, Tinku wrote:
Quote:
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
|

September 1st, 2008, 12:55 PM
|
|
|
Re: is it possible?
On Sep 1, 1:45 pm, August Karlstrom <fusionf...@gmail.comwrote:
Quote:
Tinku wrote:
Quote:
please forgive me if it is a stupid question because i dont
understand
I saw a question in C -
|
>
Quote:
if (____)
printf("welcome");
else
printf("to C world");
|
>
Quote:
|
what should be the condition in if(___) for following output
|
>>
#define ____ (printf("welcome ") && 0)
|
Identifiers starting with two underscores are reserved for the
implementation, your code invokes undefined behavior.
|

September 1st, 2008, 01:05 PM
|
|
|
Re: is it possible?
On Sep 1, 1:58 pm, viza <tom.v...@gm-il.com.obviouschange.invalid>
wrote:
Quote:
Hi
>
On Sun, 31 Aug 2008 23:27:48 -0700, Tinku wrote:
Quote:
please forgive me if it is a stupid question because i dont understand
I saw a question in C -
|
>
Quote:
if (____)
printf("welcome");
else
printf("to C world");
|
>
Quote:
|
what should be the condition in if(___) for following output
|
>>
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.
Quote:
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.
Quote:
Remember that when joining strings together, you usually need to give one
of them a space character where they meet.
|
Why?
|

September 1st, 2008, 01:05 PM
|
|
|
Re: is it possible?
viza wrote:
Quote:
Hi
>
On Sun, 31 Aug 2008 23:27:48 -0700, Tinku wrote:
>
Quote:
>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.
|

September 1st, 2008, 01:05 PM
|
|
|
Re: is it possible?
vippstar@gmail.com said:
Quote:
On Sep 1, 1:58 pm, viza <tom.v...@gm-il.com.obviouschange.invalid>
wrote:
|
<snip>
Quote:
Quote:
>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.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
|

September 2nd, 2008, 12:05 AM
|
|
|
Re: is it possible?
viza <tom.viza@gm-il.com.obviouschange.invalidwrites:
Quote:
On Sun, 31 Aug 2008 23:27:48 -0700, Tinku wrote:
Quote:
>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.
|
No useful answer is possible.
Quote:
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 problem statement didn't ask for a way to make both the if and the
else happen; it asked for a way to make the program produce the output
"welcome to C world". The printf("welcome") call was a deliberate red
herring.
[...]
A sufficiently good and clever C programmer should be able to solve
the problem as stated. Note that "good" and "clever" are not
synonymous. A sufficiently clever programmer might use such a trick
in production code; a good programmer never will.
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
|

September 2nd, 2008, 12:15 PM
|
|
|
Re: is it possible?
On 1 Sep, 07:27, Tinku <sumit15...@gmail.comwrote:
Quote:
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
|
Quote:
if (!printf("welcome "))
printf("welcome");
else
printf("to C world");
|
--
Nick Keighley
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|
|
What is Bytes?
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over network members.
|