5. Assignment 1
WAP to define a macro SIZEOF(x),without using sizeof
operator.
Input:
6. Assignment 1
WAP to define a macro SIZEOF(x),without using sizeof
operator.
Input: None
7. Assignment 1
WAP to define a macro SIZEOF(x),without using sizeof
operator.
Input: None
Output:
8. Assignment 1
WAP to define a macro SIZEOF(x),without using sizeof
operator.
Input: None
Output: Print size of different data types
9. How will you find the size of an data type without using
sizeof operator?
Assignment 1
10. How will you find the size of an data type without using
sizeof operator?
You can make use of address of respective variable.
Example: int x = 5;
Assignment 1
5
x
1000 1004
11. How will you find the size of an data type without using
sizeof operator?
You can make use of address of respective variable.
Example: int x = 5;
Difference of 1004 and 1000 is 4, so, use these addresses to get
the size of the data type i.e., (&x+1) – (&x)
Assignment 1
x
1000 1004
5
12. Example: int x = 5;
Now, what is the output when you do (&x+1) – (&x) ?
&x+1 - &x = 1000 + 1 * sizeof(int) – 1000
= 1000 + 1 * 4 – 1000
= 1004 – 1000
= 4 / sizeof(int)
= 4 / 4
&x+1 - &x = 1
But, we want 4 right? So, what needs to be done to get 4?
Assignment 1
x
1000 1004
5
13. Example: int x = 5;
Now, what is the output when you do (&x+1) – (&x) ?
&x+1 - &x = 1000 + 1 * sizeof(int) – 1000
= 1000 + 1 * 4 – 1000
= 1004 – 1000
= 4 / sizeof(int) <- Replace sizeof(char)
= 4 / 1
&x+1 - &x = 4
How can we change it to char?
Assignment 1
x
1000 1004
5
14. Example: int x = 5;
Now, what is the output when you do (&x+1) – (&x) ?
&x+1 - &x = 1000 + 1 * sizeof(int) – 1000
= 1000 + 1 * 4 – 1000
= 1004 – 1000
= 4 / sizeof(int) <- Replace sizeof(char)
= 4 / 1
&x+1 - &x = 4
Typecast: (char *)(&x+1) – (char *)(&x)
Assignment 1
x
1000 1004
5