Why does the shorter string ("paid"
) get printed by this program?
#include <stdio.h>
int main()
{
char s[] = "paid", t[] = "paviDboss";
if ((strlen(s) - strlen(t)) > 0)
printf("%s\n", s);
else
printf("%s\n", t);
}
Why does the shorter string ("paid"
) get printed by this program?
#include <stdio.h>
int main()
{
char s[] = "paid", t[] = "paviDboss";
if ((strlen(s) - strlen(t)) > 0)
printf("%s\n", s);
else
printf("%s\n", t);
}
Return type of strlen
is size_t
which is an unsigned type. The result of the subtraction is also size_t
and can therefore only be positive.
Just use
if(strlen(s) > strlen(t))