is it possible write to console without stdlibs? c/c++ -


i programming on arm microprocessor , trying debug using print statements via uart. not want add stdlibs debugging. there way print console without stdio.h/iostream.h ? possible me write own printf()?

alternatively can using dma controller , writing uart directly. avoid possible. using built in test function "echo" or "remote loop-back" know have uart configured properly.

short answer: yes, it's entirely possible both of solutions.

the printf function quite complex if want support of data types , formats. it's not hard write can output string or integer in few different bases (most people need decimal , hex, octal adds 3-4 lines of code once have decimal , hex).

typically, printf written this:

 int printf(const char *fmt, ...)  {      int ret;      va_list args;        va_start(args, fmt)      ret = do_xprintf(outputfunc, null, fmt, args);       va_end(args);      return ret; } 

and do_xprintf() hard work variants (printf, sprintf, fprintf, etc)

int do_xprintf(void (*outputfunc)(void *extra, char c), void *extra, const char *fmt, va_list args) {     char *ptr = fmt;     while(1)     {        char c = *ptr++;         if (c == '%')        {             c = *ptr++; // next character format string.              switch(c)             {                case 's':                    char *str = va_arg(args, const char *);                   while(*str)                   {                       count++;                       outputfunc(extra, *str);                       str++;                   }                   break;                 case 'x':                    base = 16;                   goto output_number;                 case 'd':                   base = 10;          output_number:                   int = va_arg(args, int);                   // magical code output 'i' in 'base'.                    break;                 default:                   count++;                   outputfunc(extra, c);                   break;          }          else              count++;              outputfunc(extra, c);      }      return count;  }                 

now, need fill in few bits of above code , write outputfunc() outputs serial port.

note rough sketch, , i'm sure there bugs in code - , if want support floating point or "widths", have work bit more @ it...

(note on parameter - output file * filepointer, sprintf, can pass structure buffer , position in buffer, or that)


Comments

Popular posts from this blog

java - Jmockit String final length method mocking Issue -

asp.net - Razor Page Hosted on IIS 6 Fails Every Morning -

c++ - wxwidget compiling on windows command prompt -