Is it possible to print out only a certain section of a C-string, without making a separate substring?
Say I have the following: char* string = "Hello, how are you?"; Is it possible to print out only the last 5 bytes of this string? What about the first 5 bytes only? Is there some variation of printf that would allow for this? Is it possible to print out only the last 5 bytes of this string? Yes, just pass a pointer to the fifth-to-the-last character. You can determine this by string + strlen(string) - 5 . What about the first 5 bytes only? Use a precision specifier: %.5s #include <stdio.h> #include <string.h> char* string = "Hello, how are you?"; int main() { /* print at most the first five