c++ - Void pointer without type casting -
int main { void* i; printf("%d",i); return 0; }
output : (garbage value) how void pointer determines data type without typecasting??
compilers not bother types of arguments passed function printf
.
the function declared following way
int printf(const char * restrict format, ...);
as see there first parameter has explicitly specified type. in general case compilers unable check whether other arguments valid.
when function executed parses first parameter searching format specifies , according these format specifiers tries interpretate following arguments.
in example format string has format specifier %d
printf("%d",i);
so function interpretates value of of type int
. take account (c standard)
if argument not correct type corresponding conversion specification, behavior undefined.
among other things did not initialize variable i
.
Comments
Post a Comment