www.easyCPlusPlus.com

Easy C++

www.easyCPlusPlus.com

C Tutorial - Lesson 1: Writing And Compiling A First Program

Hello World

by John Kopp

Support this site at no cost to you

Welcome to easyCPlusPlus.com's C tutorial. The lessons in this tutorial will take you from being a beginner to being able to write real programs in C.

C is a compiled language. The C compiler is a program that reads source code, which is the C code written by a programmer, and produces an executable or binary file that in a format that can be read and executed (run) by a computer. The source file is a plain text file containing your code. The executable file consists of machine code, 1's and 0's that are not meant to be understood or read by people, but only by computers.

The best way to learn anything is to jump right in, so let's start by writing a simple C program.

First Program - Hello World

#include <stdio.h>
void main()
{
     printf("Hello World From easyCPlusPlus\n");
}

Line 1: #include <stdio.h>
     As part of compilation, the C compiler runs a program called the C preprocessor. The preprocessor is able to add and remove code from your source file. In this case, the directive #include tells the preprocessor to include code from the file stdio.h. This file contains declarations for functions that the program needs to use. A declaration for the printf function is in this file.

Line 2: void main()
     This statement declares the main function. A C program can contain many functions but must always have one main function. A function is a self-contained module of code that can accomplish some task. Functions are examined in a later tutorial. The "void" specifies the return type of main. In this case, nothing is returned to the operating system.

Line 3: {
     This opening bracket denotes the start of the program.

Line 4: printf("Hello World From easyCPlusPlus\n");
     Printf is a function from a standard C library that is used to print strings to the standard output, normally your screen. The compiler links code from these standard libraries to the code you have written to produce the final executable. The "\n" is a special format modifier that tells the printf to put a line feed at the end of the line. If there were another printf in this program, its string would print on the next line.

Line 5: }
     This closing bracket denotes the end of the program.

That's it. To get the most of this series of tutorials, you should get access to both a text editor and a C compiler. Some instructions for doing this are in our tutorials on compiling.

Next - Lesson 2: Variables