The problem is that you are either using a broken compiler or you are not paying attention to it.
"6"
is a string literal in the form of an array placed in read-only memory. This array, like any array, "decays" into a pointer to the first element when used in an expression. So the "6"+10+x+y
part is pointer arithmetic (using 1 byte characters), which in turn goes way beyond the end of that array and causes undefined behavior.
Then finally, you try to assign that strange, invalid pointer to an int
. This isn't allowed by the C language, see "Pointer from integer/integer from pointer without a cast" issues.
The address will vary between different systems. You can run this well-defined code to see for yourself what's going on:
#include<stdio.h>
#include <stdio.h>
#include <stdint.h>
int main()
{
int x=0;
int y=0;
printf("%p (address of \"6\")\n", (void*)"6");
printf("+ 0x%x\n", 10+x+y);
printf("= 0x%x\n", (uintptr_t)"6"+10+x+y);
}
I get this output:
0x402004 (address of "6")
+ 0xa
= 0x40200e
So why didn't the compiler give you an error for this invalid code? The reason: What must a C compiler do when it finds an error?
If you are a beginner learning C, then I strongly recommend that you compile like this (assuming gcc/clang/icc):
gcc -std=c11 -pedantic-errors -Wall -Wextra -Werror
This will block broken C code from resulting in a confusing, incorrect program getting built.