0

this is my code and I have a problem with array.

int main(void)
{
    PORTD = 0x01;
    LCDOUT();
    
char count = 0;
char firstLine[] = "Number of count";
char secondLine[] = {count};


while (1)
{
    Command(HOME);
    LCD_String(firstLine);
    Command(LINE2);
    LCD_String(secondLine);
    
    if ((PIND&0x01)==0x00) 
    {


        count++;
            _delay_ms(500);
        }
    }
}

I want to make

char secondLine[] = "0"; when the code starts.

And if ((PIND&0x01) == 0x00)), then

secondLine[] = "1"

secondLine[] = "2"

secondLine[] = "3"

...

...

So in the secondLine, the number of counts can be shown through LCD.

  • Does this answer your question? [How to convert an int to string in C?](https://stackoverflow.com/questions/8257714/how-to-convert-an-int-to-string-in-c) – VLL Apr 28 '22 at 05:50

1 Answers1

2

The function snprintf() is supporting your goal.

https://en.cppreference.com/w/c/io/fprintf

It allows to create string representation of parameters.

Make sure that there is enough space in char secondLine[2]; and that you use the length parameter ( size_t bufsz ) to not write beyond.

You will have to do the printing inside your loop, so that the string content is updated along with count++.

Yunnosch
  • 26,130
  • 9
  • 42
  • 54