3 ms·
> You can printf non-null terminated strings too. Check printf("%.s", length, strptr).* I haven't checked yet, but I'm about 90% confident that's UB. Is print
by torstenvl 4y ago
> You can printf non-null terminated strings too. Check printf("%.s", length, strptr).*
I haven't checked yet, but I'm about 90% confident that's UB. Is printf() guaranteed not to read to the end of the string when you give it a length?
- nwellnhof 4y agoYes, it is. From the C11 spec: > Characters from the array are written up to (but not including) the terminating null character. If the precision is specified, no more than that many bytes are written. If the precision is not specified or is greater than the size of the array, the array shall contain a null character.
- torstenvl 4y agoThanks! I guess I didn't realize expressio unius est exclusio alterius applies in the C standard :D
- habibur 4y agoWon't read a single byte beyond the length you give it. And this is a standard practice in libraries for printing pre-determined lengthed string.