'I have a problem with declaring an array. C language, Atmel Studio
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.
Solution 1:[1]
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++.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
