Newbie C Question

Discussion in 'Mac Programming' started by Ron68030, May 17, 2008.

  1. Ron68030 macrumors newbie

    Joined:
    May 17, 2008
    #1
    Hello,

    I'm trying to teach myself programming and I'm having problems already. When I add #include <c.h> I get an error message of "No such File or Directory" in X-Code.

    Any help would be appreciated.

    Thanks
     
  2. aaronw1986 macrumors 68030

    Joined:
    Oct 31, 2006
    #2
    I don't think c.h exists...why did you want to include this?
     
  3. Eraserhead macrumors G4

    Eraserhead

    Joined:
    Nov 3, 2005
    Location:
    UK
    #3
    You want to do #import <stdlib.h> or #import <stdio.h>
     
  4. Ron68030 thread starter macrumors newbie

    Joined:
    May 17, 2008
    #4
    I'm trying to add True/False logical operators to a simple program. From the book "Learn C on Mac OS X.

    Thanks
     
  5. italiano40 macrumors 65816

    italiano40

    Joined:
    Oct 7, 2007
    Location:
    NY
    #5
    u should use

    Code:
    #import <stdlib.h> 
    #import <stdio.h>
     
  6. Ron68030 thread starter macrumors newbie

    Joined:
    May 17, 2008
    #6
    When I change it to <stdlib.h> I still get a second error that reads " 'true' undeclared (first use in this function). The strange thing is the example program runs fine. But when I make my own program I still get those two error messages. This includes a copy/paste of the example.
     
  7. aaronw1986 macrumors 68030

    Joined:
    Oct 31, 2006
    #7
    Can you include your code...sounds like you are not properly declaring a boolean.
     
  8. Ron68030 thread starter macrumors newbie

    Joined:
    May 17, 2008
    #8
    #include <stdlib.h> //needed this to get definition of true
    #include <stdio.h>

    int main (int argc, const char * argv[])
    {
    int hasCar, hasTimeToGiveRide;
    int nothingElseOn, newEpisode, itsARerun;

    hasCar = true;
    hasTimeToGiveRide = true;

    if ( hasCar && hasTimeToGiveRide )
    printf( "Hop in - I'll give you a ride!\n" );
    else
    printf( "I've either got no car, no time, or both!\n" );

    nothingElseOn = true;
    newEpisode = true;

    if ( newEpisode || nothingElseOn )
    printf( "Let's watch Star Trek!\n" );
    else
    printf( "Something else is on or I've seen this one.\n" );

    nothingElseOn = true;
    itsARerun = true;

    if ( nothingElseOn || (! itsARerun) )
    printf( "Let's watch Star Trek!\n" );
    else
    printf( "Something else is on or I've seen this one.\n" );

    return 0;
    }
     
  9. Eraserhead macrumors G4

    Eraserhead

    Joined:
    Nov 3, 2005
    Location:
    UK
    #9
    I don't believe C natively has booleans, you have to use 1 and 0 AFAIK...
     
  10. admanimal macrumors 68040

    Joined:
    Apr 22, 2005
    #10
    Correct, C does not define true or false (or TRUE or FALSE as it would more likely be in C). I think the only constant defined in stdlib is NULL.
     
  11. Ron68030 thread starter macrumors newbie

    Joined:
    May 17, 2008
    #11
    Thank you. But why would it run sometimes (from the example) and not others? Thanks again for your help.
     
  12. yeroen macrumors 6502a

    yeroen

    Joined:
    Mar 8, 2007
    Location:
    Cambridge, MA
    #12
    I noticed in your first post you attempted to include the "c.h" header file. I suspect that this is a header file provided by the book and defined in an appendix or on the book's website. Many programming texts do this to economize on space on the printed page.

    Generally, header files vary from system to system and from one library implementation to the next. Booleans, an in-built type in C++, are in C a #define or an enumerated type, i.e.

    Code:
    #define FALSE 0
    #define TRUE 1
    
    or
    Code:
    enum Boolean {false, true}; /* i.e., false=0, true=1 */
    
     
  13. Mac Player macrumors regular

    Joined:
    Jan 19, 2006
  14. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #14
    Have you checked here:
    http://www.spiderworks.com/extras/index.php

    To see if that header file (c.h) is available for download? If the book's examples rely heavily on it (which would be a bit depressing anyway) you should probably have it handy.

    It does seem strange that the #include preprocessor directive uses chevrons (<>) and not quotes, as chevrons generally mean "look in system include directories" and quotes mean "look in the current directory". It seems off that a book would ask you to put a header they provide in a system directory.

    Good luck!

    -Lee

    P.S. I guess the right thing to call <> is angle brackets, not chevrons. That's what I've always heard them called, but maybe it's because I say it first in conversation and my first CS teacher taught me that way or something.
     
  15. Ron68030 thread starter macrumors newbie

    Joined:
    May 17, 2008
    #15
    Thanks to everyone for your help.

    #define FALSE 0
    #define TRUE 1

    That let the code compile.

    I'm sure I'll have more questions as I travel down this path.

    Thanks
     
  16. yeroen macrumors 6502a

    yeroen

    Joined:
    Mar 8, 2007
    Location:
    Cambridge, MA
    #16
    We all love C questions. The more arcane the better (it's our chance to show off) :)
     
  17. friarlicious macrumors newbie

    Joined:
    Aug 30, 2007
    Location:
    Pittsburgh
    #17
    C standard from 1999 ("C99") supports booleans

    Stolen from http://www.comeaucomputing.com/techtalk/c99/#bool :

    What Are _Bool and bool?
    C99 now supports a boolean type _Bool and also bool via <stdbool.h>. Consider:

    #include <stdio.h>
    int main()
    {
    // _Bool can hold 0 or 1
    // _Bool is an unsigned integer type
    _Bool b = 1;
    struct xyz {
    _Bool mem : 1; // _Bool bitfields allowed
    };

    printf("%d\n", b); // 1
    b = 0;
    printf("%d\n", b); // 0

    _Bool *pb = &b;
    // If the expression evaluates to 0, then it converts to _Bool as 0, else 1
    b = pb;
    printf("%d\n", b); // 1
    b = -1;
    printf("%d\n", b); // 1
    b = (pb == 0);
    printf("%d\n", b); // 0
    return 0;
    }

    // #define's bool, true, false, __bool_true_false_are_defined macros
    // as _Bool, 1, 0, and 1 respectively
    #include <stdbool.h>
    void foo()
    {
    int this = 99;
    int that = -99;
    bool b;
    if (this || that)
    b = true;
    else
    b = false;
    }
     
  18. Sander macrumors 6502

    Joined:
    Apr 24, 2008
    #18
    By the way: #import is not C (it's Objective-C). Just use #include.
     

Share This Page