Just out of curiosity:
#include <stdio.h>
#include <string.h>
enum { BIG_NUMBER = 1024 };
int main(void)
{
char mysterious_string[BIG_NUMBER];
if (scanf("%1023s", mysterious_string) == 1) {
if (printf("%s", mysterious_string) != strlen(mysterious_string)) {
// Can this state occur?
}
}
return 0;
}
Can the inner 'if' return true? Can I blindly trust both of these functions (printf() and strlen()) to return the right value?
It can absolutely be different.
If printf fails (for any number of reasons) then it will return a negative number which will not be the same as the value returned from strlen. In all other cases it will be the same.
Note: This is assuming the string is null terminated and that scanf works as expected.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With