is it possible?

Discussion in 'C Programming' started by Tinku, Sep 1, 2008.

  1. Tinku

    Tinku Guest

    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
     
    Tinku, Sep 1, 2008
    #1
    1. Advertising

  2. Tinku

    Guest

    On Sep 1, 9:27 am, 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


    printf("welcome "), 0
     
    , Sep 1, 2008
    #2
    1. Advertising

  3. Tinku wrote:

    > 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
     
    Martin Ambuhl, Sep 1, 2008
    #3
  4. 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)


    August
     
    August Karlstrom, Sep 1, 2008
    #4
  5. Tinku

    viza Guest

    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.

    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
     
    viza, Sep 1, 2008
    #5
  6. Tinku

    Guest

    On Sep 1, 1:45 pm, August Karlstrom <> wrote:
    > 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.
     
    , Sep 1, 2008
    #6
  7. Tinku

    Guest

    On Sep 1, 1:58 pm, viza <>
    wrote:
    > 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.

    > 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?
     
    , Sep 1, 2008
    #7
  8. Tinku

    James Kuyper Guest

    viza wrote:
    > 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.
     
    James Kuyper, Sep 1, 2008
    #8
  9. wrote:
    > On Sep 1, 1:45 pm, August Karlstrom <> 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.

    > 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
     
    August Karlstrom, Sep 1, 2008
    #9
  10. Tinku

    Flash Gordon Guest

    August Karlstrom wrote, On 01/09/08 18:07:
    > wrote:
    >> On Sep 1, 1:45 pm, August Karlstrom <> 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.


    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 ____

    >> your code invokes undefined behavior.

    >
    > 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.
    --
    Flash Gordon
     
    Flash Gordon, Sep 1, 2008
    #10
  11. On Mon, 01 Sep 2008 19:07:48 +0200, August Karlstrom
    <> wrote:

    > wrote:
    >> On Sep 1, 1:45 pm, August Karlstrom <> 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.
    >
    >> your code invokes undefined behavior.

    >
    >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?

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


    --
    Remove del for email
     
    Barry Schwarz, Sep 1, 2008
    #11
  12. On Mon, 01 Sep 2008 12:14:44 -0700, Barry Schwarz wrote:
    > On Mon, 01 Sep 2008 19:07:48 +0200, August Karlstrom
    > <> wrote:
    >> wrote:
    >>> On Sep 1, 1:45 pm, August Karlstrom <> 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.
    >>
    >>> your code invokes undefined behavior.

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


    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 ____.
     
    Harald van Dijk, Sep 1, 2008
    #12
  13. August Karlstrom <> writes:
    > wrote:
    >> On Sep 1, 1:45 pm, August Karlstrom <> 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.
    >
    >> your code invokes undefined behavior.

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

    --
    Keith Thompson (The_Other_Keith) <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"
     
    Keith Thompson, Sep 1, 2008
    #13
  14. viza <> writes:
    > 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.


    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.

    --
    Keith Thompson (The_Other_Keith) <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"
     
    Keith Thompson, Sep 1, 2008
    #14
  15. On 1 Sep, 07:27, 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


    > if (!printf("welcome "))
    > printf("welcome");
    > else
    > printf("to C world");


    --
    Nick Keighley
     
    Nick Keighley, Sep 2, 2008
    #15
  16. Tinku

    James Kuyper Guest

    August Karlstrom wrote:
    > wrote:
    >> On Sep 1, 1:45 pm, August Karlstrom <> 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.


    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.

    >> your code invokes undefined behavior.

    >
    > 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.
     
    James Kuyper, Sep 2, 2008
    #16
  17. Tinku

    Mohamad Room Guest

    "Tinku" <> wrote in message
    news:...
    > 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.
     
    Mohamad Room, Sep 2, 2008
    #17
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Michael Giagnocavo [MVP]

    Re: Is it possible....

    Michael Giagnocavo [MVP], Jun 28, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    1,214
    Michael Giagnocavo [MVP]
    Jun 28, 2003
  2. Ekkehard Kraemer

    Development on 98 possible at all?

    Ekkehard Kraemer, Sep 13, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    429
    Dino Chiesa [Microsoft]
    Sep 22, 2003
  3. Markus
    Replies:
    1
    Views:
    483
    Markus
    Nov 22, 2005
  4. Replies:
    4
    Views:
    294
    Kenny McCormack
    Feb 21, 2006
  5. Replies:
    10
    Views:
    491
    Chris Gonnerman
    Dec 14, 2007
Loading...

Share This Page