PDA

View Full Version : Newbie C Question




Ron68030
May 17, 2008, 04:36 PM
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



aaronw1986
May 17, 2008, 04:41 PM
I don't think c.h exists...why did you want to include this?

Eraserhead
May 17, 2008, 04:42 PM
You want to do #import <stdlib.h> or #import <stdio.h>

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

Thanks

italiano40
May 17, 2008, 04:45 PM
u should use

#import <stdlib.h>
#import <stdio.h>

Ron68030
May 17, 2008, 04:49 PM
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
May 17, 2008, 04:50 PM
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
May 17, 2008, 04:52 PM
#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;
}

Eraserhead
May 17, 2008, 05:19 PM
I don't believe C natively has booleans, you have to use 1 and 0 AFAIK...

admanimal
May 17, 2008, 05:34 PM
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
May 17, 2008, 05:36 PM
Thank you. But why would it run sometimes (from the example) and not others? Thanks again for your help.

yeroen
May 17, 2008, 05:53 PM
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.


#define FALSE 0
#define TRUE 1


or

enum Boolean {false, true}; /* i.e., false=0, true=1 */

Mac Player
May 17, 2008, 08:49 PM
You can use #include<stdbool.h>

lee1210
May 17, 2008, 09:52 PM
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
May 17, 2008, 11:16 PM
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

yeroen
May 17, 2008, 11:20 PM
We all love C questions. The more arcane the better (it's our chance to show off) :)

friarlicious
May 19, 2008, 05:57 AM
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;
}

Sander
May 19, 2008, 06:29 AM
By the way: #import is not C (it's Objective-C). Just use #include.