Thread: One more newbie question..

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    4

    One more newbie question..

    Hi again, I have a question I think about types. When I have something like:
    Code:
    time_t hour = time(NULL);
    What is time_t ? What purpose does it play? How different is it from:
    Code:
     int hour = time(NULL);
    And lastly, can I get any recommendations to online books for C programming for beginners?

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by somebody View Post
    What is time_t ? What purpose does it play? How different is it from:
    Code:
     int hour = time(NULL);
    Types like that are used to be communicative, and to provide an implementation with the opportunity to give a type very definite characteristics. Eg, apparently, systems that use time_t vary to the point where it may be an integer or floating point. If you wanted to write portable code for all those systems, you would want to take that into account, and the existence of the type would give you some chance of success, whereas if time() on some of those systems returned a long int and on others a double, things would get much uglier.

    Always use a more specific type (time_t) rather than a more general one (int) if applicable.

    Another common example is size_t, which is used for taking the length of something, and is thus an unsigned integer type (since something can't have a length of -5).

    Also, be aware that an int does not have a standardized size, but the some standards exist for defining such things, eg:

    <stdint.h>

    <stdint.h> is also part of the C99 standard, altho that version is not as expansive as the POSIX one. This gives you fixed width integers like int32_t (which is guaranteed to be 4 bytes), etc.

    And lastly, can I get any recommendations to online books for C programming for beginners?
    There's one in my sig below.
    Last edited by MK27; 04-08-2012 at 07:09 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    4
    Ah, thank you. The link in your signature is really good too!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie with Very Newbie Question
    By Jedi_Mediator in forum C++ Programming
    Replies: 18
    Last Post: 07-01-2008, 08:00 AM
  2. Newbie Question
    By mafioso1823 in forum C Programming
    Replies: 5
    Last Post: 04-29-2004, 04:10 PM
  3. C++ newbie / linux not so newbie question
    By goldmonkey in forum C++ Programming
    Replies: 7
    Last Post: 12-13-2003, 12:27 PM
  4. Another newbie question.
    By RealityFusion in forum C++ Programming
    Replies: 9
    Last Post: 08-20-2003, 01:59 PM
  5. Newbie Question, can anyone help?
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 05-12-2002, 09:05 PM