Wednesday, December 10, 2008

Hi,
I'm an IT engineering student. Here i have some easy points on c language & some part on data structure using C.
---------------------------------------------------------------------
C language:-
All things in this are based on windows platform.
To perform this program u have to install turbo c/c++.
Then in bin folder double-click on tc.
click on file and click new.
Then start typing the program.
Note that the instructions should be in either upper or lower case.
So first let me give u a simple program & then explain the terms.
************************************************************************
#include"stdio.h"
#include"conio.h"
void main()
{
printf("Hello world");
getch();
}

O/P=Hello world
************************************************************************
include:-This means including the file from the include folder which contains the information of the words in angular bracket. in any case this means the same.
#:-This is to differentiate the include files.
" ":-This is the way in which the include files are represented. This can also be represented as < >.
"" searches the header file everywhere on the
.h:-Extension of the header file.
stdio:-This means standard input & output.
conio:-This means console input output.
void:-This means there is no return type for main. Now it will be better to assume that this is a must component. The meaning of this will be understood on way.
main:-This is the main part which is compulsory in all the programmes.
{}:-The program is enclosed in the curly brackets. These are necessary.
printf:-This is the output instruction.
(""):-This is are the must after the instruction to differentiate the output from other instruction.
;:-This indicates the termination of the instruction.
getch():-This means get character. When in this program 'Hello world' is printed on the screen it will vanish immediately. So to stop this instruction is used. This results the program to halt and wait until a character or any key is pressed to end execution.
-------------------------------------------------------------------------------------
Now i think u have understood what a c program is to some extent from the above program.
To compile the program press ALT+f9.This will show the errors if any. If any errors are shown then check and correct the errors. Then press CTRL+f9 to run the program.
---------------------------------------------------------------------
Now we will do the further program which still harder than the previous one.
I will explain the newer instructions in the program.
************************************************************************
#include"stdio.h"
#include"conio.h"
void main()
{
int x;
clrscr();
x=10;
printf("%d",x);
getch();
}
O/p=10
************************************************************************
One term ‘clrscr();’ is used in this program. It is used to clear the contents of the previous program on the user screen. It ends with a semicolon.
int:-This stands for integer.
Float:-this means also integers but having decimal point.
This means we are declaring a variable named x having type integer.
this means we are allotting or booking a space for an integer in the computer memory and giving it name x so that we can access that integer stored in the memory named x.
x=10; :-This means we are storing the value 10 in the memory location named x.
%d :- this indicates that the output to be printed is of type integer.
%f : - This indicates that the output is of type float.i.e having decimal point.
There is one more term i.e
Char:- It is used to allocate space for character.
%c :- it indicates that output is of type character.
Memory allocations & example:-
Int:-2 bytes e.g- 40, 111
Float: - 2 bytes e.g- 4.4, 100.55
Char: - 1 byte e.g- a to z
Int, float, char are called the data types.
For character the ASCII values can be used.
ASCII means American standard code for information interchange.
The ASCII values
A to Z= 56 to 82
A to z=97 to 123
i.e if u declare character assign it a value and then print its value by taking its data type int the it will print its ASCII value.
#include”stdio.h”
#include”conio.h”
void main()
{
char x;
x=A;
printf(“%c”,x);
printf(“%d”,x);
getch();
}
O/P=A56
***********************************************************************
Here A is the output of the first printf & 56 is the output of the second printf.
In float the number without decimal point is given with decimal point.
e.g- 40 is given as 40.00
Now we will see how to accept data from user and work on it.
We use the same operators which we use in our normal mathematical operations to do calculations on numbers.
From now onwards I will not write the #include”stdio.h” & #include“conio.h” int the program. It should be assumed and written in the program. Other header files except this header files will only be included.
void main()
{
int x,y,z;
clrscr();
printf(“Enter the first number”);
scanf(“%d\n”,&x);
printf(“Enter the second number\n”);
printf(“\n”);
scanf(“%d”,&y);
z=x+y;
printf(“\n%d”,z);
getch();
}
O/P:- Enter the first number 45
Enter the second number
50
95
************************************************************************
In this program first 3 variables are declared. First number is stored in x, second in y and the addition of x & y is stored in z.
scanf : -This is used to accept input from user. The data types are given in the double quotation and the ampersand sign is used.
See here we have used ‘\n’. This means the control will go to the next line after ‘\n’.
First we have used it after %d. that means the control will go to the next line after accepting the number.
Then we have given it in printf which means it will go to the next line where it is printed.
Then it is used before %d it means that it will go to the next line before printing the output.
Like + operator the operators like -,*,/,% can also be used.
This program can be done in still simpler way.
printf(“Enter the numbers”);
scanf(“%d%d”,&x,&y);
and everything as before.
O/P= Enter the numbers 45 55
100
Like ‘\n’ which is used to go to the next line ‘\t’ is also used which means tab space. It gives one tab space in the program where it is printed or used.
‘%c’ is the data type of characters.
Similarly one more data type is used i.e %s. It is used to accept group of characters. For that the concept of array is used which will be given later.

No comments:

Watch tv!