Guys, any idea why this code doesnt work?Code:int main()
{
int lastDigit;
char num1[3] = "123";
lastDigit = strlen(num1) - 1;
printf("%c", num1[lastDigit]);
return 0;
}
I want to get the last character of the string. In that case its "3"
Printable View
Guys, any idea why this code doesnt work?Code:int main()
{
int lastDigit;
char num1[3] = "123";
lastDigit = strlen(num1) - 1;
printf("%c", num1[lastDigit]);
return 0;
}
I want to get the last character of the string. In that case its "3"
Because
char num1[3] = "123";
does NOT have a \0 at the end of it for strlen() to do it's thing properly.
Write
char num1[4] = "123";
or better, let the compiler do the work
char num1[] = "123";