This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Printf doesn't seem to use my Putchar

I added a putchar routine to my main program ... a call to my new
putchar works as expected

but a call to printf acts like it is still using the old putchar

what am I missing here?

  • Try again without the putchar()-prototype in stdio.h. Make sure that your own putchar()-function is public.

    HHK

  • stdio.h shouldn't matter - the compiler doesn't care where the prototype appears, so long as it's there somewhere; and the Linker knows nothing about prototypes.

    Just make sure that the prototype really does agree with the definition.

    As HHK said, make sure it's Public (else the Linker won't see it)

    Note that C51 names functions differently depending on whether they pass parameters in registers, or are reentrant.
    Have you set some inappropriate options here?

  • I tried to remove the putchar prototype in stdio.h and put one in
    my main source file ... it didn't seem to help ...

    I think the function is public ... my putchar is called _putchar
    in the symbol table with the words PUBLIC CODE PROC

    the code for my putchar routine is in my main source file ...
    the prototype for the putchar is in the stdio.h ... it uses the pragma
    regparms
    my function matches the prototype in stdio.h ... char putchar ( char )
    I use the "favor small code" option ....
    I have not made any attempt to make the routine reentrant...

    I appreciate the responses to my request for help ... I hope the
    information above points to a solution


  • Did you use all lower-case letters for your function (please check!) ? The "_"-character preceding the function name is added by the compiler to indicate that the function passes parameter(s) in register(s). You must not use this character as first character of your function.

    Have you tried to link the program using IXREF-control for the linker ? The map file should show where your function is used. If your putchar is called by printf and defined in main, then the cross reference listing should show both file names as reference for putchar.

    HHK

  • I created the following simple program:

    #include <stdio.h>
    
    char putchar (char c)
    {
    return (c);
    }
    
    void main (void)
    {
    printf ("");
    
    while (1)
      {
      }
    }
    

    When I compiled and linked, the following input modules were included in the final output (from the .M51 file):

    INPUT MODULES INCLUDED:
      .\STARTUP.obj (?C_STARTUP)
      main.obj (MAIN)
      C:\KEIL\C51\LIB\C51S.LIB (PRINTF)
      C:\KEIL\C51\LIB\C51S.LIB (?C?CLDPTR)
      C:\KEIL\C51\LIB\C51S.LIB (?C?CLDOPTR)
      C:\KEIL\C51\LIB\C51S.LIB (?C?CSTPTR)
      C:\KEIL\C51\LIB\C51S.LIB (?C?PLDIIDATA)
      C:\KEIL\C51\LIB\C51S.LIB (?C?CCASE)
    
    

    Note that the putchar function is NOT included from the library! If I change my example program to the following:

    #include <stdio.h>
    
    void main (void)
    {
    printf ("");
    
    while (1)
      {
      }
    }
    

    and I compile and link, I get the following:

    INPUT MODULES INCLUDED:
      .\STARTUP.obj (?C_STARTUP)
      main.obj (MAIN)
      C:\KEIL\C51\LIB\C51S.LIB (PRINTF)
      C:\KEIL\C51\LIB\C51S.LIB (?C?CLDPTR)
      C:\KEIL\C51\LIB\C51S.LIB (?C?CLDOPTR)
      C:\KEIL\C51\LIB\C51S.LIB (?C?CSTPTR)
      C:\KEIL\C51\LIB\C51S.LIB (?C?PLDIIDATA)
      C:\KEIL\C51\LIB\C51S.LIB (?C?CCASE)
      C:\KEIL\C51\LIB\C51S.LIB (PUTCHAR)
    

    Now, note that PUTCHAR is included from the C51S.LIB library.

    This is what you should get when you compile and link. Try my examples if you don't get these results.

    Jon

  • I tried the XREF and looked at the linker output ....

    putchar is NOT on the list of included functions
    printf does seem ref my _putchar function

    so it looks like printf should use my putchar function ... this is
    what made me think it was not using my putchar ...

    I added this test code to my program

       putchar ( '>');
       printf ( "AB");
       putchar ('>');
    

    the two putchar calls send out a character, the printf call sends
    nothing ... any ideas???

    once again, thanks for the comments/help

  • does printf "hold" its output til it's got a \n ?

  • Nope. The printf function does not buffer the output.

    Jon

  • If it had a buffer, It would be such a big problem in an Embedded.

    printf is also non-reentrant ( no buffer )...