C Programming language

First Program

In this section we are going to provide First Program of C, and define printf() function with Examples.

Previous Home Next

First Program

We are staring C Tutorial by this Hello World example.


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

Save the code in the file Hello.c or Hello.cpp, then compile it by typing:

gcc Hello.c

Output:

Hello World

In this example we including a library header file. Basically C language defines all methods into harder files which we need to include in our program before using these methods. To include any library header file C uses.


# include<nameofheaderfile.h>

A C program contains functions and variables. There are two type of functions user define and function define by C Language. The functions specify the tasks to be performed by the program.

Each program has a main() functions. The main function establishes the overall logic of the code. It is normally kept short and calls different functions to perform the necessary sub-tasks. All C codes must have a main() function.

printf() This function is an output function from the I/O (input/output) library (defined in the file stdio.h). In this example we are using this function to display Hello World.

\n is use to prints a "new line"character, which brings the cursor onto the next line . We can make above program as following. But result will same.


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

Try leaving out the ``\n'' lines and see what happens.

The first statement ``#include < stdio.h>'' includes a specification of the C I/O library. All variables in C must be explicitly defined before use: the ``.h'' files are by convention ``header files'' which contain definitions of variables and functions necessary for the functioning of a program, whether it be in a user-written section of code, or as part of the standard C libaries.

The directive ``#include'' tells the C compiler to insert the contents of the specified file at that point in the code. The ``< ...>'' notation instructs the compiler to look for the file in certain ``standard'' system directories.

The void preceeding ``main'' indicates that main is of ``void'' type--that is, it has no type associated with it, meaning that it cannot return a result on execution.

The ``;'' denotes the end of a statement. Blocks of statements are put in braces {...}, as in the definition of functions. All C statements are defined in free format, i.e., with no specified layout or column assignment. Whitespace (tabs or spaces) is never significant, except inside quotes as part of a character string. The following program would produce exactly the same result as our earlier example:

 


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

The reasons for arranging your programs in lines and indenting to show structure should be obvious.

Previous Home Next

Tolal:0 Click:

Show All Comments

Did not find what you were looking for leave your name and message. We will revert within 24 hours
Name:
eMail:
Comment / Feedback: