Let's write our first C program in Linux.
Start Eclipse and Click File at the menu bar, then select New and C Project.
Give My First as the project's name. There are several options. Select the Make File project and Empty Project. Use Linux G C C for the tool chain and then click Finish.
Click Window at the menu bar. Select Show View and Project Explorer.
Our first C program will be very simple. We will write a program that prints a single line of message
Hello C
ending with the exclamation mark.
At the project explorer, select New and Source File.
I am going to call this file first.c.
What is the difference between the name My First and this source file? A project can have several source files. The project's name is somewhat like a folder. Some people also use "directory" instead of "folder". Inside this folder, you may have many files.
In this simple example, we have only one file.
At the top, eclipse automatically adds some information about this file: the name of this file, the date, and who creates this file.
Next, I am going to type the code.
The first two lines I entered include S T D I O dot H and S T D L I B dot H. These are called the header files. I will talk about header files in a later lecture.
Every C program starts at a special function and its name is main, M A I N, all lower case. This function returns an integer. That is the reason we need to put I N T in front of M A I N. This function takes two arguments. The first is an integer. The second is character star star. I will talk about these arguments later.
By convention, the names of the two arguments are always A R G C and A R G V. Don't change the names because you will confuse people if you do so.
The M A I N function has a statement using P R I N T F to print a message Hello C.
The ending slash n means adding a new line after the message.
Please remember to add a semicolon after the statement.
The next line is return EXIT SUCCESS, with an underscore between the two words.
This means the program has successfully accomplished what it intends to do. In this very simple program, it always succeeds. In complex programs, it is possible that the programs are unable to do what they need to do. In those cases, the programs should return EXIT FAILURE.
Save the file.
This file does not have correct indentation. Eclipse can indent code to make it more readable. Click Source in the menu bar and select Format.
As you can see, the code is now correctly indented.
Next, I am going to show some options in setting up this project.
Click Project in the menu bar and select Properties.
You can choose different coding styles. Eclipse will format your code based on the style you choose. Each style has some examples.
The default style is the K and R style. You can try B S D, G N U, or Whitesmiths.
The B S D style uses more spaces for each tab than the G N U style. You can also try the Whitesmiths style.
I prefer the G N U style and I will select that.
Click Apply.
Next, we set the Build Option.
Select Generate Make File automatically. I will talk about Make File in a later lecture.
Click OK
Since I have changed the style, I will format the code again.
Please remember to save your program often.
Before running the program, we need to build it. To build it, click Project at the menu bar and select Build All.
To run the program, click Run at the menu bar and select Run.
At the bottom of the window, you can see the "Hello C" message.
Congratulations! You have successfully written your first program.
The next lecture will explain the meanings of these lines.