Altera_Forum
Honored Contributor
20 years agoprintf on Nios2 not working properly
Hi,
I am working on a custom board with Nios2. I am facing a weird problem with printf. When I do any printf I am getting an inversed output. i.e. If I do printf("Hello World"); I get eHll ooWlrd. Take 2 chars from H and reverse. U get the o/p I am getting. All this is happening in vsprintf which is called by printf for formatting. Below I will give the code snippet where things are going wrong. int vsprintf(char *buf, const char *fmt, va_list args) { ................... char * str; ................... for (temp=str=buf ; *fmt ; ++fmt) { if (*fmt != '%') { *str++ = *fmt; continue; } ..................... case 's': s = va_arg(args, char *); if (!s) s = "<NULL>"; len = strnlen(s, precision); if (!(flags & LEFT)) while (len < field_width--) *str++ = ' '; for (i = 0; i < len ; ++i) *str++ = *s++; while (len < field_width--) *str++ = ' '; continue; .................... } for (i = 0; i < len ; ++i) *str++ = *s++; This is where every thing goes wrong. suppose s="Hello from NiosII!" it coppies correctly to str but it appears as mentioned above in buf i.e "eHll oofmoN oiIsI. Clue: For loop which is suppose to incerement from 0 to len, actually decrements from len to 0. I beleive it is compiler optimization.