Thread: sizeof operator in c

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    50

    sizeof operator in c

    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?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    sizeof is a compile time operator, except when used with variable length arrays.

    In your example, the result of sizeof(a) does not change from one run of the program to another, so I do not see what is your confusion.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    Sizeof () is compile time. That is why the compiler has to see the full definition of a type, before it can determine the 'sizeof' it. Consider the following:
    Code:
    char a[] = "some string";
    ...
    printf("sizeof a %lu\", sizeof a);
    The compiler counts the length of "some string" + \0 and multiply that by the sizeof a char (Which the compiler already knows) and it can now replace any calls to 'sizeof a' with the pre-calculated size.

    But let's say we have this:
    Code:
    struct some_object object;
    ...
    printf("address of object %p sizeof object %lu\n", &p, sizeof object);
    This code is not valid because the compiler doesn't yet know what's contained inside a 'struct some_object' and thus can not determine its size.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    It is a compile time operator, and your use of it doesn't prove one way or the other whether it is compile time or run time.

    Code:
    #include<stdio.h>
    
    int main ( int argc, char *argv[] ) {
      char foo[sizeof(int)];
      char bar[argc];
      return 0;
    }
    
    
    $ gcc -std=c89 -Wall -pedantic bar.c
    bar.c: In function ‘main’:
    bar.c:5:3: warning: ISO C90 forbids variable length array ‘bar’ [-Wvla]
    bar.c:5:8: warning: unused variable ‘bar’ [-Wunused-variable]
    bar.c:4:8: warning: unused variable ‘foo’ [-Wunused-variable]
    Since the original standard for C didn't permit variable length arrays (run-time), the sizeof(int) in this context shows that it happens at compile time to provide the dimension of the foo array.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    It is determined at compile-time. When the program is compiled, one intermediate step effectively turns the above into:
    Code:
    int a;
     scanf("%d",&a);
     printf("%d",4);
    Assuming you compile it on a system that uses 4-byte ints.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Registered User
    Join Date
    Nov 2012
    Posts
    50
    @salem thankyou for your answer I agree to your point..but then what about the following code
    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? is the return type determined before the program runs?

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The return type is determined at compile time... you even wrote it right there: int.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    But you have managed to demonstrate that using sizeof on expressions with side effects can have surprises attached.
    Code:
    $ cat bar.c
    #include<stdio.h>
    int foo ( ) {
      printf("foo called\n");
      return 5;
    }
    int main(){
      printf("%zd\n",sizeof(foo()));
      return 0;
    }
    $ gcc -W -Wall bar.c
    $ ./a.out 
    4
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    There are no if's or but's, sizeof always, and can only produce a value determined at compile time.
    There are no examples to the contrary!
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by iMalc
    There are no if's or but's, sizeof always, and can only produce a value determined at compile time.
    There are no examples to the contrary!
    Here is an example to the contrary:
    Code:
    #include <stdio.h>
    
    int main(int argc, char *argv[])
    {
        int numbers[argc + 1];
        printf("%lu\n", (unsigned long)sizeof(numbers));
        return 0;
    }
    Hence my assertion in post #2 that "sizeof is a compile time operator, except when used with variable length arrays."
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Hmph. Damn C99 and it's cursed VLA's!

    Okay, there are no examples to the contrary in pre-C99 C then
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 10-15-2007, 08:05 AM
  2. sizeof operator
    By dhanulakshmi in forum C Programming
    Replies: 1
    Last Post: 12-01-2006, 07:09 AM
  3. sizeof operator
    By Roaring_Tiger in forum C Programming
    Replies: 9
    Last Post: 09-05-2004, 07:31 AM
  4. sizeof operator
    By studentc in forum C Programming
    Replies: 5
    Last Post: 05-20-2004, 02:42 PM
  5. sizeof operator
    By rjeff1804 in forum C Programming
    Replies: 0
    Last Post: 01-13-2003, 01:52 PM

Tags for this Thread