though there are already several questions asked on this forum & others related to sizeof operator, i could not get any answer on how compiler evaluates the sizeof operator to find the size of any datatype, variable, pointer,array etc. if possible also point me to some links which can help me understand this in detail. any help will be greatly appreciated. thanks.
The compiler just knows the size of primitive datatypes; this knowledge is fundamentally built in to the compiler.
For traditional fixed-size arrays and complex data types (structs and classes), it just adds up the sizes of the constituent primitives and accounts for any necessary padding. See http://en.wikipedia.org/wiki/Data_structure_alignment
The sizeof()
calculation is done at compile-time in most cases. The exception is for variable-length arrays (new in C99), where it is calculated at runtime once the number of elements is known.