
In programming language like C,C++ the sizeof operator generates the sizeof its operand. Operand may be any data type or constants. It gives the sizeof datatype or constants in bytes.
syntax of sizeof operator:
1 2 |
sizeof(operand/datatype); sizeof constants/variable name; |
note:
- If you have to find out the size of any datatype you must have to use first syntax. Your datatype must be within the small brackets sign, if you will use second syntax it will show you error.
- Second syntax can be used while finding out the size of constants like 4,6,9.8 etc.
- The keyword sizeof must be a single word. Size and of must not separated otherwise it will show you error.
Let us have a look to program given below, you will be more clear about sizeof operator .
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
/* this program find size of different operands.* #include<stdio.h> int main() { printf("size of int=%d\n", sizeof(int));//displays the size of integer datatype printf("size of float=%d\n", sizeof(float));//displays the sizeof float datatype printf("size of char=%d\n", sizeof(char));//displays the sizeof character datatype printf("size of double=%d\n", sizeof(double));//displays the size of double datatype printf("size of %d=%d\n",4, sizeof 4);//display the size of constant 4 printf("size of %lg=%d\n",4.4f,sizeof 4.4f);//displays the size of float type constants. return 0; } |
================================================================================
OUTPUT:
================================================================================
1 2 3 4 5 6 |
size of int=4 size of float=4 size of char=1 size of double=8 size of 4=4 size of 4.4=4 |
all the above size are in bytes.
Why sizeof operator?:
- Generally while doing any program or writing sizeof operator in any program a question might have came in your mind the datatype which you are using for storing your data will be sufficient or not whether you should go for another type of datatype or you should continue with the same datatype. So you need not to be worry about this kind of questions. Reason behind this is that sizeof operator helps us to overcome this type of questions. By using sizeof operator you can find out how much memory your data will consume and you can use different type of datatype as you required.
- It is useful in allocating the memory as required by the user .In C function like malloc(),calloc() and realloc() are used to allocate memory.In these function sizeof operator is used. More about calloc(),malloc() and realloc() will be discussed later as required.
There are many site which provides information related to the Programming please click here for more details.