So, I have a string with a certain number of bytes (or lengths). I say bytes because there is no NULL terminator at the end of the line. Although, I know how long the string. Usually, as we all know, when you printf("%s", str); , it will print every byte until it reaches the NULL character. I know that there is no C line that is not NULL terminated, but I have a strange situation where I am storing material (not specifically strings) and I am not storing NULL, but the length is βthingβ.
Here is a small example:
char* str = "Hello_World"; //Let use our imagination and pretend this doesn't have a NULL terminator after the 'd' in World long len = 5; //Print the first 'len' bytes (or char's) of 'str'
I know that you are allowed to do something like this:
printf("%.5s", str);
But in this situation, I hardcode 5, although in my situation 5 is in a variable. I would do something like this:
printf("%.(%l)s", len, str);
But I know that you cannot do this. But it gives you an idea of ββwhat I'm trying to accomplish.
source share