extending printf

Discussion in 'C Programming' started by jacob navia, Mar 8, 2014.

  1. jacob navia

    BartC Guest

    Yes, %?? would tick all the boxes (apart from not being able to use just %?
    in the majority of cases).

    In general, you might have a variable with a typedef-ed type like this:

    T a;

    where the only thing you know about T is that it's numeric (and might
    further know that it's an integer type). And want to quickly print the value
    of it:

    printf("A = % ....\n", a);

    In this case, you don't necessarily know if a is signed or not. You can find
    out, but the idea is to avoid going to that effort. And also if T changes,
    or this code fragment gets pasted elsewhere where T might be different, then
    you want to avoid having to update all these format specifiers.

    Actually the same applies even when T isn't a typedef; one day it might just
    change from int to long long int; at ordinary warning levels, you will not
    be aware anything is amiss.

    Of course in the case of the format string not being a constant, then you
    can't really use this, unless somehow the compiler can impart type
    information to the printf handler, but that would be a bigger, more
    disruptive change to how printf works.

    (My point of view is from writing this stuff outside of C where you might
    just do 'println a'; in general you don't need to specify anything at all,
    unless you want extra control over the formatting, but even then you don't
    normally need to say what the type of the variable is - the compiler knows
    that already, and better than you!)
     
    BartC, Mar 9, 2014
    #21
    1. Advertisements

  2. %zx of course.
     
    Ben Bacarisse, Mar 9, 2014
    #22
    1. Advertisements

  3. %?? would collide too easily with trigraphs.
     
    Keith Thompson, Mar 9, 2014
    #23
  4. Good point -- it's a problem just waiting to happen (but then that's
    trigraphs for you). The second ? should probably be something like #.
     
    Ben Bacarisse, Mar 9, 2014
    #24
  5. printf doesn't know the types of the arguments, so the compiler
    has to match the types with the corresponding print functions.
    (Hence the need for #pragma rather than a library function like
    SetNewPrintfFormat.)

    However, I doubt if this idea can implemented so that it works in all cases:

    #pragma specifier (Point*, printPoint)
    #pragma specifier (Shape*, printShape)

    void *p;
    if (rand() % 2)
    p = &point;
    else
    p = &shape;
    printf("%?\n", p);

    Now how can the compiler figure out at compile time which function to use?
     
    Seungbeom Kim, Apr 15, 2014
    #25
  6. jacob navia

    Martin Shobe Guest

    I would assume that the compiler would use the type of p, i.e. void *.

    Martin Shobe
     
    Martin Shobe, Apr 15, 2014
    #26
  7. jacob navia

    Stefan Ram Guest

    I have invented something! I think I am going to call it
    »object-oriented programming«. I am going to write a
    preprocessor for C to make this style of programming
    easier. I might call the preprocessor »cfront«.

    #include <stdio.h> /* printf */

    struct o { void( *print )( void const * const); };
    void print( void const * const p )
    { struct o const * const o = p; o->print( p ); }

    struct a { struct o o; int i; };
    void printa( void const * const p )
    { struct a const * const a = p; printf( "%d\n", a->i ); }

    struct b { struct o o; char c; };
    void printb( void const * const p )
    { struct b const * const b = p; printf( "%c\n", b->c ); }

    int main()
    { struct a a_ ={ { printa }, 65 }; struct a * a = &a_;
    struct b b_ ={ { printb }, 'A' }; struct b * b = &b_;
    print( a );
    print( b ); }
     
    Stefan Ram, Apr 15, 2014
    #27
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.