Is the sizeof operator in c a compile time or run time operator..Because the following code works fine with a gcc compiler

int a;
scanf("%d",&a);
printf("%d",sizeof(a));

But I found in one of the tutorials thatsizeof is compile time??Can someone please clear the confusion?

In the case that you've presented here, sizeof(a) is resolved during compile. The only time it's resolved during runtime is when you're dealing with C99's variable length arrays.

The reason the above code works is that the number of bytes taken up by an int is always going to be the same, no matter what number is being stored in it. If you're compiling for a system where sizeof(int) is four bytes, then it's going to be four bytes no matter what you do to it.

@Tumlee thankyou foryour answer I agree to your point..but then what about the following code

int foo(){return 5;}
int main()
{
 printf("%d",sizeof(foo()));
 return 0;
}

here the call to foo() is made at runtime so the sizeof is evaluated at runtime??

commented: Good question. +13

it says about the size of the operator

commented: Signature spammer. -3

@tux4life thank you for your links but I have a different doubt...I want to know that since the sizeof is compile time operator then why does thee above code outputs 4 even though the call to foo() will be made at runtime..So how does the compiler know that the value passed as parameter to sizeof will be int if the call itself is made at runtime!!!

I guess it is because the size of the return type of foo() is known at compile time, but I'm probably NOT correct on this.
I hope that someone else can clarify on this, I might learn something too then ;)

[EDIT]
Okay, apparently I was wrong

Standard C (ISO/IEC 9899:1999) section 6.5.3.4:

"The sizeof operator shall not be applied to an expression that has function type or an incomplete type, to the parenthesized name of such a type, or to an expression that designates a bit-field member."

(source: http://stackoverflow.com/questions/2666392/what-does-sizeof-function-name-return)
[/EDIT]

foo() on line 4 of that code is a pointer, not a function. All pointers regardless of type are the same size, and in 32-bit compilers they are 4 bytes. szieof() does not work on functions as it treats function names as pointers to a function.

I want to know that since the sizeof is compile time operator then why does thee above code outputs 4 even though the call to foo() will be made at runtime.

You'll notice that foo() isn't called when used as an operand to sizeof:

#include <stdio.h>

int foo()
{
    puts("foo!");
    return 5;
}

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

That's because sizeof only evaluates the result of an expression, even if the expression contains runtime-only components or would be completely invalid if actually run. Another example is with malloc():

int *p = malloc(sizeof *p);

Dereferencing an uninitialized pointer is a horrible idea, but because sizeof evaluates the result as if the expression were run instead of actually running it, the above line is completely safe and even recommended.

@ancient dragon thankyou for your answer>> i tried the code by replacing the return type of foo() with float then also the output was 4..So this proves that foo() in sizeof behaves as function pointer..

i tried the code by replacing the return type of foo() with float then also the output was 4..So this proves that foo() in sizeof behaves as function pointer..

That proves nothing as the most common size of float is 4 bytes and there's no guarantee that a function pointer have the same size as object pointers. Try using double and you'll probably get output of 8, or char and you'll get output of 1. sizeof in this case is evaluating to the type of the return value.

I tried the code by replacing the return type of foo() with float then also the output was 4..So this proves that foo() in sizeof behaves as function pointer..please contribute if you feel my conclusion is wrong!!

please contribute if you feel my conclusion is wrong!!

I'll assume that you simply missed my post before saying that. If so, please read my post. If not, please read my post again until you understand it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.