Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old September 1st, 2008, 07:35 AM
Tinku
Guest
 
Posts: n/a
Default 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




  #2  
Old September 1st, 2008, 07:45 AM
vippstar@gmail.com
Guest
 
Posts: n/a
Default 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
Default 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
Default 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
Default 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
Default 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
Default 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
Default 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
Default 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
Default 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
Default 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





 

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

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.
Post your question now . . .
It's fast and it's free

Popular Articles