Professional Documents
Culture Documents
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.
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)
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!!!!!)
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.
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()
{
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.
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()
{
}
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()
{
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
F(2x + 3y +6)
5. Memory Management
#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 “ } “
< 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
Important point is there if you want your program to run successfully then
check that …
Printf ------ This is a function that prints everything that is written in the inverted
commas. Its syntax is always same and is
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.
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
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
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.
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.
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
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…….
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.
(Ambassador) (Country)
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