New To Mac-Forums?

Welcome to our community! Join the discussion today by registering your FREE account. If you have any problems with the registration process, please contact us!

Get your questions answered by community gurus Advice and insight from world-class Apple enthusiasts Exclusive access to members-only contests, giveaways and deals

Join today!

 
Start a Discussion
 

Mac-Forums Brief

Subscribe to Mac-Forums Brief to receive special offers from Mac-Forums partners and sponsors

Join the conversation RSS
OS X - Development and Darwin Discussion and questions about development for Mac OS X.

Need help working with basic C in xcode


Thread Closed

 
Thread Tools
DB_KG

 
Member Since: Aug 03, 2009
Posts: 2
DB_KG is on a distinguished road

DB_KG is offline
Alright im new to both C and xcode right now, im looking for a bit of help.
Im trying to learn C right now using "the C programming language" book. I am trying to work though the examples, and im still trying to work out the kinks before I really get into it.

when I open a new project and use the standard tool it offers this code for the hello world program

Code:
#include <stdio.h>
int main ()
{
    printf("Hello, World!");
	return 0;
}
this works fine and builds with no warnings, however when I try the example in the book im learning from, written by the people who made the language, I get errors during the build

Code:
#include <stdio.h>
main ()
{
    printf("Hello, World!");
}
now from a programming point of view I understand what the errors are telling me. My question is why does xcode seems to be using different rules while compiling then what I am expecting to see, and whats in the book?

Also iv noticed both these programs will print if i open them in "console", and it will also run with the same result if I remove the \n, which I definitely cant explain.
xstep

 
xstep's Avatar
 
Member Since: Jun 25, 2005
Location: On the road
Posts: 3,231
xstep is a name known to allxstep is a name known to allxstep is a name known to allxstep is a name known to allxstep is a name known to allxstep is a name known to allxstep is a name known to all
Mac Specs: 2011 MBP, i7, 16GB RAM, MBP 2.16Ghz Core Duo, 2GB ram, Dual 867Mhz MDD, 1.75GB ram, ATI 9800 Pro vid

xstep is offline
Is the warning "warning: return type defaults to 'int'"?

When code or scripts are run from the command line, they are expected to return an 'int' value relating to success or some failure. The compiler seems to be insisting on it. I would consider that a good thing. By the way, I tried using 'void' instead and the compiler warned me I wasn't using an 'int' value.

Perhaps the version of the book isn't up to date on this, has excluded the information for brevity, or the newer compiler is just being helpful. The latest version of that book is 11 years old. A lot has happened in compiler design since then.

As for the '\n'. That is simply a line feed. Removing it will will do no harm here. On the command line the cursor will be at the end of the period if you remove that token. The would mean another print line would start printing at that location.

CameraTime - Time lapse photography for novice and advanced users.

When asking questions, post the version of your software. You'll receive better answers.

Please post your results to the thread as it is good feedback.
vansmith

 
vansmith's Avatar
 
Member Since: Oct 19, 2008
Location: Toronto
Posts: 17,761
vansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond repute
Mac Specs: 2012 13" MBP (2.5 i5, 8GB)

vansmith is offline
Quote:
Originally Posted by xstep View Post
As for the '\n'. That is simply a line feed. Removing it will will do no harm here. On the command line the cursor will be at the end of the period if you remove that token. The would mean another print line would start printing at that location.
Not that this is a bad explanation (it's quite good in fact) but if you're like me, examples help a great deal.

Note that before I show an example, my examples are in Python so ignore the fact that my code looks different. I'm just trying to illustrate what the "\n" does. So, let's say I execute the following command:
Code:
print("Hello World!")
Executing that will print Hello World! to the screen. Now, if I change this code to include "\n", the output will put what I include after it on a new line. So, if I execute the following:
Code:
print("Hello\nWorld!")
the output looks like this:
Hello
World!


Hope that helps.

Important Links: Community Guidelines : Use the reputation system if you've been helped.
M-F Blog :: Write for the blog
Writing a Quality Post
D3v1L80Y

 
D3v1L80Y's Avatar
 
Member Since: Feb 02, 2004
Location: PA
Posts: 12,459
D3v1L80Y has a reputation beyond reputeD3v1L80Y has a reputation beyond reputeD3v1L80Y has a reputation beyond reputeD3v1L80Y has a reputation beyond reputeD3v1L80Y has a reputation beyond reputeD3v1L80Y has a reputation beyond reputeD3v1L80Y has a reputation beyond reputeD3v1L80Y has a reputation beyond reputeD3v1L80Y has a reputation beyond reputeD3v1L80Y has a reputation beyond reputeD3v1L80Y has a reputation beyond repute
Mac Specs: MacBook

D3v1L80Y is offline
Thread closed due to high spammer activity.

Please continue discussion in PM.

__________________________________________________
Posting and YOU|Forum Community Guidelines|The Apple Product Cycle|Forum Courtesy

mac: a waterproof raincoat made of rubberized fabric
MAC: a data communication protocol sub-layer, also known as the Media Access Control
Mac: a brand name which covers several lines of personal computers designed, developed, and marketed by Apple Inc.


Thread Closed


« Remote X application on MacOS | 3ware RAID driver »
Thread Tools

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
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
Trackbacks are On
Pingbacks are On
Refbacks are Off
Forum Jump

Similar Threads
Thread
Thread Starter
Forum
Replies
Last Post
Carbon framework on XCode Rafi Shalom OS X - Development and Darwin 1 04-17-2008 01:14 PM
keyboard stops working, only in Finder? Help! coolskierguy OS X - Operating System 0 02-25-2008 12:56 PM
Xcode 1.5 alejandrico OS X - Development and Darwin 0 06-03-2006 05:56 PM
Features I miss from Visual Studio, now using xCode... RChickenMan OS X - Development and Darwin 0 10-18-2005 11:14 AM
XCode Include path problem Jove OS X - Development and Darwin 0 03-18-2005 09:41 AM

All times are GMT -4. The time now is 04:35 PM.

Powered by vBulletin
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
X

Welcome to Mac-Forums.com

Create your username to jump into the discussion!

New members like you have made this community the ultimate source for your Mac since 2003!


(4 digit year)

Already a member?