Thread: Is it required to include typedef to use sizeof

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    113

    Is it required to include typedef to use sizeof

    Hello everyone

    I wonder if it is required to write #include <typedef.h> at the begining of the code to use the sizeof operator according to the standard.

    The return type of the sizeof operator is size_t and it is defined in typedef library. Therefore I conclude it is required. However I couldn't be sure as the sizeof keyword is mentioned as operator rather than a function in books.

    I could experiment it but the results would not be reliable as my compiler accepts printf and scanf even without stdio library.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    The size_t typedef is defined in several headers <stddef.h>, <stdio.h>, <string.h>, and in <time.h>. I don't recall seeing a standard C header with the name of typedef.h.

    Jim

  3. #3
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    Since sizeof is an operator, no header file is required to use it. The compiler knows all about sizeof, along with all the other operators and keywords.

    If you are using a compiler that provides a "typedef.h", you are using a non-standard compiler. I would recommend switching to a Standards complaint compiler.

  4. #4
    Registered User
    Join Date
    Dec 2010
    Posts
    113
    Thanks for both noticing me. Actually I was reading my old notes and I had taken note of this "size_t is defined in typdef library". However I reopened the book I was studying when I took these notes and it says: "C defines (using typedef) a special type called size_t, which corresponds loosely to an unsigned integer." Probably I did not know the keyword typedef at this time and I thought it was a library. If you didn't notice me I would keep thinking there is a standard library with name typedef.

    Thanks a lot again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Does #include indirectly include the source file too?
    By Lord Asriel in forum C Programming
    Replies: 10
    Last Post: 11-30-2011, 08:20 AM
  2. Replies: 1
    Last Post: 11-06-2011, 06:20 PM
  3. Replies: 5
    Last Post: 12-09-2010, 02:33 PM
  4. Replies: 6
    Last Post: 10-15-2007, 08:05 AM
  5. sizeof(cin) and sizeof(cout)
    By noobcpp in forum C++ Programming
    Replies: 11
    Last Post: 06-30-2007, 11:00 AM

Tags for this Thread