sizeof() is commonly used operator in the C or C++. It is a compile-time unary operator that can be used to compute the size of its operand. The result of sizeof() is of unsigned integral type which is usually denoted by size_t. This operator can be applied to any data-type, including primitive types such as integer and floating-point types, pointer types, or compound datatypes such as structure, union etc.
int means a variable whose datatype is integer.
sizeof(int) returns the number of bytes used to store an integer.
int* means a pointer to a variable whose datatype is integer.
sizeof(int*) returns the number of bytes used to store a pointer.
Since the sizeof operator returns the size of the datatype or the parameter we pass to it. So, the value it should return after passing a variable of (int *) to it:
- Since int* points to an address location as it is a pointer to a variable, So, the sizeof(int*) simply implies the value of the memory location on the machine, and, memory Locations themselves are 4-byte to 8-byte integer values.
- On a 32-bit Machine, sizeof(int*) will return a value 4 because the address value of memory location on a 32-bit machine is 4-byte integers.
- Similarly, on a 64-bit machine it will return a value of 8 as on a 64-bit machine the address of a memory location are 8-byte integers.
Now, the value it should return after passing a variable of (int) to it:
- Since int in an integer type variable. So, the sizeof(int) simply implies the value of size of an integer.
- Whether it is a 32-bit Machine or 64-bit machine, sizeof(int) will always return a value 4 as the size of an integer.
Below is the illustration of sizeof operator on 64-bit machine:
Size of (int) = 4 bytes Size of (int*) = 8 bytes
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Similarities and Difference between Java and C++
- Difference and Similarities between PHP and C
- Difference Between malloc() and calloc() with Examples
- What is the difference between single quoted and double quoted declaration of char array?
- Difference between "int main()" and "int main(void)" in C/C++?
- Difference between ++*p, *p++ and *++p
- What’s difference between header files "stdio.h" and "stdlib.h" ?
- What is the difference between "char a" and "char a"?
- What is the difference between printf, sprintf and fprintf?
- Difference between getc(), getchar(), getch() and getche()
- What’s difference between “array” and “&array” for “int array” ?
- What's difference between char s and char *s in C?
- Difference between C structures and C++ structures
- Difference between Structure and Union in C
- Difference between strlen() and sizeof() for string in C
- Difference between const char *p, char * const p and const char * const p
- Difference between %d and %i format specifier in C language
- Difference between Relational operator(==) and std::string::compare() in C++
- Difference between std::remove and vector::erase for vectors
- Difference between float and double in C/C++
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.