I was working with c puzzles, and came across this question .
In here , a at printf statement the pointer gets incremented once, so i thought it will be pointing to the random number !
but I cant understand why 25 is the output?
could anyone please help me understand ?
Many thanks in advance;)
void main()
{
int const * p=5;
printf("%d",++(*p));
}
Answer: 25
You are pointing at a random piece of memory (address 5). You’re then incrementing whatever happens to be stored there, and printing the result. This is undefined behaviour; your program could do anything.
UPDATE: Actually, this code shouldn’t even compile. You’ve declared it as a pointer to
const, so the compiler shouldn’t let you increment. What compiler are you using?