Friday, June 22, 2012

Data Type Tutorials

For java Tutorial log on to myjavaquestionbank.blogspot.in                                  Introducing C data type

Every programming language deals with some data. For example to print any message it requires charterer or string type of data. To solve any mathematic expression it requires integral as well as real number (floating type) of data. C is very rich in data type. We can broadly divide all data type in c in three categories:

1. Primitive or fundamental data type
2. Derived data type
3. User defined data type

List of data type in c


A complete picture of all c data types has been represented by following figure.
Note: Apart from these basic data type there are few other data types which have been defined inside header files which will be discussed later. 

C data type modifiers

There are some keywords in c which modify the meaning the meaning of above mentioned basic data type in c. On the basis of properties of modifier we can categories the modifier in following eight groups.

1. Size modifier
2. Signed modifier
3. Constant modifier
4. Volatile modifier
5. Storage class
6. Pointer modifier
7. Function modifier
8. Interrupt
All modifiers in c have been represented by following table:

In the above table modifier ending with * indicates they are not keyword of c language. These modifiers are called as nothing modifier since there is not any special keyword in which represents those modifiers. If you will not write any thing it then compiler will understand you are writing nothing modifier of those groups.
Meaning of following word in the above table:
nothing: It is not short as well as not long.
not_const:  It is not constant. You can modify.
Not_volatile: It is not volatile.
not_interrupt: It is not sending interrupt signal.
Important points:
1. Nothing modifier must be default modifier of that group.
2. In LINUX GCC compiler there is not any concept of pointer modifier.

Default modifier in c

    If you will not write any modifiers of a particular group then c compiler will take default modifier of that group. Default modifier of each group has written in the following table:
 
1. Default modifier of storage class is auto when we declared the variable inside any function and default modifier of storage class is static when we declared variable outside of all functions. In other word we can say if variable has declared locally then default storage class is auto and if it has declared globally then default storage class of variable is extern.

2. Default storage class of function is extern.

3. Default modifier of pointer modifier depends upon memory model. For detail knowledge click following link:
WHAT IS MEMORY MODEL IN C?

Modifiers in c


Explanation of modifiers in c programming language by examples and questions
Rules for using modifier in c
Rule 1: We cannot use two modifiers of same groups in any particular data type of c.
For example, following declaration of c are illegal:
short long int i;
static auto char c;
signed unsigned int array[5];
pascal cdecl display();
Following are valid declaration of c:
const volatile float f;
signed static long volatile int i;
Question: Is following declaration is valid in c?
1.   intnear * far * huge *p;
2.  char const * const *c;
3.  short short int i;
4.  const const int i;
Rule 2: We can write modifier either before the data type or after the data type. For example, both of following declaration is correct:
unsigned char c;
char unsigned c;
Rule 3: Order of modifier including data type doesn’t affect the meaning of declaration. For example all of the following have same meaning:
int const short extern i;
int extern const short i;
int short extern const i;
const int short extern i;
extern short const int i;
Rule 4: There is one exception in rule 3. POINTER, FUNCTION and INTERRUPT modifier must be written after the data type. For example, in the following declaration:
unsigned const char far *c;
char unsigned const *c;
char far unsigned const *c;
const char far unsigned *c;
far char const unsigned *c;
const unsigned far char *c;
First four declarations are valid as well as equivalent. But last two declarations are invalid.

Range of data types in c

Following table illustrate the range or maximum or minimum value of data types in TURBO C++ and Borland c++ compilers.  
 


Note: In the above table range of float, double and long double has written only for positive numbers. But this range is also true for negative numbers i.e. for range of float is -3.4*10^38 to -3.4*10^ (-38) and so on.
 
Interview Question: Why range of signed char is -128 to 127 not -127 to 128?

const modifier in c

Explanation of const modifier in c programming language by examples, questions and answers:

In c all variables are by default not constant. Hence, you can modify the value of variable by program. You can convert any variable as a constant variable by using modifier const which is keyword of c language.
Properties of constant variable:
1. You can assign the value to the constant variables only at the time of declaration. For example:
const int i=10;
float const f=0.0f;
unsigned const long double ld=3.14L;
2. Uninitialized constant variable is not cause of any compilation error. But you cannot assign any value after the declaration. For example:
const int i;
If you have declared the uninitialized variable globally then default initial value will be zero in case of integral data type and null in case of non-integral data type. If you have declared the uninitialized const variable locally then default initial value will be garbage.
3. Constant variables executes faster than not constant variables.
4. You can modify constant variable with the help of pointers. For example:
#include
int main(){
    int i=10;
    int *ptr=&i;
    *ptr=(int *)20;
    printf("%d",i);
    return 0;
}
Output: 20

Wednesday, June 13, 2012

Memory Mapping Fully Explained


For Complete Java Guidance(J2SE,J2EE) you can switch over to myjavaquestionbank.blogspot.in

This tutorial explains  
                                     1. C compiler.
                                     2. Turbo C compiler.
                                     3. Hexadecimal Number System.
                                     4. Address Range represented in 20 bit.
                                     5. Difference Between TSO and TSR program.
                                     6. Difference between .com program and .exe program.
                                     
                                     7. Memory cell.
                                     8. Residence memory.
                                     9. Physical Address.
                                     10. Segmentation.
                                     11. Offset Address.
                                     12. Data segement.



C compiler
There are various c compilers are variables. Some of these are:
S.N.
Name
Microprocessor
OS
1
Turbo c 3.0
8086
MS DOS
2
ANSIC C
80386
LINUX
3
Borland C 4.0
80386
WINDOW
4
Microsoft C
8086
MS DOS
5
Visual C++
80386
WINDOW

Note: 8086 is 16 bit microprocessor while 80386 is 32 bit microprocessor.


Note: Different versions of compilers are based on the different microprocessors and support many OS. It is always changing. As a programmer you should know the microprocessor name, its world length etc. which your compiler is based on. Since c language is platform dependent. In preprocessor section you will know how to make a program as much as platform independent.


Turbo c compiler

Turbo c is an IDE of c programming language created by Borland. Turbo C 3.0  is based on MS DOS operation  system. It is one of the most popular c compilers. It uses 8086 microprocessor which is 16 bit microprocessor. It has 20 address buses and 16 data bus. Its word length is two byte. 


Size of data types in Turbo C 3.0:


Data type
Size
short int
2
int
2
long int
4
char
1
float
4
double
8
long double
10

Byte ordering : Little Endianness
Default pointer : Near
Default memory model : Small


To compile a c program: Alt + F9
To run a c program: Ctrl + F9

Turbo C 4.5 is based on Microsoft window operating system. It is 32 bit compilers.
Size of data type in Turbo C 4.5:

Data type
Size (Byte)
short int
2
int
4
long int
4
char
1
float
4
double
10
long double
12

Default pointer: Far
Default memory model: Compact

Hexadecimal representation in c
In hexadecimal number system we use 16 different digits so its base is 16. List of all hexadecimal digits:

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 digits from right to left and replace the four binary digits with the equivalent hexadecimal digit using above table.
For example:

Binary number = 11000111110101
Group of four digits from right side:


to make group of four digit of left most  digit 11 , add two zero at the left side i.e. 0011

Now put or replace with it the equivalent hexadecimal digit using above table


So, equivalent hexadecimal number will be 31F5.


What will be address range which can be represented in 20 bit?

Binary
Hexadecimal
Min possible
0000 0000 0000 0000 0000                            
00000
Max possible
1111 1111 1111 1111 1111    
FFFFF


In c any hexadecimal number start with 0x 0r 0X So, address range   will be 0x00000 to 0xFFFFF. So in turbo C 3.0 memory address of all variables must be within 0x00000 to oxFFFFF.

It is 1MB memory range.

Note.
2^10 = 1KB
2^20 = 1MB
2^30 = 1GB  

Where 10, 20, 30 are number of bit.

Difference between TSR and TSO program

Difference between TSR and TSO program


TSO means terminate but stay outside. It is that program, which release the main memory after the execution of the program. Example ms paint, notepad, turbo c compilers etc.

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.
Difference between .com program and .exe program in c programming language

Both .com and .exe program are executable program but .com program execute faster than .exe program. All drivers are .com program. .com file has higher preference than .exe 
For example:
Open the command prompt in window OS. (Type CMD in Run)
In the command prompt type notepad and press enter key you will get the notepad. Since it executes notepad.exe 


Repeat the same task but now first create any .com file in the same working directory. We can create it as open the notepad save it as notepad.com, set save as type is All files or we can create the .com file from command prompt.


Then type notepad in command prompt and press the enter key you will get error message like:   
C:\notepad.com is not a valid Win32 application.


It proves that .com has higher precedence than .exe program.

Com file is binary execute used in MS DOS. Com program keeps only code and data. It stores code and data in one segment. In Turbo C memory model should tiny to create .com program. We will discuss later how to create .com file in in turbo c.



Memory cell in computer


Entire RAM has divided in numbers of equal parts, which are
known as memory cells. Following diagram represents the 256
MB RAM.


Each cell can store one-byte data. Data are stored in the
binary number system. That is a character data reserves one
memory cell while floating data reserves four memory cells.

Each memory cell has unique address. Address is always in
whole number and must be in increasing order. We will discuss
how a characters, integers etc. data are in stored in the
data type chapter. Just for now assume

int a = 4;

Here variables a stores in the memory in the flowing way:


If you know memory address of first cell is 0x5000 then
what would be the memory address of next memory cell?

It will 5001 since integer data always stores at
continuous memory location and as we know memory address
always in increasing order.


What is residence memory in c programming language?
RAM has divided into two parts:
(1)        Extended memory (useless)
(2)        Residence memory 






















































In Turbo C 3.0 compiler size of residence memory is 1MB.
Residence memory:
 
When any program is executed it is stored in the
residence memory. For turbo c 3.0, it has 1MB residence
memory i.e. when we open turbo c 3.0 it stores 1MB in the
RAM.

Segmentation in operating system


Segmentation in c programming language

Residential memory of RAM of size 1MB has divided into 16 
equal parts. These parts is called segment. Each segment 
has size is 64 KB.

16 * 64 KB = 1 MB






This process of division is known as segmentation.



Note: In turbo c 3.0 physical addresses of any variables

are stored in the 20 bits. But we have not any pointers 

(We will discuss later what is pointer?)of size 20 bits.
So pointers cannot access whole residence memory 
address.To solve this problem we there are three types
pointers in c language. They are

1. Near pointer
2. Far pointer
3. Huge pointer

Data segment in c

All the segments are used for specific purpose. Like segment number 15 is used for ROM, segment number 14 is used for BIOS etc.  



We will discuss about how to access text video memory, graphics video memory in the pointer and union chapters of 255 bits color graphics programming.
Segment number eight has special name which is known as data segment. This segment has been divided into four parts. This is very important for c programming




1. Stack area
All automatic variables and constants are stored into stack area. Automatic variables and constants in c:
1.       All the local variables of default storage class.
2.       Variables of storage calls auto.
3.       Integer constants, character constants, string constants, float constants etc in any expression.
4.       Function parameters and function return value.
Variables in the stack area are always deleted when program control reaches it out of scope. Due to this stack area is also called temporary memory area. For example:
What will be output of following c code?
#include
int main(){
    int i;
    for(i=0;i<3;i++){
         int a=5;
         printf("%d",a);
    }
    return 0;
}
Output: 5 5 5
Explanation: Since variable a is automatic variable, it will store in the stack area. Scope of variable a is within for loop. So after each iteration variable a will be deleted from stack and in each iteration variable a will initialize.  
This two concepts are only for Turbo C 3.0
It follows LIFO data structure. That in the stack area of memory data is stored in last in first out. For example:
What will be output of flowing c code. (Turbo c 3.0)?
#include
int main(){
    int a =5, b = 6, c = 7,d =8;
    printf("%d %d %d");
    return 0;
}
Output: 8 7 6
Explanation:
Default storage class of variables a, b, c and d is auto .Since it automatic variable it will be sorted in the stack area of memory. It will store in the stack as


Stack always follows LIFO data structure. In the printf function, name of variables is not written explicitly. So default output will content of stack which will be in the LIFO order i.e. 8 7 6.
It has two part one for initialize variable another for non-initialize variable. All initialize variables are more nearer than not initialized variable and vice versa. For example:
What will be output of following program (Turbo c 3.0)?
#include
int main(){
    int a =5, b, c =7;
    printf("%d %d %d");
    return 0;
}
Output: 7 5 garbage value
Explanation:
Automatic variable a and c has initialized while b has not initialized. Initialize variables are more nearer than uninitialized 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 present in the stack.
Note: Default storage class of any local variable is auto.
2. Data area:
All static and extern variable are stored in the data area. It is permanent memory space and variable will store in the memory unless and until program end. For example:
What will be output of following c code?
#include
int main(){
    int i;
    for(i=0;i<3;i++){
         static int a=5;
         printf("%d",a);
    }
    return 0;
}
 Output: 5 6 7
3. Heap area:
This memory area is use to allocate memory dynamically. In c we can allocate the memory space dynamically by using function malloc and calloc. It always allocates memory in the heap area. Its size is variable and depends upon free space in the memory.
4. Code area:
Function pointer can only access code area. Size of this area is always fixed and it is read only memory area.