learnprogramming123

LEARN PROGRAMMING 123
your resources to programming and free tutition



Navigation

 
C
 
C++ soon here!
VB - soon here!

Search

 

 

 

 

Next Back


Lesson 1: Your first ever C program:  Hello World

This lesson will teach you:

  • basics about compilers
  • printf
  • curly braces
  • first ever program

Q: If you are stranded on a desert island with Adolph Hitler, Attila the Hun, and a lawyer, and you have a gun with only two bullets, what do you do?
A: Shoot the lawyer twice.

Welcome to the first of 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 known as a compiled language. Basically a compiler (e.g. Borland C++ or Microsoft Visual C++) is a program that reads source code, which is the C code written by a programmer (the code that you write), and it produces an executable or binary file that in a format that can be read and executed (run) by a computer (the compiler converts your source code into executable code). 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 executable file is produced when you click the build/run button in the program you are using to write the code.

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

Your first ever program:

#include <stdio.h>


int main()
{

    printf("Hello World\n");

    return 0;

}


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 descriptions (explanations) for functions (e.g. printf) that the program needs to use. A declaration for the printf function is in this file, the declaration is basically where the function printf is explained or defined to the compiler. The file stdio.h is a standard file that come with all compilers - u don't need to worry about it.

Line 2: int main()
This statement declares the main function. A C program can contain many functions but must always have only one main function. A function is a self-contained module\block of code that can accomplish some task. Functions are explained in a later tutorial. The keyword int specifies the return type of the function main. It tells the compiler (program converting the code) that this function will return a value on completion - this value is normally 0. In this case, 0 is returned to the operating system.

Line 3: {
This opening curly brace instructs the start of the program. This is where execution of the code begins when the program is run.

Line 4: printf("Hello World\n");
printf is a function from a standard C library (stdio.h) that is used to print strings (a string is more than one character, e.g. a word or sentence or a number) to the standard output, normally your screen. The compiler links code from these standard libraries (reads the libraries to obtain information) to the code you have written to produce the final executable code (the code that the computer understands). The "\n" is a special format modifier (also known as an escape sequence) that tells the printf to put a line feed (a line feed is an instruction to pull the line - like in those old typewriters) 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 curly brace instructs the end of the program.

You're done!. To get the most of this series of tutorials, you should get both a text editor (e.g. Notepad) and a C compiler (u can download one from this web site for free in our download section).

back to top        go to next lesson

 

 






1