Saturday, July 26, 2008

C programming Theoretical and Programs

=====================================================

C programming Theoretical and Programs

===========================================================

(1)List the five c compiler?
Ans:
Name Work on O.S Name of microprocessor
1. Turbo c M.S DOS 8086
2. Ansic c LINUX/UNIX 80386
3. Borland c WINDOW 80386
4. Microsoft c M.S DOS 8086
5. Visual c++ WINDOW 80386
Note:- 8086 is 16 bit microprocessor while 80386 is 32 bit microprocessor.

(2) Describe turbo c compiler?
Ans:
Turbo c compiler is one of the most popular c compiler.It is based on DOS operating system.It uses 8086 microprocessor which is 16 bit microprocessor. It has 20 address buses
and 16 data bus. It’s word length is two byte.
(3) What is hexadecimal number system ?
Ans:
In hexadecimal number system we use 16 differen digit so it’s base is 16
TABLE

Hexadecimal digit decimal equivalent binary equivalent
0 0 0000
1 1 0001
2 2 0010
3 3 0011
4 4 0100
5 5 0101
6 6 0110
7 7 0111
8 8 1000
9 9 1001
A 10 1010
B 11 1011
C 12 1100
D 13 1101
E 14 1110
F 15 1111
To convert the binary number into hexadecimal number:
Make the group of four binary digit from right to left put the equivalent hexadecimal digit using TABLE.
e.g binary number =11000111110101
group of four digit from right 11 0001 1111 0101
to make group of four digit of left most digit 11,add two zero to the leftt side i.e 0011
now put the eqivalent hexadecimal digit form table
0011 0001 1111 0101
3 1 F 5
So,equivalent hexadecimal number will be 31F5

(4) What will address range which can repersent in 20 bit ?
Ans:
in binary in hexadecimal

Minimum possible number 0000 0000 0000 0000 0000 0000
Maximum possible number 1111 1111 1111 1111 1111 FFFF
In c any hexadecimal number statr with 0x 0r 0X
So,address range will be 0×0000 to 0xFFFF
It is 1MB memory range.
Note.
2^10 = 1KB
2^20 = 1MB
2^30 = 1GB
Where 10,20,30 are number of bit.

(5)What is difference betweent TSR and TSO program?
Ans :-
TSO means terminate but stay outside.It is those program, which release the main memory after the executon of the program.e.g Vcd cutter, turbo c compiler.
TSR means terminate but stay residence .It is those program, which after the execution of the program does not release the RAM (main memory).e.g antivirus.
(1) Why there are so many c compilers?
Ans:
(3)How many keywords are in c?
Ans:
43
(6)What is difference between .com program and .exe program?
Ans:
Both .com and .exe program are executable program but .com program execute faster than .exe program.All driver are .com program.
(2) How many type of error in c.
Ans:
183
Memory orgnization
To be a good programmer is very necessay to understand the memory structure.

(1) What is memory cell?

Ans:
Entire RAM has divided in number of equal part, which is known as memory cell.Capcity of each cell is to store one-byte data.
i.e char a resevre one memory cell while float a reseve four memory cell.
Each memory cell has unique addresss.Address are always in whole number an incresing order.

(6) What is residence memory?
Ans:
RAM has divided into two parts:
(1) Extended memory (useless)
(2) Residence memory :
When any program is excuted it is stored in the residence memory .For turbo c, it has 1MB residence memory i.e when we open turbo c it store 1MB in the RAM.
(3) What is physical address ?
Ans:
20 bit address of the memory cell is known as physical address or real address.In 20 bit we can repersent address from 0×00000 to 0xFFFFF.
(4) What is segmentation?
Ans:

Residential memory of RAM of size 1MB has divided into 16 equal part.These part is called segment.Each segment has size is 64KB.
1MB=16*64KB
This process of division is known as segmentation.
(5) What is necesity of segmentation?
Ans:
Physical address are 20 bit.But we have no pointer of 20 bit.So pointer can not access whole residential address .So to solve this problem we have three different pointer and segmentation has done.
(6) What is offset address?
Ans:
Each segment has divided into two parts.

1. Segment no (4bit)
2. Offset address (16 bit)
Each segment has same offset address but different segment number.
Suppose physical address is 0×500F1
Then it’s segment number is 5 and offset address is 00F1.
(7) Write a program to find the offset address of any variable?
Ans:
Void main ()
{
int x;
scanf(“%d”,&x);
printf(“%p”,x);
}
Note. %p is used to find the offset address (in hexadecimal) of any variable.
( 8) What is data segment?
Ans:
Segment number 8 has special name which is known as data segment.
It has divided into four parts.

1. Stack area:-
All automatic variables are created into stack area.Default storage class of any local variable is auto.This variable may be int, char, float, array, pointer, struct, union etc.It also return fuction argument and return address.It follow LIFO datastructure. It has two part one for initialize variable another for non-initialize variable.All initialize variable are more nearer than unintialize variable and vice versa.

2. Data area :
All static and extern variable are created in the data area.
3. Heap area:
Malloc and calloc always allocate memory in the heap area.It is used for dynamic memory allocation.It’s size depends upon free space in the memory.
4. Code area:
Fuction pointer can only access code area.Size of this area is always fixed and it is read only area.

(10) What will output:
void main()
{
int a=5,b=6,c=7;
printf(“%d,%d,%d”);
}
Ans:
Output: 7 6 5
Explanation:
Default sotrage class int a=5 is auto.Since it automatic variable it will create in the stack area.
It will store in the stack as

Stack always follows LIFO datastructure.
In the printf statement name of variable is not written explicitly.So default output will content of
Stack which will be in the LIFO order i.e 7 6 5.
(9) what will be output:
void main()
{
int a=5 ,b,c=7;
printf(“%d %d %d”);
}
Ans:
Output: 7 5 garbage value
Explanation:
Automatic variable a and c has initialized while b has not initilize.Initialize variable are more nearer than non initialize variable .They will be stored in the stack.So due to LIFO first output will be 7 then 6 (since a is more nearer than b with respect to c) then any garbage value will be output which is persent in the stack.

(7) How many number system in c.
Ans:
There are three type of number system in c:
1. Decimal number e.g 25
2. Octal number e.g 025
3. Hexadecimal number e.g 0×25

( 8) 010110 is which type of number in c?
Ans:
Octal integer constant (Since it starts with zero)
Note. C has not binary integer constant.

(9) Pointer
(1) What is pointer ?
Ans:
Pointer is variable which contain address of another variable.
e.g
1.
int i=3;
meaning:-

6024 any arbitary address.
2.
int i=3;
int *j;
j=&i;
meanig:

Here j is pointer it contain 6024 which is address of another variable i.
3.
int i=3;
int *j;
int **k;
j=&i; //line 1
k=&j; //line 2

Meaning:

Here both i and j are pointer where j contian 6024 which is address of variable I while j contain 8085 which is address of another variable which contain address of variable i.
Now printf(“%d”, **k);
printf(“%d”,*j);
printf(“%d”,i);
Above all printf will give same output : 3
Explanation:
* and & are canceled to each other i.e *&a=a
So **k
= **(&j) //from line 1
= *(*&j)
= *j //* and & are canceled to each other.
= *(&i) //from line 2
= *&i
= i
Thus **k = i ,hence **k is giving output 3

Same way *j = i
(2) What will output:
void main()
{
int i=3;
int *j;
int **k;
j=&i;
k=&j;
printf(“%u %u %u”,i,j,k);
}
Ans:

Here 6024 ,8085,9091 is any arbitrary address,it may be different.
Since contiant of i is 3,so first output will 3
Contiant of j is 6024 ,so second output will 6024
Containt of k is 8085 ,so third output will 9091
Output: 3 6024 8085
Note:-Address is always a whole number ,which can not be negative so we generally is %u instead of %d

(3) How can I know given statement is correct or not when pointer is very complex format ?
Int
(2) What is null pointer and generic pointer ?
Ans:
Null pointer:- A pointer which dosen’t point any data is known as null pointer.
Generic pointer :-A pointer to the void is known as geric pointer.
e.g void *p;
here p is generic pointer.

(1)How many pointers are in turbo C?
Ans:
3
1. Near pointer (16 bit)
2. Far pointer (32 bit)
3. Huge pointer (32 bit)
(2) What is memory model and how many memory models?
Ans:
According to size of program in code area,data area and stack area
there are six type of memory model:
1. Tiny
2. Small (default)
3. Medium
4. Compact
5. Large
6. Huge
Note: - to change memory model (in turbo c) go to
Option->compiler->code generation.
Memory model decides default type of pointer.

(10) What will output

Void main()
{
int * p,b;
b=sizeof(p);
printf(“%d”,b);
}
Ans:
Output: 2 or 4
Explanation:
Since in this question it has not written p is which type pointer. So it’s output will depends upon which memory model has selected. Default memory model is small.
Memory model default pointer size

Tiny, Small (default), Medium near 2
Compact,Large,Huge far 4

(11) near pointer?
Ans:
It is 16 bit pointer.It can hold the address of variable only within 64KB data segment.It store only offset address of any vriable.This offset address in cyclic in nature .

If you will increment the near pointer then offset address will reach maximum value FFFF (in hexadecimal) then 0000 and so on.
For example :
#include
void main()
{ What is
int a=5,I;
int *ptr; //by default it is near pointer.
ptr=&a;
for(i=0;i<300;i++)
{
printf(“\n %p”,ptr);
p++;
delay(100);
}
}
Output :

Here int two byte data type so every time offset address is increasing by two. %p gives offset address in hexadecimal number. We can perform ++,– ,+,- relation operator (>,<,==,….)

operation on offset address. We cannot perform following task.
1. Addition of two offset address.
2. Multiplication and division of two offset address or one offset address and another number.

(12) What is far pointer?
Ans:
Far pointer is 32 bit pointer .It can hold the address of variable in the data segment as well as outside of the data segment. This type modifier is usually used when compiling in the tiny, small or compact memory model to force the pointer to be far.

It stores both offset address as well as segment number of any variable But when you will increment far address then only offset address will increase (same way as in near pointer).Its segment number never will change.
In dos.h header file there two function that can find offset address and segment address which take the argument far pointer and return unsigned int.
Syntax:
unsigned int FP_OFF(void far *)

unsigned int FP_SEG(void far *)

e.g
#include
void main()
{
unsigned int i,offset_addr,seg_addr,;
char far *ptr=(char far *) 5555 FF99;
for (i=0;i<300;i++)
{
seg_addr=FP_SEG(p);
offset_addr=FP_OFF(p);
printf(“\n seg_addr %x offset_addr %x ”,seg_addr,offset_addr);
p++;
delay(100);
}
}

Output :
You can see its segment address is not changing. In relational operation whole far address is compared.

(13)How we can calculate actual memory address if we know segment and offset address ?
Ans:
Physical address =segment address * 16 + offset address; (in decimal)
Physical address =segment address * 0×10 + offset address; (in hexadecimal)

e.g
Let far address is 0×59994444 (in c hexadecimal integer constant has prefix 0x);
offset address is 0×4444
segment address is 0×5999
temp=segmemt addrss *0×10
=0×59990 (trick : only write one 0 to the right side )
=0×9990 (since offset address can have only for hexadecimal digit ,so remove the last digit)
Physical address =temp+offset address
=0×9990+0×4444
=0×0DDD4 (physical address are 20 bit so it is represented in 5 hexadecimal digit)
Process of conversion of 32 bit far address into 20 bit physical address is called normalization.

Note: It is possible that two different far address can represent same physical address.

(14)What is huge pointer ?
Ans : Is is 32 bit pointer .Its is similar to far pointer but any arithmetic or relational operation is performed in huge address then first it is normalize.

What will be output:
void main()
{
int huge *a=(int huge *)0×59990005;
int huge *b=(int huge *)0×59980015;
if(a==b)
printf(”power of pointer”);
else
printf(”power of c”);
getch();
}
Output: power of pointer

Explanation:

Here we are performing relational operation between two huge address. So first both a and b will normalize.
a=(0×5999)* (0×10)+(0×0005)=0×9990+0×0005=0×9995
b=(0×5998)* (0×10)+(0×0015)=0×9980+0×0015=0×9995
Here both huge address is representing same physical address. So a==b is true.
Note. Two 32 bit address can be represent same physical address but two physical address must be unique.

How will we read complex pointer ?

Rule 1. Give the first priority to the name (identifier) of pointer variable.
Rule 2. Give the least priority of to the data type with modifier (like int,char,float,unsigned int,static int etc.)
Rule 3. () and [] operator has higher priority (associativity left to right ) than * ,& (associativity right to left)
Priority: It means operator which have highest priority will execute first.
Associativity: If associativity is left to right and two operator have same priority then left most operator will have more priority and vice versa.
(to read the priority and associativity of each operator visit in the link operator then precedence table)

How to read
If right side of name (identifier) is ( ) then read function after the function read all as return type
Read [ ] operator array and after this read as it contain
Read * pointer if you have not encounter function or array otherwise read address.
If you will read example then you will understand more easily.

Example 1.
Read the pointer
int (*ptr)();

Ans:
Give first priority to ptr :1

There are two () operator ,associativity of () operator is left to right so left most operator will have more priority.

Left most ( ) operator will execute first . So give * to the second priority and right most ( ) operator to the third priority.
Give fourth priority to int

Read : ptr is pointer to such function which return type is integer data type.

Program:
#include
#include
void main()
{
int (*ptr1)();
int (*ptr2)();
void (*ptr3)();
ptr1=puts;
ptr2=getch;
ptr3=clrscr;
(*ptr3)();
(*ptr1)(”POWER OF POINTER”);
(*ptr2)();
Output: POWER OF POINTER
Example 2.
char *arr [3];
Ans :

arr is array of size 3 which contain address of char

program:
void main()
{
char *arr[3];
char a=1,b=2,c=3;
arr[0]=&a;
arr[1]=&b;
arr[2]=&c;
clrscr();
printf(”%u %u”,&b,arr[1]);
printf(”\n%d”,*arr[2]);
getch();
}
Output :
any addresss same address 3

Example 3
char (*ptr)[5];

Ans:

ptr is pointer to array of size 5 which contain char data type.

Program:
#include
#include
void main()
{
char arr[5]={1,2,3,4,5};
char (*ptr)[5];
ptr=&arr;
ptr=(*ptr)+2;
clrscr();
printf(”%d”,**ptr);
getch();
}
Output: 3
Example 4
unsigned long int ( * avg ())[ 3]

Program:
#include
#include
unsigned long int (* avg())[3]
{
static unsigned long int arr[3]={1,2,3};
return &arr;
}
void main()
{
unsigned long int (*ptr)[3];
ptr=avg();
clrscr();
printf(”%d”,*(*ptr+2));
getch();
}
Output :3

No comments: