In this chapter and all others, I've assumed that you know how to type in, compile and run programs on your computer. Since such details vary from compiler to compiler, you'll have to read your software's documentation or contact a local expert for such details. I'll deal only with the C language itself. If you have a C++ compiler, make sure it compiles your programs in C mode, not C++ or you may get warnings or errors, in some programs. If you have any trouble, contact me and I'll try to help you out.
The first program that you will write in C will simply display the words 'Hello, world.' on the screen. I recommend that you type the program in yourself, and don't just cut and paste it. Because if you don't type in the program yourself, you might tend to read the tutorial like a novel, and end up learning absolutely nothing.
#include <stdio.h>
main()
{
printf ("Hello, world.\n");
}
Note that C is a case-sensitive language, so you can't replace, say printf by Printf. If you've correctly typed and compiled the program, you will get a program that displays the words 'Hello, world.' on the screen. Congratulations, you've just written your first C program. Now let's go through the program line by line, so you understand it thoroughly.
#include <stdio.h>
This tells the compiler to include a header file that gives you access to STanDard Input/Output functions. The header file stdio.h gives you access to many functions other than printf, and other fuctions may require some other header file. You will learn about the contents and need for header files in later chapters when you learn about functions.
This chapter only deals with giving you a feel of C programs and getting you off the ground. Most topics covered here are covered in more detail in later chapters. If all topics were covered in detail right here, it would leave you quite thoroughly confused.
main()
This line is a declaration for the 'main' function. This tells the compiler that the statements in the following braces {} belong to function main. The empty parenthesis indicates that it accepts no arguments. (More on functions and arguments below)
The braces {} enclose the statements that comprise the main function. In this case there is only one statement:
printf ("Hello, world.\n");
Here, the main function executes the printf function (which is included with the compiler) to display the words 'Hello, world.' on the screen.
A function is a part of a program that is designed to do a particular task. A C program consists of one or more functions. Execution always starts from the main function, i.e. the first line of the main function is the first line of your program to be executed. One functions may call another functions. For example, in this program main calls (i.e. executes) the printf function to display text on the screen. The printf function is provided by the compiler, and is accessible because we included the stdio.h file. An argument is a value that is supplied to a function by the caller to help it do its assigned job. In this program, we call the printf function to print (i.e. display) text on the screen. But printf needs to be told what text to print. This is given by the argument "Hello, world.\n". A function may accept more than one argument as we shall see later, or may not accept arguments at all, like the main function. If a function accepts two or more arguments, they are separated by a comma (,). Arguments are also called parameters.
Finally, printf's argument "Hello, world.\n" remains to be discussed. This is string argument. A string argument consists of one or more characters (letters, digits, symbols etc.) enclosed in double quotes (" "). H, e, l, l, o etc. all are printable characters. However \n does not represent one backslash and n. Infact \n is a single character called the newline character. (A backslash followed by letter is called an escape sequence. There are other escape sequences with which we shall deal with in later chapters. Note that the backslash itself is represented by the escape sequence \\) It tells printf to move the cursor to the next line, so that subsequent printfs produce output on the next line. If \n is not specified, the cursor remains at the end of the displayed message and the next printf will produce output there. (Unless, of course, the edge of the screen is reached). Hence, several printfs can be used to build up a single line of output. Thus the following program has exactly the same effect as the previous one.
#include <stdio.h>
main()
{
printf ("Hello, ");
printf ("world.");
printf ("\n");
}
The semicolon ; at the end of each printf is called a terminator. It tells the compiler when each statement ends. Thus each statement is terminated not by the end of each line, but by the semicolon.
In C, spaces, tabs and new lines are all treated equally. (These are collectively called white spaces). This means that you can replace a space anywhere by a new line. Remember that a statement is terminated by a semicolon, not by a new line. So this means that the printf statement in the above program can be written as:
printf
(
"Hello, world.\n"
)
;
This may look silly in this case, but it gives you flexibility to make programs more readable, and easier for you to understand more complicated programs. Note that the five lines above together comprise only one statement. Similarly, a new line may be replaced by a space. So you can put several statements on one line, separated by semicolons ; as shown below:
printf ("Hello, ");printf (" world."); printf ("\n");
The single line above, comprises three statements. Thus, you'll notice that that C's grammar is a lot more relaxed on formatting rules than many other languages.
Anything between a pair of /* and */ is ignored by the compiler, and is known as a comment. Comments serve no functional purpose, but can be used to make it easier for other people to understand your programs. Indeed, you may forget specific details of your own programs if you don't include comments. A comment can appear wherever a space, tab or newline can. For example:
#include <stdio.h> /* Required for printf */
main() /* The main function begins here */
{
printf ("Hello, world!"); /* print the message */
}
A comment may span several lines like this:
#include <stdio.h>
/* main
This is the main function. It displays the message
'Hello, world.' and ends.
Uses the printf() function
*/
main()
{
printf("Hello, world.\n"); /* print the message */
}
The following is also legal, because a comment can appear wherever spaces can! It, admittedly is rather wierd, though. I don't think you'll see constructs like this very often!
printf /* The printf function */ ("Hello, world.\n");
Its considered good programming practice to use plenty of comments. Of course once you are familiar with C you won't want to include a comment for each line like this, but should still include a comment for several lines of code, explaining the purpose of those lines. Many programmers include a comment giving their name and copyright notice before the begining of the program.
I won't include comments for programs I introduce over the next few chapters, because I will explain them in detail following the program listing. Later on, as you become more familiar with C, I won't explain the programs in such detail, but will include comments that make the program fairly self-explanatory, and then give you a brief overview of the program.
While reading other programs, you may come across something likeprintf ("Hello, world.\n"); // Print the message
Everything on that line after the // is ignored by the compiler. The above line is the same as
printf ("Hello, world.\n"); /* Print the message */
Actually, // is the new syntax for comments introduced in C++, and is not a part of the C language. However, many new C compilers allow that syntax, and many programmers use that syntax even in C programs.
In this chapter you've learnt:
Now that you've typed and run your first program, first make sure you understand in thoroughly. Read this chapter several times if necessary. Then its time to experiment a little before you move on the the next chapter. Here are some things you should try:
Now, you should be in a position to write the following programs. If you are not, I suggest you read the chapter again.
If you've tried writing the above programs, and have understood everything done in this chapter, you're now ready to move on the next chapter.
C Tutorial Quicklist | Start Page
| Contents |
| Ch 1:Introduction
| Ch 2:Variables
| Ch 3:Loops
| Ch 4:Expresisons
| Ch 5:Control Flow |
| Ch 6:Basic I/O
| Ch 7:Arrays |
| Home Page
| Beginners
| Advanced
| Links
| Downloads
| Books |
| C Tutorial
| HYPERSearch
| Guestbook
| Feedback |