Some odd place to declare

Discussion in 'C Programming' started by Ravi, Jul 18, 2007.

  1. Ravi

    Ravi Guest

    Is this valid in C99:

    int main(void)
    {
    printf("%s\n", char *p = foo());
    return 0;
    }

    char *foo()
    {
    return "abc";
    }
     
    Ravi, Jul 18, 2007
    #1
    1. Advertising

  2. Ravi

    Ian Collins Guest

    Ravi wrote:
    > Is this valid in C99:
    >
    > int main(void)
    > {
    > printf("%s\n", char *p = foo());
    > return 0;
    > }
    >
    > char *foo()
    > {
    > return "abc";
    > }
    >

    No.

    --
    Ian Collins.
     
    Ian Collins, Jul 18, 2007
    #2
    1. Advertising

  3. Ravi

    santosh Guest

    On Wednesday 18 Jul 2007 1:47 pm, in comp.lang.c, Ravi
    <> wrote:
    Message ID: <>

    > Is this valid in C99:
    >
    > int main(void)
    > {
    > printf("%s\n", char *p = foo());
    > return 0;
    > }
    >
    > char *foo()
    > {
    > return "abc";
    > }


    No. It will not compile. Even if you correct it to do so, it still
    invokes undefined behaviour.
     
    santosh, Jul 18, 2007
    #3
  4. In article <f7kne6$6qc$>, santosh <> wrote:
    >On Wednesday 18 Jul 2007 1:47 pm, in comp.lang.c, Ravi
    ><> wrote:
    >Message ID: <>
    >
    >> Is this valid in C99:
    >>
    >> int main(void)
    >> {
    >> printf("%s\n", char *p = foo());
    >> return 0;
    >> }
    >>
    >> char *foo()
    >> {
    >> return "abc";
    >> }

    >
    >No. It will not compile. Even if you correct it to do so, it still
    >invokes undefined behaviour.


    I would imagine that the truth of the later assertion depends very much
    on exactly what specific changes are made in order to get it to compile.
     
    Kenny McCormack, Jul 18, 2007
    #4
  5. Kenny McCormack wrote:
    > In article <f7kne6$6qc$>, santosh <>
    > wrote:
    >>On Wednesday 18 Jul 2007 1:47 pm, in comp.lang.c, Ravi
    >><> wrote:
    >>Message ID: <>
    >>
    >>> Is this valid in C99:
    >>>
    >>> int main(void)
    >>> {
    >>> printf("%s\n", char *p = foo());
    >>> return 0;
    >>> }
    >>>
    >>> char *foo()
    >>> {
    >>> return "abc";
    >>> }

    >>
    >>No. It will not compile. Even if you correct it to do so, it still
    >>invokes undefined behaviour.

    >
    > I would imagine that the truth of the later assertion depends very much
    > on exactly what specific changes are made in order to get it to compile.


    Correct, more so considering the question is about C99, and C99 does not
    allow for implicit function declarations. Using an undeclared identifier,
    such as printf or foo here, is a syntax error in C99. Trying to fix that by
    declaring char *printf(); or something equally silly obviously means the
    behaviour is undefined, but assuming you don't do that, I see no UB.
     
    Harald van =?UTF-8?B?RMSzaw==?=, Jul 18, 2007
    #5
    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. R-D-C

    Odd place to post Netscape query

    R-D-C, Jul 11, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    355
    R-D-C
    Jul 11, 2003
  2. Dan
    Replies:
    0
    Views:
    319
  3. David Veerasingam

    Some more odd behaviour from the Regexp library

    David Veerasingam, Oct 20, 2005, in forum: Python
    Replies:
    4
    Views:
    281
    David Veerasingam
    Oct 20, 2005
  4. Michael Speer

    Odd behavior with odd code

    Michael Speer, Feb 16, 2007, in forum: C Programming
    Replies:
    33
    Views:
    1,101
    Richard Heathfield
    Feb 18, 2007
  5. yoodavid
    Replies:
    64
    Views:
    603
    Tim Rentsch
    Oct 24, 2013
Loading...

Share This Page