Here's a couple programs - they do the same thing:
PHP:
/*Using the [ php ] tag for MacRumors color codes the code
Code coloring helps you to understand what and where your variables are
Ignore everything above this line*/
#include <stdio.h> // include the standard input/output header
int main(int argc, char* argv[]) // starting point of any C/C++/Objective-C program
{
printf("Hello world!\n");
return 0;
}
Output
The above is C. Let's disect it:
#include <stdio.h> --links that header file to your program, this will allow you to use specific functions such as printf, scanf, sprintf, etc.
int main(int argc, char* argv[]) --this is the starting point of any C/C++/Objective-C program, int main is a function that takes two arguments, int argc, and char* argv[]. If you use this on the command line and you type something after the executable name (let's say its myfirstprogram) so if you type: myfirstprogram this program -- argc would have the number 3 stored in it. myfristprogram = 1, this = 2, program = 3 and char* argv[] would contain those arguments in an array. -- basically ignore all that about int argc and char* argv[] for now.
All functions that you are specifying what they do have curly braces around them, so inside the function int main(int argc, char* argv[]) you specify what it's going to do between { and }.
printf("Hello world!\n"); -- this will print out text out if you run it in a terminal or command line window. It will print out:
Hello world!
_
_ denotes the cursor. \n tells printf to go to a new line. So \n\n\n would go to the next line 3 times.
return 0; -- tells you everything executed fine and that there were no errors. if you get 1 or -1 or something above or below 0 then there was an error with your program.
You'll notice that lines that do something (like print output, return variables, declare variables, declaring functions that haven't been defined) all have semi-colons after them ( ; ). This is to tell the compiler when that function ends what its doing. Now this is different with different functions, for example if you are using variables, you can do a new line... ehh.. here's two examples:
NSLog(@"He"
"ll"
"o w"
"or"
"l"
"d!\n");
[myFraction number1:23
number2:44
string
"The fraction is: "];
oh the stuff between /* */ are comments and are ignored by the compiler same with //
Programmers use these to document there programs so they know what is going on.
PHP:
//This is a C++ program now
#include <iostream>
using namespace std;
//use the standard namespace std so we don't have
//to type in std::cout
int main(int argc, char* argv[]
{
cout << "Hello world!" << endl;
//now let's say we didn't specify using namespace std;
//we'd have to type this in to get Hello world!
// std::cout << "Hello world!" << std::endl;
return 0; // let our app know it ended fine
}
The above is C++ read through the comments to see what it does.
PHP:
#import <Foundation/Foundation.h>
//Yes names are case sensitive
//and notice we #import the files we need, we don't #include them
//#import is better so it doesn't have to compile code included from
//those files again
int main(int argc, char* argv[])
{
NSLog(@"Hello world!\n");
return 0;
}
Output:
Code:
2006-07-13 15:09:48.915 a.out[7822] Hello world!
Oi this post is long, if someone wants to explain more, go for it. The Objective-C program uses a part of the Foundation framework, but its not the first objective-c program I made in the one book, the first objective-c program used <stdio.h> (using #import)..
Anyways I've probably scared you from programming huh? there's a lot, and a lot to explain, just be prepared