top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

why is sizeof() an operator and not a function ?

+2 votes
855 views
why is sizeof() an operator and not a function ?
posted Sep 6, 2013 by Ganesh Kumar

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button
It's not possible to write a function that can take a type as an argument a function can take only variable as output.

1 Answer

+1 vote
 
Best answer

Sizeof() is a compile time operator. To calculated size of an element (either for inbuilt data type or user defined data type) type information is necessary at the compile time along with operating system information(32 bit or 64 bit). After compilation phase, compiler generates object code. object code doesn't has type information. while executing sizeof() just return calculated size of object then we use malloc, calloc etc function to allocate memory dynamically.

answer Sep 6, 2013 by Vimal Kumar Mishra
Similar Questions
0 votes

Used the sizeof of function, which gives 1; why?
I want to know the size of the entire function. How to achive it?

#include <stdio.h>
void (*p)(int); 
void test_func(int data)
{
  printf("%d\n",data);
}

main(void)
{
    p = test_func;
    (*p)(4);
    printf("%d",sizeof(test_func));
}
+7 votes
#include<stdio.h>

int &fun()
{
   static int x;
   return x;
}   

int main()
{
   fun() = 10;
   printf(" %d ", fun());

   return 0;
}

It is fine with c++ compiler while giving error with c compiler.

+4 votes
#include<stdio.h>
int var;
int var;
int var;

int main(void) { 
  printf("%d \n",var); 
  return 0;
}
...