1 votes 1 votes #include<stdio.h> int main(){ short int i=20; char c=97; printf("%d %d %d\n",sizeof(i),sizeof(c),sizeof(i+c)); return 0; } could anyone explian why the answer of sizeof(i+c) is 4 ? Programming in C data-structures programming-in-c + – dragonball asked Aug 27, 2017 dragonball 5.6k views answer comment Share Follow See all 0 reply Please log in or register to add a comment.
Best answer 2 votes 2 votes Actually, sizeof operator returns size in bytes and is implementation dependent. Some compilers assign two bytes to int and some four bytes.Characters are internally stored as integers. It seems your integers are stored as 4 bytes. (i+c) is 4 because 97 is stored as an integer and short is promoted to higher type int berfore addition (97+20) = int so 4 bytes Shivam Chauhan answered Aug 27, 2017 • selected Aug 27, 2017 by Habibkhan Shivam Chauhan comment Share Follow See all 3 Comments See all 3 3 Comments reply dragonball commented Aug 27, 2017 reply Follow Share I am little confused . As sizes are given , so now u should not talk about compiler . 0 votes 0 votes dragonball commented Aug 27, 2017 reply Follow Share r u sure it works with other data types ? 0 votes 0 votes chandra sai commented Aug 27, 2017 i edited by chandra sai Aug 27, 2017 reply Follow Share no. sorry. see this http://www.geeksforgeeks.org/sizeof-operator-c/ 1 votes 1 votes Please log in or register to add a comment.
0 votes 0 votes If you want that your size of data type remain same in every compiler u can use int8_t (which will allocate 8 bit) and int16_t will allocate 16 bit (2 byte), int32_t will allocate 4 byte (32 bit) . Rishabh Agrawal answered Aug 28, 2017 Rishabh Agrawal comment Share Follow See all 0 reply Please log in or register to add a comment.