.... snip ...
its possible
printf("size of variable is %d\n",(char*)(&a+1)-(char*)(&a));
This will print the proper size of variable.
No it won't, it involves undefined behaviour, even if you did
#include <stdio.h>. The type of a pointer subtraction is not int.
When you lie to a variadic function about the type of parameters
all rules are cancelled, and you may launch WWIII. In addition
printf("%d\n", (int)sizeof(int));
is considerable shorter and more understandable, besides being
defined. From N869:
6.5.3.4 The sizeof operator
Constraints
[#1] The sizeof operator shall not be applied to an
expression that has function type or an incomplete type, to
the parenthesized name of such a type, or to an expression
that designates a bit-field member.
Semantics
.... snip ...
[#4] The value of the result is implementation-defined, and
its type (an unsigned integer type) is size_t, defined in
the <stddef.h> header.
....
6.5.6 Additive operators
.... snip ...
[#9] When two pointers are subtracted, both shall point to
elements of the same array object, or one past the last
element of the array object; the result is the difference of
the subscripts of the two array elements. The size of the
result is implementation-defined, and its type (a signed
integer type) is ptrdiff_t defined in the <stddef.h> header.
If the result is not representable in an object of that
type, the behavior is undefined. In other words, if the
expressions P and Q point to, respectively, the i-th and j-
th elements of an array object, the expression (P)-(Q) has
the value i-j provided the value fits in an object of type
ptrdiff_t. Moreover, if the expression P points either to
an element of an array object or one past the last element
of an array object, and the expression Q points to the last
element of the same array object, the expression ((Q)+1)-(P)
has the same value as ((Q)-(P))+1 and as -((P)-((Q)+1)), and
has the value zero if the expression P points one past the
last element of the array object, even though the expression
(Q)+1 does not point to an element of the array object.79)