Hello friends and gurus,
this is my first post here.
i have few basic C questions.
is sizeof an opearator(i actually studied it as a operator but then not so serious about the things)
1.why is it called as an opeartor?
2.sizeof is compile time operator. what does that mean ?
3.what about all other opearators arent they compile time?

its syntax and the usage is exactly like the function usage

examples:

int x;
       x=sizeof(x);
       FILE *fp;
       printf("%d",sizeof (fgetc(fp));

and sometimes i observed

sizeof(i)
    sizeof i

when can i ignore the paranthesis around the argument.

as this is my first post here, Xcuse me if i missfollow any of the forum rules. ( **though i had a glance at the rules**)

thanks .

Comments
N/A
go
Thanks for reading the rules and using code tags. :)

>>2.sizeof is compile time operator. what does that mean ?
It means that its only evaluated when the program is compiled because the result is a const unsigned int.

>>3.what about all other opearators arent they compile time?
Sometime yes and sometimes no. The = operator has to be evaluated every time the program is executed because the result may be different every time. If you initialize a variable when declared, such as int x = 0; then the = operator is a compile time. But if you assign it a math expression like x = (a * b)/2; then it becomes runtime.

>>when can i ignore the paranthesis around the argument.
The parentheses are optional. Whether you use them or not is just a programming style -- either way is correct.

You have to be careful of the sizeof operator because it can sometimes produce a result that you did not intend. For example, the sizeof a pointer is the same regardless of what kind of pointer it is.

>>2.sizeof is compile time operator. what does that mean ?
It means that its only evaluated when the program is compiled because the result is a const unsigned int.

>>3.what about all other opearators arent they compile time?
Sometime yes and sometimes no. The = operator has to be evaluated every time the program is executed because the result may be different every time. If you initialize a variable when declared, such as int x = 0; then the = operator is a compile time. But if you assign it a math expression like x = (a * b)/2; then it becomes runtime.

>>when can i ignore the paranthesis around the argument.
The parentheses are optional. Whether you use them or not is just a programming style -- either way is correct.

You have to be careful of the sizeof operator because it can sometimes produce a result that you did not intend. For example, the sizeof a pointer is the same regardless of what kind of pointer it is.

is sizeof an opearator(i actually studied it as a operator but then not so serious about the things)

Yes, it is an operator just like return , or = , or << . Sometimes it looks like a function because of parentheses around the operand, but that is just an appearance thing. Any expression can have redundant parentheses. :)

1.why is it called as an opeartor?

Because as an operator it is easier for the compiler to implement. Operators can be evaluated at compile time, and type information is available at compile time. sizeof is defined as evaluating to a compile time constant, so being an operator makes the most sense.

2.sizeof is compile time operator. what does that mean ?

It means that sizeof(expr) will probably be processed by the compiler and replaced with a constant value before generating object code. For example, if the size of an int is 4 on a hypothetical machine, the compiler might process printf("%lu\n", (unsigned long)sizeof(int)); and replace it with printf("%lu\n", (unsigned long)4UL); .

3.what about all other opearators arent they compile time?

If all parts of an expression are compile time constants, it can be evaluated at compile time, but might not be. That is up to the compiler. Otherwise it will be evaluated at run time because it relies on run time values and there is no other option.

when can i ignore the paranthesis around the argument.

When the operand is an object you can omit the parentheses. When the operand is a type name, the parentheses are required because some type names are multiple tokens and the parentheses make it easier to parse. For example, sizeof unsigned long is harder for a compiler to parse than sizeof(unsigned long) because the former has no definite end to the operand expression but the latter does.

i worked on below code and have few questions:

int main()
{
        //char just_c(char a);
        int just_i(int a);
        double just_d(double a);
        //char fun(char c);
        printf("char1 :%d\n",sizeof(fun('a')));
        printf("char: %d\n",sizeof(just_c('a')));
        printf("int : %d\n",sizeof(just(1)));// retained purposefully 
                                                              // not generating any error.
        printf("double : %d\n",sizeof(just(1.1)));
        //printf("just :%d\n",just(1));
return 0;
}
char just_c(char c)
{
        return c;
}

int just_i(int c)
{
        return c;
}
double just_d(double c)
{
        return c;
}

char fun(char c)
{
      return c;
}

in the program above
(i) when you call a function using sizeof ( just_c('a'));
the out put is 1, when i have the prototype of the function
4 if i dont have the prototype of the function .
the statement

printf("char: %d\n",sizeof(just_c('a')));

produces 4 without proto, and 1 with proto, why is it so?
is it making use of return type of the function?


2.sizeof is compile time operator. what does that mean ?

It means that sizeof(expr) will probably be processed by the compiler and replaced with a constant value before generating object code. For example, if the size of an int is 4 on a hypothetical machine, the compiler might process printf("%lu\n", (unsigned long)sizeof(int)); and replace it with printf("%lu\n", (unsigned long)4UL); .

this is fine as long as i use only objects and types.
But,
if i write any functions in sizeof operator its not generating any errors rather its giving different outputs as mentioned above ( i )

ii) i have debugged the code using GDB its doesnt jump to the function calls when functions are included in the sizeof operators
not even bothered whether function names are correct.

iii) when sizeof is compile time operator how its returning the value 4 or 1 when functions are called inside it .( as functions are runtime entities)

please Xcuse me if this is silly. but these questions are heating my my head and i dont have any one else to ask except putting in forums like these.

the out put is 1, when i have the prototype of the function
4 if i dont have the prototype of the function .

If there is no prototype for a function at the time it is called, the compiler will assume that the function returns int.

if i write any functions in sizeof operator its not generating any errors

In the case of a function return value, sizeof will evaluate the type of the object that the function returns. You are not getting the size of the function itself. I am not even sure how that would work. ;)

ii) i have debugged the code using GDB its doesnt jump to the function calls when functions are included in the sizeof operators
not even bothered whether function names are correct.

Yes! This is a tricky point where you can write correct code that looks wrong. For example:

int* p = malloc(sizeof *p);

Oh noes! An uninitialized pointer is being dereferenced and that is undefined behavior! But the code is correct and nothing is wrong with it because sizeof does not evaluate the expression. It only evaluates the type of the result of the expression. So the pointer is not being dereferenced at all, just like your functions are not being called. A more obvious test follows:

#include <stdio.h>

int main()
{
    int value;

    printf("%d\n", value = 123);
    printf("%lu\n", (unsigned long)sizeof(value = 456));
    printf("%d\n", value);

    return 0;
}

123 will be printed because the assignment statement is not run in sizeof, but sizeof(value = 456) still evaluates to the size of an int because the result of an assignment expression is the object being assigned to and the type of that object is int.

iii) when sizeof is compile time operator how its returning the value 4 or 1 when functions are called inside it .( as functions are runtime entities)

Types are known at compile time, including return types from functions. I cannot say exactly how sizeof is implemented because I am not a compiler programmer, but it should be pretty simple to query the symbol table for a function, get the return type, and then find the size of that type.

please Xcuse me if this is silly.

Silly? You've caught on to some pretty heady stuff like how a function is not called as an operand to sizeof. I am impressed with your thoroughness and it gives me renewed hope for the next generation of C programmers. :)

This article has been dead for over six months. Start a new discussion instead.