Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old May 17, 2008, 03:36 PM   #1
Ron68030
macrumors newbie
 
Join Date: May 2008
Newbie C Question

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 is offline   0 Reply With Quote
Old May 17, 2008, 03:41 PM   #2
aaronw1986
macrumors 68030
 
Join Date: Oct 2006
I don't think c.h exists...why did you want to include this?
__________________
Mac Pro 8 core 2.8Ghz, 8800GT, 8GB RAM, 1.3TB
iPad 2 Black 64GB Verizon
MBP 15" 2.4Ghz Glossy:4GB RAM,160GB 7200RPM HDD
Nikon D7000 and D90: 70-200mm 2.8 VR2, 24-70 2.8, 50mm 1.8
aaronw1986 is online now   0 Reply With Quote
Old May 17, 2008, 03:42 PM   #3
Eraserhead
macrumors G4
 
Eraserhead's Avatar
 
Join Date: Nov 2005
Location: UK
You want to do #import <stdlib.h> or #import <stdio.h>
__________________
If they have to tell you every day they are fair you can bet they arent, if they tell you they are balanced then you should know they are not - Don't Hurt me
Eraserhead is offline   0 Reply With Quote
Old May 17, 2008, 03:43 PM   #4
Ron68030
Thread Starter
macrumors newbie
 
Join Date: May 2008
I'm trying to add True/False logical operators to a simple program. From the book "Learn C on Mac OS X.

Thanks
Ron68030 is offline   0 Reply With Quote
Old May 17, 2008, 03:45 PM   #5
italiano40
macrumors 65816
 
italiano40's Avatar
 
Join Date: Oct 2007
Location: NY
Send a message via AIM to italiano40 Send a message via Skype™ to italiano40
u should use

Code:
#import <stdlib.h> 
#import <stdio.h>
__________________
Macbook 1gb Ram 2.10ghz 120gb HD Leopard
iphone 3g 8gb
Xbox360(Italiano40)
twitter
italiano40 is offline   0 Reply With Quote
Old May 17, 2008, 03:49 PM   #6
Ron68030
Thread Starter
macrumors newbie
 
Join Date: May 2008
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.
Ron68030 is offline   0 Reply With Quote
Old May 17, 2008, 03:50 PM   #7
aaronw1986
macrumors 68030
 
Join Date: Oct 2006
Quote:
Originally Posted by Ron68030 View Post
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.
__________________
Mac Pro 8 core 2.8Ghz, 8800GT, 8GB RAM, 1.3TB
iPad 2 Black 64GB Verizon
MBP 15" 2.4Ghz Glossy:4GB RAM,160GB 7200RPM HDD
Nikon D7000 and D90: 70-200mm 2.8 VR2, 24-70 2.8, 50mm 1.8
aaronw1986 is online now   0 Reply With Quote
Old May 17, 2008, 03:52 PM   #8
Ron68030
Thread Starter
macrumors newbie
 
Join Date: May 2008
#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;
}
Ron68030 is offline   0 Reply With Quote
Old May 17, 2008, 04:19 PM   #9
Eraserhead
macrumors G4
 
Eraserhead's Avatar
 
Join Date: Nov 2005
Location: UK
I don't believe C natively has booleans, you have to use 1 and 0 AFAIK...
__________________
If they have to tell you every day they are fair you can bet they arent, if they tell you they are balanced then you should know they are not - Don't Hurt me
Eraserhead is offline   0 Reply With Quote
Old May 17, 2008, 04:34 PM   #10
admanimal
macrumors 68040
 
Join Date: Apr 2005
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.
admanimal is offline   0 Reply With Quote
Old May 17, 2008, 04:36 PM   #11
Ron68030
Thread Starter
macrumors newbie
 
Join Date: May 2008
Thank you. But why would it run sometimes (from the example) and not others? Thanks again for your help.
Ron68030 is offline   0 Reply With Quote
Old May 17, 2008, 04:53 PM   #12
yeroen
macrumors 6502a
 
yeroen's Avatar
 
Join Date: Mar 2007
Location: Cambridge, MA
Quote:
Originally Posted by Ron68030 View Post
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 */
__________________
all Jarvis, all the time

Last edited by yeroen; May 17, 2008 at 05:01 PM.
yeroen is offline   0 Reply With Quote
Old May 17, 2008, 07:49 PM   #13
Mac Player
macrumors regular
 
Join Date: Jan 2006
You can use #include<stdbool.h>
Mac Player is offline   0 Reply With Quote
Old May 17, 2008, 08:52 PM   #14
lee1210
macrumors 68040
 
lee1210's Avatar
 
Join Date: Jan 2005
Location: Seattle, WA
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.
lee1210 is offline   0 Reply With Quote
Old May 17, 2008, 10:16 PM   #15
Ron68030
Thread Starter
macrumors newbie
 
Join Date: May 2008
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
Ron68030 is offline   0 Reply With Quote
Old May 17, 2008, 10:20 PM   #16
yeroen
macrumors 6502a
 
yeroen's Avatar
 
Join Date: Mar 2007
Location: Cambridge, MA
We all love C questions. The more arcane the better (it's our chance to show off)
__________________
all Jarvis, all the time
yeroen is offline   0 Reply With Quote
Old May 19, 2008, 04:57 AM   #17
friarlicious
macrumors newbie
 
Join Date: Aug 2007
Location: Pittsburgh
C standard from 1999 ("C99") supports booleans

Quote:
Originally Posted by Mac Player View Post
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;
}
__________________
GNU/Linux on a Mac Pro 8-core, 5GB RAM, 1.25TB storage. Free thyself.
friarlicious is offline   0 Reply With Quote
Old May 19, 2008, 05:29 AM   #18
Sander
macrumors 6502
 
Join Date: Apr 2008
By the way: #import is not C (it's Objective-C). Just use #include.
Sander is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
thread Thread Starter Forum Replies Last Post
Newbie question about Xcode 4 Apple2 iPhone/iPad Programming 2 Apr 16, 2011 05:03 PM
Apple Newbie/Pre-Switch Questions LameBolus Switch Stories 8 Jun 21, 2003 02:07 PM
newbie iTunes question davrouk Mac Applications and Mac App Store 10 May 19, 2003 02:03 AM
newbie printing question imacryan Mac Applications and Mac App Store 6 May 9, 2003 02:23 PM
Mac Newbie with questions regarding RAM lpj8 General Mac Discussion 4 Nov 18, 2002 09:50 PM


All times are GMT -5. The time now is 10:41 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

Privacy / DMCA contact / Affiliate and FTC Disclosure
Copyright 2002-2013, MacRumors.com, LLC