Hi, I need your piece of advice. I am kind of novice to programming C. I am trying to create a Generic LCD_printf function, that can print decimals, string, characters.
The following is the code i am trying
#include <all necessary h file> // declaration void lcd_printf (const char *,...); //definition void lcd_printf(const char *identifier, const int idata *datastring) { char id; while (*identifier !='\0') { if (*identifier == '%') { identifier++; id = *identifier; switch (id) { case 'd' : dectolcd(datastring); // converts decimal to string break; } } else { _lcd_data(*identifier); // prints the character in LCD } identifier++; } } void dectolcd(int *datastring) { char *temp; sprintf(temp,"%d",datastring); // converts decimal to string lcd_display(temp); // prints string on the lcd } Void main { lcd_printf("%d",123); }
Now when I run this code, I am not getting the 123 on the LCD. I know i am messing with the pointers somewhere. Can someone help as what i can do better ??
You could consider using sprintf() instead, and have the C runtime library perform all formatting of data into a text string that you then send to the display.
Or capture the output of printf() character-by-character and route to the LCD.
If you really do want to play with implementing your own printf(), you could look at the declarations in stdarg.h, for suitable macros to process a variable number of arguments.
sprintf(temp,"%d",*datastring); // converts decimal to string