Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Ron68030

macrumors newbie
Original poster
May 17, 2008
6
0
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
 

Ron68030

macrumors newbie
Original poster
May 17, 2008
6
0
I'm trying to add True/False logical operators to a simple program. From the book "Learn C on Mac OS X.

Thanks
 

Ron68030

macrumors newbie
Original poster
May 17, 2008
6
0
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.
 

aaronw1986

macrumors 68030
Oct 31, 2006
2,622
10
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.

Can you include your code...sounds like you are not properly declaring a boolean.
 

Ron68030

macrumors newbie
Original poster
May 17, 2008
6
0
#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;
}
 

admanimal

macrumors 68040
Apr 22, 2005
3,531
2
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.
 

Ron68030

macrumors newbie
Original poster
May 17, 2008
6
0
Thank you. But why would it run sometimes (from the example) and not others? Thanks again for your help.
 

yeroen

macrumors 6502a
Mar 8, 2007
944
2
Cambridge, MA
Thank you. But why would it run sometimes (from the example) and not others? Thanks again for your help.

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 */
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
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.
 

Ron68030

macrumors newbie
Original poster
May 17, 2008
6
0
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
 

friarlicious

macrumors newbie
Aug 30, 2007
9
0
Pittsburgh
C standard from 1999 ("C99") supports booleans

You can use #include<stdbool.h>

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;
}
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.