|  | is it possible? |  | |
| | | Tinku |  |
| Posted: Mon Sep 01, 2008 6:27 am Post subject: 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 |
| |
| | | Guest |  |
| Posted: Mon Sep 01, 2008 6:44 am Post subject: Re: is it possible? |  |
On Sep 1, 9:27 am, Tinku <sumit15...@gmail.com> 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
|
printf("welcome "), 0 |
| |
| | | Martin Ambuhl |  |
| Posted: Mon Sep 01, 2008 7:22 am Post subject: Re: is it possible? |  |
Tinku wrote:
| Quote: | 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 |  |
| Posted: Mon Sep 01, 2008 8:45 am Post subject: 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 |  |
| Posted: Mon Sep 01, 2008 8:58 am Post subject: 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 |
| |
| | | James Kuyper |  |
| Posted: Mon Sep 01, 2008 9:58 am Post subject: Re: is it possible? |  |
viza wrote:
| Quote: | 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 |  |
| Posted: Mon Sep 01, 2008 10:05 am Post subject: 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: | 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 |
| |
| | | Guest |  |
| Posted: Mon Sep 01, 2008 11:53 am Post subject: Re: is it possible? |  |
On Sep 1, 1:45 pm, August Karlstrom <fusionf...@gmail.com> wrote:
| Quote: | 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. |
| |
| | | Guest |  |
| Posted: Mon Sep 01, 2008 11:56 am Post subject: 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: 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.
| 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? |
| |
| | | August Karlstrom |  |
| Posted: Mon Sep 01, 2008 3:07 pm Post subject: Re: is it possible? |  |
vippstar@gmail.com wrote:
| Quote: | 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.
| Quote: | 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 |
| |
| Page 1 of 2 .:. Goto page 1, 2 Next | |
|
|