Popular Posts

Getting started in C



The best way to get started with C is to actually look at a program, so load the file named trivial.c into edit and display it on the monitor.


Your First C Program
You are looking at the simplest possible C program. There is no way to simplify this program, or to leave anything out. Unfortunately, the program doesn’t do anything.

main()
{}

The word "main" is very important, and must appear once, and only once, in every C program. This is the point where execution is begun when the program is run. We will see later that this does not have to be the first statement in the program, but it must exist as the entry point. Following the "main" program name is a pair of parentheses, which are an indication to the compiler that this is a function. We will cover exactly what a function is in due time. For now, I suggest that you simply include the pair of parentheses. The two curly brackets { }, properly called braces, are used to define the limits of the program itself. The actual program statements go between the two braces and in this case, there are no statements because the program does absolutely nothing. Youcan compile and run this program, but since it has no executable statements, it does nothing. Keep in mind however, that it is a valid C program.


A Program That Does Something
For a much more interesting program, load the program named wrtsome.c and display it on your monitor. It is the same as the previous program except that it has one executable statement between the braces.

main( )
{
printf("This is a line of text to output.");
}

The executable statement is another function. Once again, we will not worry about what a function is, but only how to use this one. In order to output text to the monitor, it is put within the function parentheses and bounded by quotation marks. The end result is that whatever is included between the quotation marks will be displayed on the monitor when the program is run.

Notice the semi-colon ; at the end of the line. C uses a semi-colon as a statement terminator, so the semi-colon is required as a signal to the compiler that this line is complete. This program is also executable, so you can compile and run it to see if it does what you think it should. With some compilers, you may get an error message while compiling, indicating the printf() should have been declared as an integer. Ignore this for the moment.