C Tutorial
History Of C Language
In this section we will learn the brief history of C Language. This tutorial
teach you brief introduction about "How you can start C language.
C Language based on two languages named BCPL and B. BCPL was
written by Martin Richards and B was written by Ken Thompson in
1970.In 1988 the American National Standards Institute (ANSI) adopted
new and improved'' version of C, known today as ``ANSI C''. C language is a
middle level language.
1. A 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 a 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.
More>>>
Previous>>