Re: Just a bit of silliness

Discussion in 'C Programming' started by Larry Gates, Feb 23, 2009.

  1. Larry Gates

    Larry Gates Guest

    On Sat, 21 Feb 2009 03:41:06 +0000, The Hippy wrote:

    > Just found this on an 8 bit forum and it made me giggle
    >
    > A C Man's Lament
    > ----------------
    >
    > /*
    >
    > The problem I find when I'm looking at lines
    > Of programs all written in C
    > Is that the syntax and grammar resemble the stammer
    > Of a dyslexic demoralized bee.
    >
    > I'll bet any man here (I'll wager a beer)
    > Can't guess how to copy a string.
    > The mess is dramatic, all [ "open square bracket"
    > . & _ ! "dot, ampersand, underscore, pling!"
    >
    > Pointers collected, and thrice indirected,
    > Collated in structs and compiled,
    > When traced by debugger can make coders shudder,
    > And conditionals drive a man wild.
    >
    > I don't wish to seem bitchy, but if only old Ritchie
    > Had been strangled a birth by a Nurse;
    > And the fate that I've planned for all Kernighan's clan
    > Is unprintably several times worse.
    >
    > I find that the pain begins with the main(),
    > The only way out is to hack it.
    > The one bit of syntax that keeps my mind intact
    > Is the very last } "close curly bracket"
    >
    > I hope that this ode is clearer that code
    > I write in that monstrosity.
    > You might think that Pascal's a bit of a rascal,
    > But the ultimate b*d is C. "b*d --> b-a star-d"
    > "bastard"
    >
    > My program is calling (in structure appalling),
    > I must finish my poetic plea.
    > But, let's all face it, use Forth, LISP or BASIC,
    > Whatever you do, don't use C.
    >
    > */
    >
    > (Translator's guide to pronunciation:
    >
    > [ = open square bracket
    > .. = dot
    > & = ampersand
    >
    > _ = underscore
    > ! = pling
    > } = close curly bracket
    > * = a star)
    >
    >
    > I might be using this as a comment in all my programs from now on :)


    Funny stuff. I'd never seen that before. When I started up with C
    seriously a decade ago, my biggest headache was getting things printed out.
    I think the table for printf conversions in K&R2 is a masterpiece in
    incomprehensible brevity. Invariably, I would have to resort to asking on
    usenet, with the attendant spanking machine for asking a question that
    could have been deduced.

    It's such a relief now to use fortran as a first means of development. No
    matter what type x is,
    print *, x
    gives me the whole thing.

    Why couldn't a C compiler "guess" what the type is, in lieu of a format
    specifier?
    --
    larry gates

    If I allowed "next $label" then I'd also have to allow "goto $label",
    and I don't think you really want that... :)
    -- Larry Wall in
    <>
     
    Larry Gates, Feb 23, 2009
    #1
    1. Advertising

  2. Larry Gates

    Bartc Guest

    "Larry Gates" <> wrote in message
    news:1l6mqi2sdha2u$.gn6la4mbcdjj$...


    > It's such a relief now to use fortran as a first means of development. No
    > matter what type x is,
    > print *, x
    > gives me the whole thing.
    >
    > Why couldn't a C compiler "guess" what the type is, in lieu of a format
    > specifier?


    No real reason. I'm sure something like the following could be possible:

    double x;
    int i;
    char c;

    printf("X = %?, I = %?, C = %?\n",x,i,c);

    With the compiler replacing the ?'s in the string literal by a more
    appropriate code, ending up with:

    printf("X = %f, I = %d, C = %c\n",x,i,c);

    --
    Bartc
     
    Bartc, Feb 23, 2009
    #2
    1. Advertising

  3. Larry Gates

    user923005 Guest

    On Feb 23, 2:47 pm, "Bartc" <> wrote:
    > "Larry Gates" <> wrote in message
    >
    > news:1l6mqi2sdha2u$.gn6la4mbcdjj$...
    >
    > > It's such a relief now to use fortran as a first means of development.  No
    > > matter what type x is,
    > > print *, x
    > > gives me the whole thing.

    >
    > > Why couldn't a C compiler "guess" what the type is, in lieu of a format
    > > specifier?

    >
    > No real reason. I'm sure something like the following could be possible:
    >
    > double x;
    > int i;
    > char c;
    >
    > printf("X = %?, I = %?, C = %?\n",x,i,c);
    >
    > With the compiler replacing the ?'s in the string literal by a more
    > appropriate code, ending up with:
    >
    > printf("X = %f, I = %d, C = %c\n",x,i,c);


    What if you wanted to print the contents of void pointers?
     
    user923005, Feb 23, 2009
    #3
  4. Larry Gates

    Bartc Guest

    user923005 wrote:
    > On Feb 23, 2:47 pm, "Bartc" <> wrote:
    >> "Larry Gates" <> wrote in message
    >> news:1l6mqi2sdha2u$.gn6la4mbcdjj$...
    >>
    >>> It's such a relief now to use fortran as a first means of
    >>> development. No matter what type x is,
    >>> print *, x
    >>> gives me the whole thing.

    >>
    >>> Why couldn't a C compiler "guess" what the type is, in lieu of a
    >>> format specifier?

    >>
    >> No real reason. I'm sure something like the following could be
    >> possible:
    >>
    >> double x;
    >> int i;
    >> char c;
    >> printf("X = %?, I = %?, C = %?\n",x,i,c);
    >>
    >> With the compiler replacing the ?'s in the string literal by a more
    >> appropriate code, ending up with:
    >>
    >> printf("X = %f, I = %d, C = %c\n",x,i,c);

    >
    > What if you wanted to print the contents of void pointers?


    How would you dereference such a pointer anyway?

    --
    Bartc
     
    Bartc, Feb 24, 2009
    #4
  5. Larry Gates

    CBFalconer Guest

    user923005 wrote:
    > "Bartc" <> wrote:
    >> "Larry Gates" <> wrote:
    >>

    .... snip ...
    >>
    >>> Why couldn't a C compiler "guess" what the type is, in lieu of
    >>> a format specifier?

    >>
    >> No real reason. I'm sure something like the following could be
    >> possible:
    >>
    >> double x;
    >> int i;
    >> char c;
    >>
    >> printf("X = %?, I = %?, C = %?\n",x,i,c);
    >>
    >> With the compiler replacing the ?'s in the string literal by a
    >> more appropriate code, ending up with:
    >>
    >> printf("X = %f, I = %d, C = %c\n",x,i,c);

    >
    > What if you wanted to print the contents of void pointers?


    Use %p. Note that you have to cast those pointers to void*.
    However you can't do that %? game. The format string need not be
    known at compile time.

    --
    [mail]: Chuck F (cbfalconer at maineline dot net)
    [page]: <http://cbfalconer.home.att.net>
    Try the download section.
     
    CBFalconer, Feb 24, 2009
    #5
  6. Larry Gates

    user923005 Guest

    On Feb 23, 4:05 pm, "Bartc" <> wrote:
    > user923005 wrote:
    > > On Feb 23, 2:47 pm, "Bartc" <> wrote:
    > >> "Larry Gates" <> wrote in message
    > >>news:1l6mqi2sdha2u$.gn6la4mbcdjj$...

    >
    > >>> It's such a relief now to use fortran as a first means of
    > >>> development. No matter what type x is,
    > >>> print *, x
    > >>> gives me the whole thing.

    >
    > >>> Why couldn't a C compiler "guess" what the type is, in lieu of a
    > >>> format specifier?

    >
    > >> No real reason. I'm sure something like the following could be
    > >> possible:

    >
    > >> double x;
    > >> int i;
    > >> char c;
    > >> printf("X = %?, I = %?, C = %?\n",x,i,c);

    >
    > >> With the compiler replacing the ?'s in the string literal by a more
    > >> appropriate code, ending up with:

    >
    > >> printf("X = %f, I = %d, C = %c\n",x,i,c);

    >
    > > What if you wanted to print the contents of void pointers?

    >
    > How would you dereference such a pointer anyway?


    Good point, I guess they would have to cast to the actual type anyway.
     
    user923005, Feb 24, 2009
    #6
  7. CBFalconer <> writes:
    > user923005 wrote:
    >> "Bartc" <> wrote:
    >>> "Larry Gates" <> wrote:
    >>>

    > ... snip ...
    >>>
    >>>> Why couldn't a C compiler "guess" what the type is, in lieu of
    >>>> a format specifier?
    >>>
    >>> No real reason. I'm sure something like the following could be
    >>> possible:
    >>>
    >>> double x;
    >>> int i;
    >>> char c;
    >>>
    >>> printf("X = %?, I = %?, C = %?\n",x,i,c);
    >>>
    >>> With the compiler replacing the ?'s in the string literal by a
    >>> more appropriate code, ending up with:
    >>>
    >>> printf("X = %f, I = %d, C = %c\n",x,i,c);

    >>
    >> What if you wanted to print the contents of void pointers?

    >
    > Use %p. Note that you have to cast those pointers to void*.
    > However you can't do that %? game. The format string need not be
    > known at compile time.


    But in principle, a compiler could recognize "%?" only if the format
    string *is* known at compile time. Since an invalid conversion
    specifier such as "%?" invokes undefined behavior, doing some
    compile-time magic to substitute an appropriate specifier for the
    actual argument type is one of the infinitely many things the compiler
    is allowed to do.

    In the implementation I have in mind, the printf function itself would
    never see the "%?"; instead it would see "%f", "%d", and "%c".

    Another possibility is that code could be generated that makes the
    actual type information visible to printf at execution time; then "%?"
    would work with format strings that aren't known at compile time.

    Either of these would be a valid extension, as permitted by C99 4p6.

    I don't know of any C implementation that does this kind of thing.

    --
    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, Feb 24, 2009
    #7
  8. Larry Gates

    CBFalconer Guest

    Keith Thompson wrote:
    > CBFalconer <> writes:
    >> user923005 wrote:
    >>> "Bartc" <> wrote:
    >>>

    .... snip ...
    >>>
    >>>> double x;
    >>>> int i;
    >>>> char c;
    >>>>
    >>>> printf("X = %?, I = %?, C = %?\n",x,i,c);
    >>>>
    >>>> With the compiler replacing the ?'s in the string literal by a
    >>>> more appropriate code, ending up with:
    >>>>
    >>>> printf("X = %f, I = %d, C = %c\n",x,i,c);
    >>>
    >>> What if you wanted to print the contents of void pointers?

    >>
    >> Use %p. Note that you have to cast those pointers to void*.
    >> However you can't do that %? game. The format string need not be
    >> known at compile time.

    >
    > But in principle, a compiler could recognize "%?" only if the
    > format string *is* known at compile time. Since an invalid
    > conversion specifier such as "%?" invokes undefined behavior,
    > doing some compile-time magic to substitute an appropriate
    > specifier for the actual argument type is one of the infinitely
    > many things the compiler is allowed to do.
    >
    > In the implementation I have in mind, the printf function itself
    > would never see the "%?"; instead it would see "%f", "%d", and "%c".


    True. In this case the compiler would scan the argument list (in
    the call) and record the types of each, after applying the needed
    default conversions. Then it scans the fixed format string and
    replaces the appropriate '?'s. The advantage here is that the
    library needs no attention, and no extra run time is spent. The
    disadvantage is that such a feature is usable only with a fixed
    format, not with one generated at run time. This would cause many
    newbie errors.

    >
    > Another possibility is that code could be generated that makes the
    > actual type information visible to printf at execution time; then "%?"
    > would work with format strings that aren't known at compile time.


    I wouldn't approve of such. It seems to require running all sorts
    of hidden code.

    --
    [mail]: Chuck F (cbfalconer at maineline dot net)
    [page]: <http://cbfalconer.home.att.net>
    Try the download section.
     
    CBFalconer, Feb 24, 2009
    #8
  9. In article <fIFol.39091$> "Bartc" <> writes:
    ....
    > No real reason. I'm sure something like the following could be possible:
    >
    > double x;
    > int i;
    > char c;
    >
    > printf("X = %?, I = %?, C = %?\n",x,i,c);
    >
    > With the compiler replacing the ?'s in the string literal by a more
    > appropriate code, ending up with:
    >
    > printf("X = %f, I = %d, C = %c\n",x,i,c);


    How about:
    double x;
    int i;
    char c;
    char format[] = "X = %?, I = %?, C = %?\n";
    printf(format, x, i, c);
    printf(format, c, x, i);
    ?

    It came from the Fortran:
    print *, x
    which is essentially unformatted i/o. Because in Fortran print is not a
    function (nor a reserved keyword), the compiler has to translate it as
    "print x unformatted" and issues instructions to obtain that goal. The
    actual code will depend on the type of x (*). For formatted i/o Fortran has
    also no solution. And as unformatted i/o in text does not fit in the
    model of C, all odds are off. Algol 68 did provide it, but it was quite
    elaborate and would not fit in the C model at all. (It involved the union
    of all possible types and the ability to determine the actual type from the
    union value given.)
    --
    (*) For
    print *, x, i, y
    the compiler would possibly emit code similar to:
    call startio(standardoutput)
    call printreal x
    call printinteger i
    call printreal y
    call endio
    I have seen such code.
    --
    dik t. winter, cwi, science park 123, 1098 xg amsterdam, nederland, +31205924131
    home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
     
    Dik T. Winter, Feb 24, 2009
    #9
  10. Larry Gates

    Larry Gates Guest

    On Mon, 23 Feb 2009 20:48:39 -0500, CBFalconer wrote:

    > Keith Thompson wrote:
    >> CBFalconer <> writes:
    >>> user923005 wrote:
    >>>> "Bartc" <> wrote:
    >>>>

    > ... snip ...
    >>>>
    >>>>> double x;
    >>>>> int i;
    >>>>> char c;
    >>>>>
    >>>>> printf("X = %?, I = %?, C = %?\n",x,i,c);
    >>>>>
    >>>>> With the compiler replacing the ?'s in the string literal by a
    >>>>> more appropriate code, ending up with:
    >>>>>
    >>>>> printf("X = %f, I = %d, C = %c\n",x,i,c);
    >>>>
    >>>> What if you wanted to print the contents of void pointers?
    >>>
    >>> Use %p. Note that you have to cast those pointers to void*.
    >>> However you can't do that %? game. The format string need not be
    >>> known at compile time.

    >>
    >> But in principle, a compiler could recognize "%?" only if the
    >> format string *is* known at compile time. Since an invalid
    >> conversion specifier such as "%?" invokes undefined behavior,
    >> doing some compile-time magic to substitute an appropriate
    >> specifier for the actual argument type is one of the infinitely
    >> many things the compiler is allowed to do.
    >>
    >> In the implementation I have in mind, the printf function itself
    >> would never see the "%?"; instead it would see "%f", "%d", and "%c".

    >
    > True. In this case the compiler would scan the argument list (in
    > the call) and record the types of each, after applying the needed
    > default conversions. Then it scans the fixed format string and
    > replaces the appropriate '?'s. The advantage here is that the
    > library needs no attention, and no extra run time is spent. The
    > disadvantage is that such a feature is usable only with a fixed
    > format, not with one generated at run time. This would cause many
    > newbie errors.
    >
    >>
    >> Another possibility is that code could be generated that makes the
    >> actual type information visible to printf at execution time; then "%?"
    >> would work with format strings that aren't known at compile time.

    >
    > I wouldn't approve of such. It seems to require running all sorts
    > of hidden code.


    I see a possibility here.

    I can run C's preprocessor in fortran. I can do so with standard fortran
    and then interoperate with C.

    The good part of this is that the fortran guys know C better than, well,
    Barack Obama, Lindsay Graham, and the last fella he tap-danced with in the
    john.
    --
    larry gates

    Of course, if we make the MMD rules sufficiently complicated, we'll just
    have
    to make the warning spit out a spreadsheet to show the calculations. Then
    we
    hide all that behind an interview process, just like all our wonderful tax
    preparation software... -- Larry Wall in <>
     
    Larry Gates, Feb 24, 2009
    #10
  11. Larry Gates

    Guest

    On Feb 23, 3:49 pm, Larry Gates <> wrote:
    > On Sat, 21 Feb 2009 03:41:06 +0000, The Hippy wrote:
    > > Just found this on an 8 bit forum and it made me giggle

    >
    > > A C Man's Lament
    > > ----------------

    >
    > > /*

    >
    > > The problem I find when I'm looking at lines
    > > Of programs all written in C
    > > Is that the syntax and grammar resemble the stammer
    > > Of a dyslexic demoralized bee.

    >
    > > I'll bet any man here (I'll wager a beer)
    > > Can't guess how to copy a string.
    > > The mess is dramatic, all [ "open square bracket"
    > > . & _ ! "dot, ampersand, underscore, pling!"

    >
    > > Pointers collected, and thrice indirected,
    > > Collated in structs and compiled,
    > > When traced by debugger can make coders shudder,
    > > And conditionals drive a man wild.

    >
    > > I don't wish to seem bitchy, but if only old Ritchie
    > > Had been strangled a birth by a Nurse;
    > > And the fate that I've planned for all Kernighan's clan
    > > Is unprintably several times worse.

    >
    > > I find that the pain begins with the main(),
    > > The only way out is to hack it.
    > > The one bit of syntax that keeps my mind intact
    > > Is the very last } "close curly bracket"

    >
    > > I hope that this ode is clearer that code
    > > I write in that monstrosity.
    > > You might think that Pascal's a bit of a rascal,
    > > But the ultimate b*d is C. "b*d --> b-a star-d"
    > > "bastard"

    >
    > > My program is calling (in structure appalling),
    > > I must finish my poetic plea.
    > > But, let's all face it, use Forth, LISP or BASIC,
    > > Whatever you do, don't use C.

    >
    > > */

    >
    > > (Translator's guide to pronunciation:

    >
    > > [ = open square bracket
    > > .. = dot
    > > & = ampersand

    >
    > > _ = underscore
    > > ! = pling
    > > } = close curly bracket
    > > * = a star)

    >
    > > I might be using this as a comment in all my programs from now on :)

    >
    > Funny stuff. I'd never seen that before.  When I started up with C
    > seriously a decade ago, my biggest headache was getting things printed out.
    > I think the table for printf conversions in K&R2 is a masterpiece in
    > incomprehensible brevity.  Invariably, I would have to resort to asking on
    > usenet, with the attendant spanking machine for asking a question that
    > could have been deduced.
    >
    > It's such a relief now to use fortran as a first means of development.  No
    > matter what type x is,
    > print *, x
    > gives me the whole thing.
    >
    > Why couldn't a C compiler "guess" what the type is, in lieu of a format
    > specifier?
    > --
    > larry gates
    >
    > If I allowed "next $label" then I'd also have to allow "goto $label",
    > and I don't think you really want that...  :)
    >              -- Larry Wall in
    > <>


    What you appear to be wishing for is overloading.
    The feature that almost makes C++ worthwhile.

    The tradeoff is that the overall interface actually becomes
    more complicated. You could accomplish a similar effect
    with a few print-type(type x) functions to hide the
    format strings. This is probably more useful for structures
    than basic types.
     
    , Feb 24, 2009
    #11
  12. Larry Gates

    Larry Gates Guest

    On Tue, 24 Feb 2009 02:42:04 GMT, Dik T. Winter wrote:

    > In article <fIFol.39091$> "Bartc" <> writes:
    > ...
    > > No real reason. I'm sure something like the following could be possible:
    > >
    > > double x;
    > > int i;
    > > char c;
    > >
    > > printf("X = %?, I = %?, C = %?\n",x,i,c);
    > >
    > > With the compiler replacing the ?'s in the string literal by a more
    > > appropriate code, ending up with:
    > >
    > > printf("X = %f, I = %d, C = %c\n",x,i,c);

    >
    > How about:
    > double x;
    > int i;
    > char c;
    > char format[] = "X = %?, I = %?, C = %?\n";
    > printf(format, x, i, c);
    > printf(format, c, x, i);
    > ?
    >
    > It came from the Fortran:
    > print *, x
    > which is essentially unformatted i/o. Because in Fortran print is not a
    > function (nor a reserved keyword), the compiler has to translate it as
    > "print x unformatted" and issues instructions to obtain that goal. The
    > actual code will depend on the type of x (*).


    x(*) is a little ambiguous. He means * as nota beta.

    > For formatted i/o Fortran has
    > also no solution. And as unformatted i/o in text does not fit in the
    > model of C, all odds are off. Algol 68 did provide it, but it was quite
    > elaborate and would not fit in the C model at all. (It involved the union
    > of all possible types and the ability to determine the actual type from the
    > union value given.)
    > --
    > (*) For
    > print *, x, i, y
    > the compiler would possibly emit code similar to:
    > call startio(standardoutput)
    > call printreal x
    > call printinteger i
    > call printreal y
    > call endio
    > I have seen such code.


    The frontiers of fortran are coded in C right now. If instead, you chose ë
    as a unique identifier from a C point of view.

    Then for the

    > > printf("X = %?, I = %?, C = %?\n",x,i,c);


    , then ë could contain a format statement that was either
    comparable/non-comparable to C. For the comparable subset, which is 85%
    +, there could be interoperation.

    --
    larry gates

    Does the same as the system call of that name.
    If you don't know what it does, don't worry about it.
    -- Larry Wall in the perl man page regarding chroot(2)
     
    Larry Gates, Feb 24, 2009
    #12
  13. Larry Gates

    Phil Carmody Guest

    Larry Gates <> writes:
    > The frontiers of fortran are coded in C right now. If instead, you chose ë
    > as a unique identifier from a C point of view.
    >
    > Then for the
    >
    >> > printf("X = %?, I = %?, C = %?\n",x,i,c);

    >
    > , then ë could contain a format statement that was either
    > comparable/non-comparable to C. For the comparable subset, which is 85%
    > +, there could be interoperation.


    You started making very little sense, but with each post you
    seem to make less and less sense. I suggest you learn both a
    little C and a little English. I regret wasting the time I
    spent trying to decipher your earlier gibberings now.

    Phil
    --
    I tried the Vista speech recognition by running the tutorial. I was
    amazed, it was awesome, recognised every word I said. Then I said the
    wrong word ... and it typed the right one. It was actually just
    detecting a sound and printing the expected word! -- pbhj on /.
     
    Phil Carmody, Feb 24, 2009
    #13
  14. Larry Gates

    Bartc Guest

    "Dik T. Winter" <> wrote in message
    news:...
    > In article <fIFol.39091$> "Bartc"
    > <> writes:
    > ...
    > > No real reason. I'm sure something like the following could be possible:
    > >
    > > double x;
    > > int i;
    > > char c;
    > >
    > > printf("X = %?, I = %?, C = %?\n",x,i,c);
    > >
    > > With the compiler replacing the ?'s in the string literal by a more
    > > appropriate code, ending up with:
    > >
    > > printf("X = %f, I = %d, C = %c\n",x,i,c);

    >
    > How about:
    > double x;
    > int i;
    > char c;
    > char format[] = "X = %?, I = %?, C = %?\n";
    > printf(format, x, i, c);
    > printf(format, c, x, i);
    > ?


    I don't know; it depends whether the compiler is confident that format[] is
    an immutable string literal. As written it isn't, but perhaps with a const
    in there..

    But the idea is just to help out with (possibly) the majority of printf
    calls that do use a string constant:

    #define printme(x) printf ( #x " = %?\n",x)
    ....
    printme(x);

    > (*) For
    > print *, x, i, y
    > the compiler would possibly emit code similar to:
    > call startio(standardoutput)
    > call printreal x
    > call printinteger i
    > call printreal y
    > call endio
    > I have seen such code.


    So? That's exactly what you would expect. It's printf() that's odd if you're
    coming from outside of C.

    --
    Bartc
     
    Bartc, Feb 24, 2009
    #14
  15. On Feb 24, 11:10 am, "Bartc" <> wrote:
    > "Dik T. Winter" <> wrote in messagenews:...
    >
    >
    >
    > > In article <fIFol.39091$> "Bartc"
    > > <> writes:
    > > ...
    > > > No real reason. I'm sure something like the following could be possible:

    >
    > > > double x;
    > > > int i;
    > > > char c;

    >
    > > > printf("X = %?, I = %?, C = %?\n",x,i,c);

    >
    > > > With the compiler replacing the ?'s in the string literal by a more
    > > > appropriate code, ending up with:

    >
    > > > printf("X = %f, I = %d, C = %c\n",x,i,c);

    >
    > > How about:
    > >   double x;
    > >   int i;
    > >   char c;
    > >   char format[] = "X = %?, I = %?, C = %?\n";
    > >   printf(format, x, i, c);
    > >   printf(format, c, x, i);
    > > ?

    >
    > I don't know; it depends whether the compiler is confident that format[] is
    > an immutable string literal. As written it isn't, but perhaps with a const
    > in there..
    >
    > But the idea is just to help out with (possibly) the majority of printf
    > calls that do use a string constant:


    In my code I think 100% of printf calls use a string constant.
    Is there ANY plausible and defensible case for non-constant format
    strings?
    I cannot presently think of this. It becomes harder when one has
    custom
    functions that wrap around printf and its relatives - i.e. they
    receive
    something else than a string constant and use va_start/va_end to pass
    arguments
    to another variadic function such as printf. For that, gcc provides a
    facility
    to annotate the wrapper function announcing that it accepts arguments
    identical
    for example to the printf family:

    #ifdef __GNUC__
    __attribute__ ((format (printf, 2, 3)))
    #endif

    Also, gcc provides the -Wformat flag (part of the -Wall settings)
    which makes
    it check printf format and argument consistency.

    Stijn
     
    stijnvandongen, Feb 24, 2009
    #15
  16. In article <ZAQol.39230$> "Bartc" <> writes:
    ....
    > > (*) For
    > > print *, x, i, y
    > > the compiler would possibly emit code similar to:
    > > call startio(standardoutput)
    > > call printreal x
    > > call printinteger i
    > > call printreal y
    > > call endio
    > > I have seen such code.

    >
    > So? That's exactly what you would expect. It's printf() that's odd if you're
    > coming from outside of C.


    Eh? I come from Algol 60/Algol 68, I do not think it odd at all.
    --
    dik t. winter, cwi, science park 123, 1098 xg amsterdam, nederland, +31205924131
    home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
     
    Dik T. Winter, Feb 24, 2009
    #16
  17. Larry Gates

    Bartc Guest

    "Dik T. Winter" <> wrote in message
    news:...
    > In article <ZAQol.39230$> "Bartc"
    > <> writes:
    > ...
    > > > (*) For
    > > > print *, x, i, y
    > > > the compiler would possibly emit code similar to:
    > > > call startio(standardoutput)
    > > > call printreal x
    > > > call printinteger i
    > > > call printreal y
    > > > call endio
    > > > I have seen such code.

    > >
    > > So? That's exactly what you would expect. It's printf() that's odd if
    > > you're
    > > coming from outside of C.

    >
    > Eh? I come from Algol 60/Algol 68, I do not think it odd at all.


    I can't exactly remember but I'm fairly sure that in Algol 60, although I/O
    was performed by functions, it was not necessary to tell it the types of the
    values being output.

    C also doesn't have print statements built-in; it uses the library function
    printf(), but in this case, although variables and values all carry type
    attributes (and the compiler knows full well what they are), it's necessary
    for the programmer to duplicate this information for the benefit of printf.


    --
    Bartc
     
    Bartc, Feb 24, 2009
    #17
  18. In article <djSol.39272$> "Bartc" <> writes:
    > "Dik T. Winter" <> wrote in message
    > news:...

    ....
    > > Eh? I come from Algol 60/Algol 68, I do not think it odd at all.

    >
    > I can't exactly remember but I'm fairly sure that in Algol 60, although I/O
    > was performed by functions, it was not necessary to tell it the types of the
    > values being output.


    Algol 60 proper had no I/O defined at all, so every implementation had its
    own method. However, quite some time later (even after the coming of
    Algol 68), Knuth c.s. came with a new, adapted standard of Algol 60 that
    included I/O. Completely in the style of C as it is now.

    > C also doesn't have print statements built-in; it uses the library function
    > printf(), but in this case, although variables and values all carry type
    > attributes (and the compiler knows full well what they are), it's necessary
    > for the programmer to duplicate this information for the benefit of printf.


    Indeed, because the standard does not guarantee that that information is
    available to the called function. However, exactly the same was true in
    the case of the revised standard of Algol 60, that is, the standard did
    *not* state that that information was available within the called function.
    --
    dik t. winter, cwi, science park 123, 1098 xg amsterdam, nederland, +31205924131
    home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
     
    Dik T. Winter, Feb 24, 2009
    #18
  19. stijnvandongen <> writes:

    > In my code I think 100% of printf calls use a string constant. Is
    > there ANY plausible and defensible case for non-constant format
    > strings? I cannot presently think of this.


    Support for multiple (human) languages.

    printf(english ? "There are %d apples" : "Hay %d manzanas", num_apples);

    Imagine the selection buried in another function and it becomes
    impossible for the compiler to check. Look up "gettext" for a more
    elaborate system. An example might look like

    printf(_("There are %d apples"), num_apples);

    Here _() is a function or macro which looks up the given string in a
    translation file and returns the equivalent in the pre-specified
    language. If the language is set to Spanish, _("There are %d apples")
    would return "Hay %d manzanas". If it's set to French, it would return
    "Il y a %d pommes", and so on.
     
    Nate Eldredge, Feb 24, 2009
    #19
  20. On Feb 24, 4:26 pm, Nate Eldredge <> wrote:
    > stijnvandongen <> writes:
    > > In my code I think 100% of printf calls use a string constant.  Is
    > > there ANY plausible and defensible case for non-constant format
    > > strings?  I cannot presently think of this.

    >
    > Support for multiple (human) languages.
    >
    > printf(english ? "There are %d apples" : "Hay %d manzanas", num_apples);
    >
    > Imagine the selection buried in another function and it becomes
    > impossible for the compiler to check.  Look up "gettext" for a more
    > elaborate system.  An example might look like
    >
    > printf(_("There are %d apples"), num_apples);
    >
    > Here _() is a function or macro which looks up the given string in a
    > translation file and returns the equivalent in the pre-specified
    > language.  If the language is set to Spanish, _("There are %d apples")
    > would return "Hay %d manzanas".  If it's set to French, it would return
    > "Il y a %d pommes", and so on.


    OK. It would be doable in the gcc framework by having a function
    iprintf(FILE*, fmt, ...), annotating iprintf with the attributes
    supported by gcc, and issuing _(fmt) in the definition of iprintf,
    right?

    Stijn
     
    stijnvandongen, Feb 24, 2009
    #20
    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. Luc The Perverse

    Avoiding Silliness

    Luc The Perverse, Jun 23, 2006, in forum: Java
    Replies:
    9
    Views:
    449
    Luc The Perverse
    Jun 28, 2006
  2. walterbyrd
    Replies:
    1
    Views:
    379
    Damjan
    Apr 10, 2006
  3. Replies:
    3
    Views:
    1,809
    Timothy Bendfelt
    Jan 19, 2007
  4. Replies:
    9
    Views:
    1,014
    Juha Nieminen
    Aug 22, 2007
  5. Jeff.M
    Replies:
    6
    Views:
    188
    Lasse Reichstein Nielsen
    May 4, 2009
Loading...

Share This Page