Connecting Tech Pros Worldwide Help | Site Map

is it possible?

  #1  
Old September 1st, 2008, 07:35 AM
Tinku
Guest
 
Posts: n/a
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




  #2  
Old September 1st, 2008, 07:45 AM
vippstar@gmail.com
Guest
 
Posts: n/a

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
  #3  
Old September 1st, 2008, 10:25 AM
Martin Ambuhl
Guest
 
Posts: n/a

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
  #4  
Old September 1st, 2008, 11:55 AM
August Karlstrom
Guest
 
Posts: n/a

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
  #5  
Old September 1st, 2008, 12:05 PM
viza
Guest
 
Posts: n/a

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
  #6  
Old September 1st, 2008, 12:55 PM
vippstar@gmail.com
Guest
 
Posts: n/a

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.
  #7  
Old September 1st, 2008, 01:05 PM
vippstar@gmail.com
Guest
 
Posts: n/a

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?
  #8  
Old September 1st, 2008, 01:05 PM
James Kuyper
Guest
 
Posts: n/a

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.
  #9  
Old September 1st, 2008, 01:05 PM
Richard Heathfield
Guest
 
Posts: n/a

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
  #10  
Old September 2nd, 2008, 12:05 AM
Keith Thompson
Guest
 
Posts: n/a

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"
  #11  
Old September 2nd, 2008, 12:15 PM
Nick Keighley
Guest
 
Posts: n/a

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is it possible to open a popup from a existing popup? tejesht answers 2 August 1st, 2006 01:33 PM
Running JSP page under ASP.NET. Is it possible? Wolfgang answers 3 November 18th, 2005 03:15 PM
Is it possible to override MainWndProc Maxim Kazitov answers 2 November 15th, 2005 08:00 PM
is it possible to find out the caller of a method programmatically 2pac answers 20 November 13th, 2005 06:17 PM
Is it possible to run an exe file (alone) make in vb4 without the need of 'vb40032.dll' ? Raymond H. answers 2 July 17th, 2005 10:10 PM