Connecting Tech Pros Worldwide Help | Site Map

is it possible?

Tinku
Guest
 
Posts: n/a
#1: Sep 1 '08
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




vippstar@gmail.com
Guest
 
Posts: n/a
#2: Sep 1 '08

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
Martin Ambuhl
Guest
 
Posts: n/a
#3: Sep 1 '08

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
August Karlstrom
Guest
 
Posts: n/a
#4: Sep 1 '08

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
viza
Guest
 
Posts: n/a
#5: Sep 1 '08

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
vippstar@gmail.com
Guest
 
Posts: n/a
#6: Sep 1 '08

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
>
Quote:
welcome to C world
>
#define ____ (printf("welcome ") && 0)
Identifiers starting with two underscores are reserved for the
implementation, your code invokes undefined behavior.
vippstar@gmail.com
Guest
 
Posts: n/a
#7: Sep 1 '08

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
>
Quote:
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.
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?
James Kuyper
Guest
 
Posts: n/a
#8: Sep 1 '08

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.
Richard Heathfield
Guest
 
Posts: n/a
#9: Sep 1 '08

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
Keith Thompson
Guest
 
Posts: n/a
#10: Sep 2 '08

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"
Nick Keighley
Guest
 
Posts: n/a
#11: Sep 2 '08

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





Closed Thread