int main(){
printf("hello world"+2);
}
test.c:32:25: warning: adding 'int' to a string does not append to the string
[-Wstring-plus-int]
printf("hello world"+2);
~~~~~~~~~~~~~^~
test.c:32:25: note: use array indexing to silence this warning
printf("hello world"+2);
^
& [ ]
1 warning generated.
alpha@Desktop % ./a.out
llo world%
So this is what I am getting. And if I increase the number then it is simply slicing the string by that.Can anyone explain this output and why it's happening to me?
In the C and C++ languages, every string that has been defined with double quotes is of type char*
(pointer to char) as a statically allocated char array. When you call printf()
on such string it does something like this:
void printf(char* str) {
while(*str!= '\0') {
// write the current character to stdout
write(STDOUT_FILENO, str, sizeof(char));
str ++;
}
}
I don't know if it really is written that way, indeed it probably isn't, the functionality is the same. It iterates over the string until it finds the null-terminator ( '\\0'
). All pointers are almost equivalent to long
and when you add a number to them, it increments their underlying value with sizeof(type)
, where type is the type of value the pointer points towards. Thus, when you add a number to "Hello World", makes printf()
think your string starts on a different memory address and prints 'llo World'.
If you want to print the string with the value '2' appended to the end of it, as @Elliott Frisch stated, you would use print("Hello World%d", 2)
.
I would suggest looking at sprintf()
and strcat()
for string concatenation in C.
In C, what we might think of as a string is actually an array of characters. What you pass to printf()
in both cases is a pointer to the start of such an array. You can do arithmetic with pointers, which is what you are doing here.
Here's some clarification.
#include <stdio.h>
int main(void) {
// A
printf("Hello, world!\n");
// B
printf("Hello, world!\n" + 2);
}
Output:
Hello, world!
llo, world!
In case A , you point to the start of an array containing the H
character, and it reads until the last one (which is in fact a 'this is the end'-character: \\0
).
⬇️ | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
H | e | l | l | o | , | w | o | r | l | d | ! | \\n |
\\0 |
In case B , you add 2 to the pointer before passing it to printf()
. Now printf()
starts reading here:
⬇️ | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
H | e | l | l | o | , | w | o | r | l | d | ! | \\n |
\\0 |
This is what causes your 'slice'.
The rules of the C language do not allow you to do anything like this. Your question could be compared to the question why I pour milk into the tank of the car instead of gasoline, and it does not go. The computer is just a stupid piece of hardware and it can only do what it was taught.
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.