printf("%s\n", "123456789" + 3); //output: "456789"
why output this result? In the same way, what should I do if I want to output "123"? Now I'm confused. Help me.
printf() is passed a pointer to a string constant incremented by 3
printf("%s\n", "123456789" + 3);
printf() takes a format string argument followed by a variadic set of arguments. In this case, the '%s' is the first (and only) variable in the format string.
There is only one argument in the variadic section after the format string: a string constant: "12345678" which resolves to a pointer to the '1' character; however, that pointer is incremented by 3 due to the "+ 3". This moves the pointer 3 positions to the '4'.
Then, printf prints the string that starts at '4'. As the string constant is terminated by a '\0' character right after the '9', printf prints "456789"
- Splitting an array into 2 arrays C#
- Use Visual Studio 2017 with .Net Core SDK 3.0
- Find the point on a circle with given center point, radius, and degree
- Why doesn't the if statement print 'well done' and why does the loop repeat out questions()
- Why can't we delete an initialized pointer?
- Does C++20 mandate source code being stored in files?
- unique_copy for a class does not recognize the operator<
- Difference between normal pointer and const pointer in c
- C++ sources should be files in c++20?
- How to filter data from xml using xmldocument class & xpath
- nment * * * in function calls * operator * st
- | Z Symbols * ! (exclamation) * in a command inte
- r(var c=0,a=f.length;c
- Generic Structure Publishers.Zip4 A publisher created b
- Generic Structure Publishers.Zip3 A publisher created b
- Generic Structure Publishers.CombineLatest4 A publisher
- Generic Structure Publishers.CombineLatest3 A publisher
- ols [index top] ! (logical NOT), Logical Operators != (no
- the form "gcc", "g++", "c++", and "clang". This is fine for
- Generic Structure Publishers.Merge8 A publisher created