I came across this code snippet from a blog who was asking for it's output and an explanation.
#include<stdio.h>
int main()
{
int *const volatile p = 5;
printf("%d\n",5/2+p);
return 0;
}
Here's what I understand so far:
1.The const and volatile keywords don't contribute in context of the output
2.This code is a bad one as it performs arithmetic operation between an int* and an int without a proper cast.
3.I initially assumed the output should be undefined. The blog stated the answer to be 13 (which what i experimentally found).
Q.What I don't understand is how was 13 obtained, was it the outcome of an undefined behavior from the compiler?
4.I used gdb and determined that the crux lies in the operation p+2
, where p is the pointer (holding value 5) and 2 is the outcome of integer division (5/2).