EmbeddedRelated.com
Forums

Porting code from IAR to GCC printf problem

Started by Martin Steppuhn July 9, 2007
Hi,

I am changing from IAR (Kickstart) to Keil with GCC.

It works fine but there ist a problem with the printf(). My putchar() is working and Code like this also:

if(uart0_kbhit())
{
c = uart0_getc();
printf("%02X\r\n",c);
}

But if there is no "\n" at the end of the printf, the putchar is not called. So printf("Hello world!\n"); is working but printf("Hello world!"); not. Single character printf("X"); are also working. I am very confused and have seen never such a printf behaviour. The gcc is Version 3.3.1 (arm-uclibc-gcc) comming with Keil.

Best regards,
Martin

An Engineer's Guide to the LPC2100 Series

Martin Steppuhn schrieb:
> Hi,
>
> I am changing from IAR (Kickstart) to Keil with GCC.
>
> It works fine but there ist a problem with the printf(). My putchar() is working and Code like this also:
>
> if(uart0_kbhit())
> {
> c = uart0_getc();
> printf("%02X\r\n",c);
> }
>
> But if there is no "\n" at the end of the printf, the putchar is not called. So printf("Hello world!\n"); is working but printf("Hello world!"); not. Single character printf("X"); are also working. I am very confused and have seen never such a printf behaviour. The gcc is Version 3.3.1 (arm-uclibc-gcc) comming with Keil.

This might be a buffering-related problem. The output buffer is flushed
upon transmission of a \n.

___________________________________________________________
Der fre Vogel fgt den Wurm. Hier gelangen Sie zum neuen Yahoo! Mail: http://mail.yahoo.de
You can also use 'fflush (stdout);' to force the output. printf(), by
definition, uses buffered I/O, unless certain flags have been modified
via the termios calls (which are not typically supported on smaller
embedded systems).

You can also write to stderr ('fprintf (stderr, "some string");'),
which is configured as unbuffered, by default. Your system may or may
not support stderr correctly.

--jc

--- In l..., haare_in_der_dusche
wrote:
>
> Martin Steppuhn schrieb:
> > Hi,
> >
> > I am changing from IAR (Kickstart) to Keil with GCC.
> >
> > It works fine but there ist a problem with the printf(). My
putchar() is working and Code like this also:
> >
> > if(uart0_kbhit())
> > {
> > c = uart0_getc();
> > printf("%02X\r\n",c);
> > }
> >
> > But if there is no "\n" at the end of the printf, the putchar is
not called. So printf("Hello world!\n"); is working but
printf("Hello world!"); not. Single character printf("X"); are also
working. I am very confused and have seen never such a printf
behaviour. The gcc is Version 3.3.1 (arm-uclibc-gcc) comming with Keil.
>
> This might be a buffering-related problem. The output buffer is flushed
> upon transmission of a \n.
>
>
>
> ___________________________________________________________
> Der fre Vogel fgt den Wurm. Hier gelangen Sie zum neuen Yahoo!
Mail: http://mail.yahoo.de
>
Thanks jc for fflush (stdout), it works, but is there a option to set
unbuffered output for
printf ?

Best reagrds,
Martin

----- Original Message -----
From: jcwren
To: l...
Sent: Monday, July 09, 2007 4:55 PM
Subject: [lpc2000] Re: Porting code from IAR to GCC printf problem
You can also use 'fflush (stdout);' to force the output. printf(), by
definition, uses buffered I/O, unless certain flags have been modified
via the termios calls (which are not typically supported on smaller
embedded systems).

You can also write to stderr ('fprintf (stderr, "some string");'),
which is configured as unbuffered, by default. Your system may or may
not support stderr correctly.

--jc

--- In l..., haare_in_der_dusche
wrote:
>
> Martin Steppuhn schrieb:
> > Hi,
> >
> > I am changing from IAR (Kickstart) to Keil with GCC.
> >
> > It works fine but there ist a problem with the printf(). My
putchar() is working and Code like this also:
> >
> > if(uart0_kbhit())
> > {
> > c = uart0_getc();
> > printf("%02X\r\n",c);
> > }
> >
> > But if there is no "\n" at the end of the printf, the putchar is
not called. So printf("Hello world!\n"); is working but
printf("Hello world!"); not. Single character printf("X"); are also
working. I am very confused and have seen never such a printf
behaviour. The gcc is Version 3.3.1 (arm-uclibc-gcc) comming with Keil.
>
> This might be a buffering-related problem. The output buffer is flushed
> upon transmission of a \n.
>
> __________________________________________________________
> Der fre Vogel fgt den Wurm. Hier gelangen Sie zum neuen Yahoo!
Mail: http://mail.yahoo.de
>
Martin Steppuhn wrote:
>
> Thanks jc for fflush (stdout), it works, but is there a option to set
> unbuffered output for
> printf ?
>

See "setvbuf"

-Ed
Hi Ed,

that is it, mni tnx !
For the protocol :-) To disable buffering for printf() use:

setvbuf(stdout,(char*)NULL,_IONBF,0);

Martin

----- Original Message -----
From: Edwin Olson
To: l...
Sent: Tuesday, July 10, 2007 2:11 PM
Subject: Re: [lpc2000] Re: Porting code from IAR to GCC printf problem
Martin Steppuhn wrote:
>
> Thanks jc for fflush (stdout), it works, but is there a option to set
> unbuffered output for
> printf ?
>

See "setvbuf"

-Ed