is it possible?

T

Tinku

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
 
V

vippstar

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
 
M

Martin Ambuhl

Tinku said:
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
 
A

August Karlstrom

Tinku said:
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
 
V

viza

Hi

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
 
V

vippstar

#define ____ (printf("welcome ") && 0)

Identifiers starting with two underscores are reserved for the
implementation, your code invokes undefined behavior.
 
V

vippstar

Hi






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.
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.
Remember that when joining strings together, you usually need to give one
of them a space character where they meet.

Why?
 
J

James Kuyper

viza said:
Hi



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.
 
A

August Karlstrom

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.
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
 
F

Flash Gordon

August Karlstrom wrote, On 01/09/08 18:07:
Then the problem may have no solution, depending on how you interpret
the question from the original poster.

I would have interpreted it as the ____ being a place-holder. Or even
the original being a printed piece where you use a pen to write your
answer in the place marked by the ____
Which additional options do GCC need in order to tell me this?

<snip>

There is no requirement for a compiler to produce a diagnostic for
undefined behaviour, and in general it is not possible. It would be
possible in this specific case, but I'm not aware of any option to make
gcc warn you about it.
 
B

Barry Schwarz

Then the problem may have no solution, depending on how you interpret
the question from the original poster.


Which additional options do GCC need in order to tell me this?

The compiler is not required to diagnose undefined behavior. In this
case it might not be possible.
Terminal session:

$ cat test.c
#include <stdio.h>

#define ____ (printf("welcome ") && 0)

If this line of code was the last line in stdio.h instead of your
source file, the input to the compiler after the preprocessor was
finished would be the same. How is the compiler to distinguish
between your case which is undefined behavior and my case which is
not?
 
H

Harald van Dijk

The compiler is not required to diagnose undefined behavior. In this
case it might not be possible.


If this line of code was the last line in stdio.h instead of your source
file, the input to the compiler after the preprocessor was finished
would be the same.

Not with GCC's preprocessor.
How is the compiler to distinguish between your case
which is undefined behavior and my case which is not?

Even without the preprocessor extensions that allow the compiler to
determine whether the preprocessed output comes from a system header, if
the compiler would give an error on ____, the system headers can be
written in a way that does not use ____.
 
K

Keith Thompson

August Karlstrom said:
Then the problem may have no solution, depending on how you interpret
the question from the original poster.


Which additional options do GCC need in order to tell me this?
[...]

There may well not be any such options. If the behavior is undefined,
then silently accepting the code is perfectly acceptable; gcc is under
no obligation to do anything else. (If there is such an option, you
can find out about it in the gcc documentation or by asking in
gnu.gcc.help.)

In any case, I think the intent was that ___ was placeholder, not an
identifier to be defined as a macro.
 
K

Keith Thompson

viza said:
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.
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.
 
N

Nick Keighley

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
 
J

James Kuyper

August said:
Then the problem may have no solution, depending on how you interpret
the question from the original poster.

I don't see an interpretation that would make me think that use of
#define was called for, much less that it was mandatory. It seemed clear
to me that the underscores were meant to indicate a blank that was to be
filled in, not a C identifier which was to be #defined.
Which additional options do GCC need in order to tell me this?

As far as I know, there is no GCC option which identifies this problem.
Since the behavior is undefined, diagnosis is not required, so this does
not prevent gcc from conforming.

The reason why certain identifiers are reserved for the implementation
is so that that implementation can give those identifiers an
implementation-specific meaning, one that code written by knowledgeable
users might, in some cases, take advantage of. As a result, it might not
even make any sense for the compiler to diagnose it.

I think that a good compiler should diagnose any use of any reserved
identifier that it doesn't attach it's own special meaning to, but
that's a matter of QoI, and I'm not sure whether any real compiler does
this.
 
M

Mohamad Room

Tinku said:
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
if(printf("welcome ") = = 7)
printf("welcome");
else
printf("to C world");


function printf() returns the number of characters printed.
In this case the printf("welcome ") will evalute first and the result is
false ( it should return 8) in order to evalute the statement in else part.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top