execution function:
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"
Printing the first 3 chars of a string To print only the first 3 characters of a sting, use %.3s
in the printf format string:
printf("%.3s\n", "123456789");
Basically, strings are character arrays, meaning their name (variable name) points to the first element in the array. For instance, in the array a[]={3, 4, 5}, a[0] is obviously 3. However, the value of a itself is also 3 (like printf("%d", *a);) If you increment a itself, you are incrementing the address of a, so the value of a + 1 would be 4. Implicitly declaring a character array (what you did with the string) still requires the program to give it some form of address (That is, the location where the 1 in "123456789" is located) That being said, incrementing the string itself basically acts the same as incrementing the array name, so "123456789" + 3 would give the address associated with "456789". So what the printf basically sees is "print 456789".
I doubt I was able to explain this as simply as possible, or if you could even follow this, but I tried.
Best of luck!
One just can't add a string ("123456789") and a number (3). What is it that you want? a) Integer addition or string concatenation?
a) 123456789 + 3
b) "123456789" + "3"
The compiler will not guess which option do you want! So, one of the arguments needs to be converted to the type of the other, using some function such as ( unsafe ) atoi()
, etc...
"123456789"
is a string literal . It is an object of type char [10]
in C. It is an object of array type.
When an object of array type is used with binary +
operator, it gets implicitly converted (decays) to pointer type. The resultant pointer value points to the beginning of the array. So, the original char [10]
array decays to char *
pointing to character '1'
in "123456789"
.
Binary operator +
, when applied to a pointer, performs pointer arithmetic . Adding 3
to a char *
pointer produces a pointer that points 3 bytes to the right of the original pointer. So, you get a pointer that points to character '4'
in "123456789"
.
After that you use format %s
to ask ask printf
to print a string that begins from that '4'
. And that is the output you get.
The same thing happens in C++, except that in C++ this string literal has const char [10]
type and decays to const char *
pointer,
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.