a silent error

Discussion in 'C Programming' started by RoS, Dec 3, 2007.

  1. RoS

    RoS Guest

    what does it say the standard for below program?
    it has to run or not? or it is UB?
    it is better know there is a error or the portability?
    it compile and run with gcc?

    -------------------------------
    #include <stdio.h>

    int main(void)
    {char *p="123";

    printf("jijdid\n", p);
    return 0;
    }
    -------------------------------
     
    RoS, Dec 3, 2007
    #1
    1. Advertising

  2. RoS

    santosh Guest

    RoS wrote:

    > what does it say the standard for below program?
    > it has to run or not? or it is UB?
    > it is better know there is a error or the portability?
    > it compile and run with gcc?
    >
    > -------------------------------
    > #include <stdio.h>
    >
    > int main(void)
    > {char *p="123";
    >
    > printf("jijdid\n", p);
    > return 0;
    > }


    It invokes undefined behaviour.
     
    santosh, Dec 3, 2007
    #2
    1. Advertising

  3. RoS

    RoS Guest

    In data Mon, 03 Dec 2007 17:21:48 +0100, RoS scrisse:

    --------------------
    >#include <stdio.h>
    >
    >int main(void)
    >{char *p="123";
    >
    > printf("jijdid\n", p);
    > return 0;
    >}
    >-------------------------------


    for me not seem an error
     
    RoS, Dec 3, 2007
    #3
  4. RoS

    santosh Guest

    RoS wrote:

    > In data Mon, 03 Dec 2007 17:21:48 +0100, RoS scrisse:
    >


    >>#include <stdio.h>
    >>
    >>int main(void)
    >>{char *p="123";
    >>
    >> printf("jijdid\n", p);
    >> return 0;
    >>}


    >
    > for me not seem an error


    Which is one possible outcome of Undefined behaviour.
     
    santosh, Dec 3, 2007
    #4
  5. santosh <> writes:

    > RoS wrote:
    >
    > > what does it say the standard for below program?
    > > it has to run or not? or it is UB?
    > > it is better know there is a error or the portability?
    > > it compile and run with gcc?
    > >
    > > -------------------------------
    > > #include <stdio.h>
    > >
    > > int main(void)
    > > {char *p="123";
    > >
    > > printf("jijdid\n", p);
    > > return 0;
    > > }

    >
    > It invokes undefined behaviour.


    Where, why?

    --
    Jean-Marc
     
    Jean-Marc Bourguet, Dec 3, 2007
    #5
  6. RoS

    santosh Guest

    Jean-Marc Bourguet wrote:

    > santosh <> writes:
    >
    >> RoS wrote:
    >>
    >> > what does it say the standard for below program?
    >> > it has to run or not? or it is UB?
    >> > it is better know there is a error or the portability?
    >> > it compile and run with gcc?
    >> >
    >> > -------------------------------
    >> > #include <stdio.h>
    >> >
    >> > int main(void)
    >> > {char *p="123";
    >> >
    >> > printf("jijdid\n", p);
    >> > return 0;
    >> > }

    >>
    >> It invokes undefined behaviour.

    >
    > Where, why?


    You are right sorry. Excess arguments after format specifiers are
    processed are evaluated and ignored. So no undefined behaviour.
     
    santosh, Dec 3, 2007
    #6
  7. RoS

    RoS Guest

    In data Mon, 03 Dec 2007 22:28:39 +0530, santosh scrisse:
    >RoS wrote:
    >
    >> In data Mon, 03 Dec 2007 17:21:48 +0100, RoS scrisse:
    >>

    >
    >>>#include <stdio.h>
    >>>
    >>>int main(void)
    >>>{char *p="123";
    >>>
    >>> printf("jijdid\n", p);
    >>> return 0;
    >>>}

    >
    >>
    >> for me not seem an error

    >
    >Which is one possible outcome of Undefined behaviour.


    in the sense i always wrong?

    i post that because i did read something like above in my C code

    i compile it using the current compiler in my pc but i
    fear it could be not compile in some other one compiler
     
    RoS, Dec 3, 2007
    #7
  8. On Mon, 03 Dec 2007 17:23:57 +0100, RoS <> wrote:

    >In data Mon, 03 Dec 2007 17:21:48 +0100, RoS scrisse:
    >
    >--------------------
    >>#include <stdio.h>
    >>
    >>int main(void)
    >>{char *p="123";
    >>
    >> printf("jijdid\n", p);
    >> return 0;
    >>}
    >>-------------------------------

    >
    >for me not seem an error


    It is not an error from a language point of view. The standard
    specifically allows it. However, there are some very popular
    compilers which attempt to match the types of the subsequent arguments
    against the specifications in the format string and will generate a
    diagnostic if a mismatch is detected, as in this case. Furthermore,
    when I see code like this I immediately assume the programmer forgot
    something and wonder what else is missing from the function.


    Remove del for email
     
    Barry Schwarz, Dec 6, 2007
    #8
  9. On Thu, 06 Dec 2007 04:07:36 -0800, Barry Schwarz wrote:
    > On Mon, 03 Dec 2007 17:23:57 +0100, RoS <> wrote:
    >>In data Mon, 03 Dec 2007 17:21:48 +0100, RoS scrisse:
    >>
    >>--------------------
    >>>#include <stdio.h>
    >>>
    >>>int main(void)
    >>>{char *p="123";
    >>>
    >>> printf("jijdid\n", p);
    >>> return 0;
    >>>}
    >>>-------------------------------

    >>
    >>for me not seem an error

    >
    > It is not an error from a language point of view. The standard
    > specifically allows it.


    It almost does, but almost means it doesn't. It allows it in va_arg,
    which may or may not be how printf is implemented. It would be a strange
    implementation where this doesn't work, but it would be a valid one.
     
    Harald van Dijk, Dec 6, 2007
    #9
  10. RoS

    Flash Gordon Guest

    Harald van Dijk wrote, On 06/12/07 20:38:
    > On Thu, 06 Dec 2007 04:07:36 -0800, Barry Schwarz wrote:
    >> On Mon, 03 Dec 2007 17:23:57 +0100, RoS <> wrote:


    <snip>

    >>>> printf("jijdid\n", p);
    >>>> return 0;
    >>>> }
    >>>> -------------------------------
    >>> for me not seem an error

    >> It is not an error from a language point of view. The standard
    >> specifically allows it.

    >
    > It almost does, but almost means it doesn't. It allows it in va_arg,
    > which may or may not be how printf is implemented. It would be a strange
    > implementation where this doesn't work, but it would be a valid one.


    Actually, the specification for fprintf explicitly specified that extra
    parameters are evaluated but otherwise ignored. It is in the first
    paragraph of the description in section 7.19.6.1 of n1256. So Barry was
    correct.
    --
    Flash Gordon
     
    Flash Gordon, Dec 6, 2007
    #10
  11. On Thu, 06 Dec 2007 21:00:19 +0000, Flash Gordon wrote:
    > Harald van Dijk wrote, On 06/12/07 20:38:
    >> On Thu, 06 Dec 2007 04:07:36 -0800, Barry Schwarz wrote:
    >>> On Mon, 03 Dec 2007 17:23:57 +0100, RoS <> wrote:

    >
    > <snip>
    >
    >>>>> printf("jijdid\n", p);
    >>>>> return 0;
    >>>>> }
    >>>>> -------------------------------
    >>>> for me not seem an error
    >>> It is not an error from a language point of view. The standard
    >>> specifically allows it.

    >>
    >> It almost does, but almost means it doesn't. It allows it in va_arg,
    >> which may or may not be how printf is implemented. It would be a
    >> strange implementation where this doesn't work, but it would be a valid
    >> one.

    >
    > Actually, the specification for fprintf explicitly specified that extra
    > parameters are evaluated but otherwise ignored. It is in the first
    > paragraph of the description in section 7.19.6.1 of n1256. So Barry was
    > correct.


    Sorry, you and Barry are right, of course. I was thinking of something
    else.
     
    Harald van Dijk, Dec 6, 2007
    #11
    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. Charles A. Lackman

    Silent Install

    Charles A. Lackman, Feb 18, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    2,092
    Fireface
    Mar 15, 2005
  2. khawar
    Replies:
    2
    Views:
    1,434
    vMike
    Oct 28, 2003
  3. paul brown

    Silent JRE installation 1.4.2_03

    paul brown, Jan 28, 2004, in forum: Java
    Replies:
    0
    Views:
    448
    paul brown
    Jan 28, 2004
  4. RJGraham
    Replies:
    8
    Views:
    10,341
    Ryan Stewart
    Feb 14, 2004
  5. Replies:
    0
    Views:
    269
Loading...

Share This Page