You are on page 1of 16

This is a Group of section that I have written about the C language I promise you that

this is the simplest way you will have to learn the C language.

I have divided it into number of sections each explained to the very basic. So if you
find this tutorial easy to learn then mail me at knowledge_forall@hotmail.com and I will
send you the next the part this is completely free. One should not charge for knowledge.

I hope you all like it

IN the beginning there were basically three languages that were used to make
computer based application (you don’t need to memorize them just knowledge sakes)

B.A.S.I.C --------- Beginner All Purpose Symbolic Instruction Code. (This was
used by new
ones that
were just
introduced
to the world
of computers)

C.O.B.O.L ------- Common Business Oriented language. (This was the


Language that
was used to
make the
software that
were business
oriented)

F.O.T.R.O.N ----- Formula Translator. (Used for


scientific formula
etc)

You don’t really need to actually learn there languages I am just telling you this so
that you have an idea that why C and C++ were there in the first place. Anyway let
us continue…..

So all these were different programming languages and when I say different, I mean
really really different. There was no way that one could make a desired software, like
basic could not make formula. In short it was always that one or the other thing was
missing so they combined the languages and make it one language and named it.
Well take a guess what they named it……..it was called C.P.L and B.C.P.L. Name
it something nice no combine the the programming language and say it
COMBINED PROGRAMMING LANGUAGE

C.P.L ----- Combined Programming Language (silly isn’t it) and another one
B.C.P.L ---- Basic Combined Programming Language

So in 1972 Dennise Rietche invented “C” a high language that we now use .(High
level language is t that such a language is used not only to make simple programs but
also operating systems like WINDOWS and UNIX it is called high level language
because it at a higher level then COBOL and Pascal etc which are called base
languages).
Fact File  (The original C was made in UNIX the UNIX then was rewritten in
the language C {that is how amazing the language is it actually is it’s the mother
of its father (WERID!!!!!)

Definition UNIX ---- This is an operating system just like WINDOWS

NOW WHAT DO YOU SAY WE START A LITTLE


ABOUT “C“
IN this language that we can build any and all the software you can imagine. Not
only softwares but even virus if you really put your head to it I mean really hardcore
programming. So read and work dedicatedly (WORK HARD!!!).

Well now I am going to tell you about the properties of the C language, these are not
basically used for making programs but they are important, So that we can
understand why and how does “C” works and what are the properties that make it
such a successful language.

1. Structure And Procedure Oriented


2. Modular And Flexible
3. Portable
4. Functional
5. Memory Management

Let us see the properties in details

1. Structure And Procedure Language

The C language is a structured language, what that means is that it has a definite
structure The code can not just be written anywhere in the program window.
There is a predefined structure which we have to follow in order to achieve our
desired result.
Now let us see an example don’t get scared we have not started making programs
as yet and u don’t need to think about wt these commands do. I have explained
the command in details later in the book it is just that I want you to see the
structure of a program.

Void main()
{

Printf (“hello user”);

Have a look at the yellow part above it is called the brackets


Now to explain this let us take an example
“Let the program be a criminal and the brackets be a jail in order to keep the
criminal calm and working we need the jail if there is no jail the criminal will not
be under control and not as we want them to”
Same is with the programs, if the program is in brackets then it will work as we
want it to or else it will not work.

The language is called a procedural language as there is a definite procedure to


follow
See the “VOID MAIN” command I can’t just write in just anywhere in the
program
It is supposed to be in the beginning and it should be in the beginning, for the
program to work properly.
2. Modular And Flexible
Definition Compiler --the one that converts the C language to computer
language and the computer language back to the high level language)

Ok let us look at the command

Printf (“hello user”)

Let me speak in a very simple term what is a module, we wrote the command
Printf (“hello user”) and this will print hello user on then computer screen. But
the point is that how does the computer’s compiler know what printf command
means, and how does it know that hello user is to be shown onto the screen. The
answer is simple that printf is a command and it tells the computer’s compiler
that the statement in the brackets is to be printed. Now the question rises, how
does it tell this to the computer’s compiler? The answer is that there is a complete
code for every command (commands like printf) in the “C” language. They are
per-build and are saved in small modules.

These modules are stored in a HEADER FILE (we will do the header file later in
the book)
To understand this think of header file as a box and commands as small cubes
that are placed in the big box. We pick them out as we need them.

So all we have to do is write the printf and the computer prints what ever
follows it.

If I confused you let us take it once more, well it is obvious


that the computer can not understand any thing unless and until the word or
command has been completely defined in the system. So there must be some
code that tells the computer what to do when the printf command is given.
In this case of printf it tells the computer to print on the screen the text that is
written after the command printf.

THE CODE TO FOR THE PRINTF STATEMENT MAY BE 4 PAGES LONG.


This code was written by the programmers of C language.

Imagine that we have to write this code every time we are going to print
anything. Then the program would be every tiring, long and confusing.

Would it not be stupid to write 4 pages of coding just to tell the computer to
print something, and then just one line that we actually have to show? It would
be something like this
Void main()
{

Blah Blah… (CODE FOR THE PRINTF STATEMENT)….blahhhhhlh


(“hello user”);

}
Had printf statement not been previously defined

Void main()
{

printf(“hello user”);

}
normal use of printf statement

Now let us suppose that the above written BLAH is the code for PRINTF so
what the makers of c language have has done is that it has already written the
code for printf and saved it in a file and given the file a command name. So all
the user has to is refer to the command and the compiler will find the code itself
instead of the typing the complete code.

Void main()
{

Printf (“hello user”);

There are many such command that we are saved in modules that is why we call
it a modular language

Flexible Well the language is very flexible as the user can bend it in any way he
or she might like
To make any desired program.

3. Portable
The reason that this language is the most successful is because it can
be transferred from one operating system to another. Transfer her
means that the programmer makes a C file in windows, puts it in a
floppy and then opens it in a UNIX system. That is why it is famous
among the users of both UNIX and WINDOWS operating system.

4. Functional

Well you must be thinking what has a programming language to do


with a function. The function like most of you might have seen is
algebra looks likes this

F(2x + 3y +6)

Now see how similar a C function and normal function is.

F(2x + 3y +6) Printf (“hello user”);


(Algebra function) (C language function)

Let Printf here be = F.


The hello user be = 2x + 3y +6.
Hence due to the use of functions the C language is called functional.

5. Memory Management

This property helps the programmer to allocate and de-allocate


memory in the hard drive and tell the programmer which part of the
memory is used by the variable (I will just come to the variable part in
a moment).

Ok let us start us with the juicy part THE CODING

#include<stdio.h>

void main()
{

printf("Hello there");

}
This is how a simple program in C would look like. I know when one look’s at it
for the first time it does looks a little confusing. If it does to you to there is
nothing to worry about that is where my works starts

The best way to understand is to break it down to small pieces.In order to explain
let me tell you every thing from the starting point “ # “ to the end brace “ } “

#  This is an operator (more details on operators later)


used in the C language this tells the complier that you
have to include the following file. It sort of yells to the
compiler “Hey yo compiler include this header file”

Include  This is clear word include tells the compiler to include


the file following.

< Anything.h >  Anything that comes in the < > and ends up with a .h
is a header file actually the .h extension is a header
file. What these files do is that they keep the modules
that we discuss earlier in them.

Void Main  These are two keywords void and main( ) . Void is used
so that the program does not return a value. well I
know what you might be wondering “VALUE WHAT
VALUE AND WHY ARE WE RETURNING IT ”
for now let us just say that every program returns a
value. When we don’t want it to return a value then we
use the “void”

Now for the main part well main ( ) is a function in which all the code is to be
written

{ }  These are the braces or the brackets these are used as


discussed earlier that C is a structural language so every thing has be in a
definite structure so when ever we use a function may it be the MAIN function or
another function the code is always to be written in these brackets

Important point is there if you want your program to run successfully then
check that …

Number of opening brackets { = Number of closing brackets }

Printf ------ This is a function that prints everything that is written in the inverted
commas. Its syntax is always same and is

Printf (" Hello there ");

Let us study in detail about the printf command.

Printf  Is the command and is a key word for the compiler.

( )  The brackets are used to specific the part that is to be printed


and also the variables. Anything out side the brackets in a
prinf statement would not be printed. Moreover would give
an error.

“ “  The inverted commas are there because everything that in


the brackets is to be printed. But the variables are defined
outside the commas. (This is inbuilt so we can not skip it.)

;  (The semicolon) It is the end point or the break point for a


particular statement. When ever the command is over we
always use a SEMICOLON in order to tell the C compiler
that the particular statement is over.

#include< stdio.h >

void main()
{

printf("Hello there");

Now if you take this example and write it on a C screen which will look like this
In order to run this program you have to goto the run (in file menu) and click on
the run command or press CTRL+F9( the functional key)

You will not see any output rather you won’t even notice anything happen other
than a flash, in order to view the output press ALT+F5( the functional key) of the
keyboard. When you run this program the output will be shown on a black screen
just like the one in DOS.

Now I want you to run this program a few more time.

When you had run the program more than one time you will see that the results
of the pervious runs are still there. So in order to clear the screen we use a
function called the clrscr() (clear screen)function. I think you already know
what a function is, if you don’t then let me help you; a function is set of
predefined commands that are saved to do a particular task. If you still don’t get
it think this way, in the morning we bathe and brush our teeth and go to toilet etc,
but one word for it is morning routine. So if you say I got up and did my morning
routine that means you did all these things but you did’nt had to repeat them.

In this case the function is MORNING ROUTINE and the commands were
BATHING AND BRUSHING etc.

Similarly in clrscr(). The function is CLRSCR() and commands you don’t need
to know.

Clrscr ( )  This is a predefined function that clears the screen so that the
new output could be displayed. It is so that the result of the previous run is
cleared.
Similarly aren’t you tired of pressing the Alt+F5 every time you
have to run the program. If you are then you are going to like the
next command.

Getch ( )  Getch  the full form is GET CHARACTER The reason that
you don’t see the result without pressing ALT+F5 is because the program is
immediately terminated after executing the last command. This is so because the
compiler executes all the commands and returns to the C programming window.
It works something like this, a guy was asked to run in a race from point A to B
and then run back, then the guy won’t stop anywhere, he will touch the point B
and run back to A. but if you ask him to get a kiss from the girl at point B then
he would stop there for sure. Similarly when we use the command getch( ) the
result will be shown on then screen as the compiler will wait to get a character
from you and then terminate the program. Unless and until you press any key it
will not terminate the program. As soon as you do it will put you back on the
program screen.

But these functions or commands do not exist in the < stdio.h > header file
these functions exist in the < conio.h > header file and you have to include this
header file in the beginning

So a complete running code would be as under

Arithmetic Operations
We all have done these operations in our school time,
• Addiction
• Subtraction
• Multiplication
• Division

All these operations can be very easily performed in the c language also

But before we do this………………

LET US TALK ABOUT VARIABLES BABY. Many people have


problem understanding them. I will try to be simple.

Most of the time when we make a program we ask the user to input some value
that we can manipulate or use .So when the user enters a number or a value it has
to be stored somewhere moreover it has to be retrieved when ever required, so
how does the C compiler do that ??? Well, this is where variables come in. When
we define a variable in a program and run the program then that variable acquire
some space in the computers memory that is used later. You can understand this
with a very simple example ,have you ever seen a human brain, I seriously hope
it was no one you knew. But notice the line or cuts. These are made by
memories. When ever something important happens a line is made on you brain,
this is the brain way to remember it, similarly when you tell the computer
something imp like a value it saves it in a variable (like the line of the brain). The
reason why we use variable is simple. In order to save a value on your hard disk
you need some space right! The space can be something like 109983rd place on
the hard disk. Now it’s very difficult to remember that number so when we use
Variable we sort of give a name to that place. So if you name your variable ABC
then ABC is actually a name for the 109983rd place.

HOW TO DECLARE ANY VARIABLE

For the declaration of any variable there is a pre-defined format, all variables are
to be declared like this only, or-else they are garbage or in other words of no use
to the system.

DATATYPE AND VARIABLE-NAME


To understand this let us break it first
Data mean any type of value entered by the user
Type is the type of data that is to be entered like a number, an alphabet or a name
etc. It can be anything for that matter (in a moment I will tell you all the data type
and what they are used for)

Variable name is the name given to a variable; it can be anything but there are
some rules that are predefined to name a variable

PROPERTIES

I. The variable can not be one of the keywords that are used in the c language (
like printf , include etc)
II. The first alphabet should always be a character after then anything goes
(w1213 = right) but
1knsd, 1234, $3mm or anything like that does not start with a character and
not accepted.
III. There can not be spaces in-between the variable ( ab c = wrong , abc =
correct )
IV. There is only one special character that is to be used while naming a variable
an underscore “_” ( ab_c , a_bc , a_bc = all correct ) but we can not use an
underscore in the beginning ( _abc = wrong )

Along with that the most important point is that the variable declaration should
be in the very first line while making a program, if it is not declared there then
it will again be a garbage to the system (garbage is nothing or waste)
Remember how we did C is a structured language. And if you don’t follow the
structure the program won’t run.

#Include<stdio.h>

void main()
{

int abc;

printf("Hello there");

Above is an example how and where it should be declared just under the first
bracket of the main function (explained in detail in a while)

Here is a list of the variable that you might use. Now there is a complete list in
the end of the book that you can use as you want but for now remember only
these

• Int
• Float
• Char
Let us have a deeper look at it shall we

DATATYPE FULL NAME DESCRIPITION

Int Integer Basically numbers


Float Decimal All the decimal numbers
Char Character Just one single character

LET US NOW TAKE EXAMPLE AS TO HOW TO DECLARE A


VARIABLE

For example we want the user to input a number and we have to store its value
in a VARIABLE .What we will simply do is

(There should be just one space between the data type and the variable name
(see image bellow) I have given more just in order to explain the things a little.
DATATYPE VARIABLE-NAME

int abc ( When we want to declare number that we have given the
. name “ abc”)
float abc1 ( When we want to declare a decimal number that we
have given the name “ abc1 ” )
char abc2 ( When we want to declare a character that we have
given the name“abc2“ )

And now I am going to show a program that will use an integer like variable
and notice how the variable is used. I will explain it in the finest details in a
moment.
FINEST details are as follows…

I have already explained most of the lines but there are new ones that need
explaining, they are…….

Int abc  This is the declaration of the variable.


Int = the variable is an integer type.
abc = the name of the variable.

Abc = 23  In this case the user is not inputting the value for the variable.
This time it is us the programmer assigning the value to abc
and the value is 23. So from now on the value of abc = 23
until we change it further.
Printf(“%d”,abc)  ok now this is a important new line. I know you
are a little confused and exited that what %d is doing here.
It is a key word and it is used to show the value for an
integer when we run the program. I know I don’t make sense
right now so let us study this part in little detail and I explain
to you why even the % is used.

Well I have told you earlier that the printf statement is the
command that tells the compiler that what ever is written
after the command is to be printed on the screen. So if you
think that just by writing “abc” (the variable name ) would
print the variable value on the screen then my dear you are
wrong it will just print Abc on the screen and we don’t want
that, so in order to print the values of a variable we have
some special keyword. %d is one of them. There is a list
right after the paragraph but for now let us concentrate on
this part only. So %d tells the compiler that there is a
variable in the position which is taken by it. And the variable
comes after the inverted commas after a comma. It works
like a ambassador for a country. The ambassador is not the
complete country. But it presence presents the presence of
the country it represents.

Printf (“ The value is : %d” , abc);

(Ambassador) (Country)

Well still confused!!! no problem don’t worry it is a little


difficult let me go with the statement word by word and tell
you exactly what is the use of every single word here

Printf  Told you before.

( )  Told you before

“ “  told you before

the value is :  Now why have we written this. This is used


because a good programmer always think about the
convenience of the end user if you just print a value 23 on
the screen the user will be all confused but if you print like
The value is : 23
then it looks like a lot simplified and easy to understand.

abc  This is the part where the value of the variable is


inserted in to the system I mean is that we have told the
compiler that there is a variable be giving %d but the value
of the variable is to be given by putting a comma after the
inverted commas and then writing the variable name

Printf (“The value is: %d” , abc)

Similarly we can give more variable and use there values as


well here is how
Here is list of the type of key words that we have to use now yes there is a list
in the end of any and all such you might need

%d This is when we are using a integer (number)


%f This is when we are using a decimal number(float)
%c This is user when we are using a charter (single charter

Here are some examples


This is an example of the float type variable. The output of it will not just be
23.2 but when we say the following term is a decimal or a float in C language
then it adds a .000000 point six digit to it by itself and the output on the screen
will be like this
I am going to wind up this part right here. If some of you think I am not writing
enough, I won't blame you because I am not. Thing is I have a paper to take
and I have to prepare for it but it is because of the amazing response of all you
people that I had to write more.

Once again I wanna thank all of you who written me mails and I have really
appreciated it. Each and every mail was very important to me.

If you like the second part then you must mail me…………… because the
response drives me to write more. Even if you have mailed me, you have to
mail again for the third part because I have not saved your emails address.

P.S : Some of you rightly pointed I had some spelling mistakes. So this time I
have the most beautiful girl called SUNSHINE (who is very intelligent by the
way) edit my tutorials from now on along with a very especial friend ANDY. I
hope you appreciate her work too because I am terrible at typing rightly. So if
you find the English proper it's because of her for sure.

I will love if you can tell me if any part was not clear or anything thing that needs to
be re explained. I would prefer criticism over phrase.

Knowledge_forall@hotmail.com

You might also like