Objective C Hello World World Example




Now we have the compiler and the environment set up, we can start writing and compiling our code. Our first "hello world" program will be written and compile from the console. Here are the steps involved.

1. On the MAC desktop create a new folder and let us call this folder test ( though you are free to give any name you wish to).

2. Now open the MAC terminal.
3. Navigate to the test folder created above using the command

$cd Desktop/test

4. Create a new text file and call it hello.c ( you can use the touch command to create a new file)

$touch hello.c

// creates a new text file hello.c

5. Open the text file for editing in a text editor. You may use open command.

$open -e hello.c

6. Now add the following lines of codes in hello.c

  1. #include <stdio.h>
  2. int main(){
  3. printf("Hello World from Referencedesigner.com\n");
  4. return 0;
  5. }


7. Save the file and exit.

8. On the command line give the following command to compile the file and generate the object file called hello.

$gcc hello.c -o hello

9. If you have not made any typing mistak, you should not see any error output.

10. Run the file using the following command

$./hello

it should give the following output.

$Hello World from Referencedesigner.com

Checkout this youtube for complete steps. The video is only a little over 2 minutes.



Exercise



It is a good idea to take up some exercises to butress you understanding

1. Change the program such that it prints something other than "Hello World from Referencedesigner.com", say "Hello World from me". Compile and run the program to make sure you get the desired output.

2. Add two successive printf statements.

printf("statement1\n");
printf("statement2\n");

compile and run the code to see that you get two lines of output.