size limits for string literals

Discussion in 'C Programming' started by copx, Mar 25, 2008.

  1. copx

    copx Guest

    Do the standards say anything about size limits for string literals (min
    size, max size)? I want to know this to make sure that my code is portable.
    The program in question is ANSI C89, but I would also be interested in
    whether or not ISO C99 changed the limits (if any exist).
    copx, Mar 25, 2008
    #1
    1. Advertising

  2. copx

    Richard Bos Guest

    "copx" <> wrote:

    > Do the standards say anything about size limits for string literals (min
    > size, max size)? I want to know this to make sure that my code is portable.
    > The program in question is ANSI C89, but I would also be interested in
    > whether or not ISO C99 changed the limits (if any exist).


    Minima, of course not; an empty string is valid in any language. The
    notional limit on the size of a string literal in The One Program (and
    see discussions of that program in this newsgroup over the years to know
    why all these limits are always slightly misleading) is 509 characters
    in C89, and 4095 in C99. Both of these are valid for normal and wide
    strings, and _after_ concatenation (so you can't get around them with a
    trick like "almost_too_long_string" "another_long_string").

    Richard
    Richard Bos, Mar 25, 2008
    #2
    1. Advertising

  3. copx said:

    > Do the standards say anything about size limits for string literals (min
    > size, max size)?


    Clearly, the minimum size is 1 (because sizeof "" is 1).

    The maximum size of a string literal or wide string literal (after
    concatenation, e.g. from "foo" "bar" to "foobar") that an implementation
    *must* support is 509 in C90. This has been increased to 4095 in C99.

    So if all your string literals are shorter than that, you will not break
    anything. If you make them longer, you're relying on your implementation
    being nice to you.

    <snip>

    --
    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
    Richard Heathfield, Mar 25, 2008
    #3
  4. In article <4all.nl>,
    Richard Bos <> wrote:

    >Minima, of course not; an empty string is valid in any language.


    I believe I've worked with some languages which did not support
    empty strings, but it has been long enough since then that I could
    not name any specifics.
    --
    "What we have to do is to be forever curiously testing new
    opinions and courting new impressions." -- Walter Pater
    Walter Roberson, Mar 25, 2008
    #4
  5. copx

    CBFalconer Guest

    copx wrote:
    >
    > Do the standards say anything about size limits for string literals (min
    > size, max size)? I want to know this to make sure that my code is portable.
    > The program in question is ANSI C89, but I would also be interested in
    > whether or not ISO C99 changed the limits (if any exist).


    Yes they do. I believe the C89/C90/C95 limit is roughly 510 bytes,
    and that C99 expanded it to roughly 1020 bytes. Look in the C
    standard.

    Some useful references about C:
    <http://www.ungerhu.com/jxh/clc.welcome.txt>
    <http://c-faq.com/> (C-faq)
    <http://benpfaff.org/writings/clc/off-topic.html>
    <http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf> (C99)
    <http://cbfalconer.home.att.net/download/n869_txt.bz2> (C99, txt)
    <http://www.dinkumware.com/c99.aspx> (C-library}
    <http://gcc.gnu.org/onlinedocs/> (GNU docs)
    <http://clc-wiki.net/wiki/C_community:comp.lang.c:Introduction>

    --
    [mail]: Chuck F (cbfalconer at maineline dot net)
    [page]: <http://cbfalconer.home.att.net>
    Try the download section.



    --
    Posted via a free Usenet account from http://www.teranews.com
    CBFalconer, Mar 26, 2008
    #5
  6. copx

    copx Guest

    "copx" <> schrieb im Newsbeitrag
    news:fsb6t9$ljl$...
    > Do the standards say anything about size limits for string literals (min
    > size, max size)?

    [snip]

    Thanks everyone!
    copx, Mar 26, 2008
    #6
  7. CBFalconer said:

    > copx wrote:
    >>
    >> Do the standards say anything about size limits for string literals (min
    >> size, max size)? I want to know this to make sure that my code is
    >> portable. The program in question is ANSI C89, but I would also be
    >> interested in whether or not ISO C99 changed the limits (if any exist).

    >
    > Yes they do. I believe the C89/C90/C95 limit is roughly 510 bytes,


    509 - not so far off...

    > and that C99 expanded it to roughly 1020 bytes.


    ....but I don't think 4095 can be plausibly called "roughly 1020".

    --
    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
    Richard Heathfield, Mar 26, 2008
    #7
  8. copx

    CBFalconer Guest

    Richard Heathfield wrote:
    > CBFalconer said:
    >> copx wrote:
    >>>
    >>> Do the standards say anything about size limits for string
    >>> literals (min size, max size)? I want to know this to make sure
    >>> that my code is portable. The program in question is ANSI C89,
    >>> but I would also be interested in whether or not ISO C99 changed
    >>> the limits (if any exist).

    >>
    >> Yes they do. I believe the C89/C90/C95 limit is roughly 510 bytes,

    >
    > 509 - not so far off...
    >
    >> and that C99 expanded it to roughly 1020 bytes.

    >
    > ...but I don't think 4095 can be plausibly called "roughly 1020".


    True. However, if we express the values with logarithms, it is
    only an error by a factor of 2. Trivial. :)

    --
    [mail]: Chuck F (cbfalconer at maineline dot net)
    [page]: <http://cbfalconer.home.att.net>
    Try the download section.



    --
    Posted via a free Usenet account from http://www.teranews.com
    CBFalconer, Mar 26, 2008
    #8
    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. Joe Van Meer
    Replies:
    2
    Views:
    632
    Peter O'Reilly
    May 5, 2004
  2. Andreas Rueckert

    Re: J2ME jar size limits

    Andreas Rueckert, Aug 4, 2003, in forum: Java
    Replies:
    2
    Views:
    2,756
    Tim Tyler
    Aug 4, 2003
  3. Darryl L. Pierce

    Re: J2ME jar size limits

    Darryl L. Pierce, Aug 5, 2003, in forum: Java
    Replies:
    0
    Views:
    594
    Darryl L. Pierce
    Aug 5, 2003
  4. Roedy Green
    Replies:
    0
    Views:
    378
    Roedy Green
    Dec 9, 2003
  5. John Goche
    Replies:
    8
    Views:
    16,456
Loading...

Share This Page