Thursday, July 14, 2011

WRITING A FIRST PROGRAM

WRITING A FIRST PROGRAM

Hello World

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 About\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 About\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.

0 comments:

Post a Comment