Need help working with basic C in xcode

Status
Not open for further replies.
Joined
Aug 3, 2009
Messages
2
Reaction score
0
Points
1
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.
 
Joined
Jun 25, 2005
Messages
3,231
Reaction score
112
Points
63
Location
On the road
Your Mac's Specs
2011 MBP, i7, 16GB RAM, MBP 2.16Ghz Core Duo, 2GB ram, Dual 867Mhz MDD, 1.75GB ram, ATI 9800 Pro vid
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.
 

vansmith

Senior Member
Joined
Oct 19, 2008
Messages
19,924
Reaction score
559
Points
113
Location
Queensland
Your Mac's Specs
Mini (2014, 2018, 2020), MBA (2020), iPad Pro (2018), iPhone 13 Pro Max, Watch (S6)
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[COLOR="Red"]\n[/COLOR]World!")
the output looks like this:
Hello
World!


Hope that helps.
 
Joined
Feb 2, 2004
Messages
12,455
Reaction score
604
Points
113
Location
PA
Your Mac's Specs
MacBook
Thread closed due to high spammer activity.

Please continue discussion in PM.
 
Status
Not open for further replies.

Shop Amazon


Shop for your Apple, Mac, iPhone and other computer products on Amazon.
We are a participant in the Amazon Services LLC Associates Program, an affiliate program designed to provide a means for us to earn fees by linking to Amazon and affiliated sites.
Top