PDF

Type of Storage class "register"

Leave a Comment
A 'register' storage class is very similar to 'auto' storage class except one most important property. All register variable in C stores in CPU not in memory.

Properties of register storage class

(1) In following declaration:

register in a;

We are only requesting not forcing to compiler to store variable a in CPU.Compiler will decide where to store in the variable 'a'.

(2) A register variable execute faster than other variables because it is stored in CPU so during the execution compiler has no extra burden to bring the variable from memory to CPU.

(3) Since a CPU have limited number of register so it is programmer responsibility which variable should declared as register variable i.e. variable which are using many time should declared as register variable.

(4) We cannot dereference register variable since it has any memory address.

For Example:

(a)

#include<stdio.h>

int main()
{
register int a = 10;
int *p;
p = &a;
printf("%u",p);
}


Output: Compilation Error


(5) Default initial value of register variable is garbage.

(6) Scope and visibility of register variable is block.


Introduction of Storage class

Leave a Comment
In C there are four types of storage class.
they are 


1. auto

2. register

3. static

4. extern


Storage class is modifier or qualifier of data types which decides :

1. In which area of memory a particular variable will be stored?
2. What is scope of variable?
3. What is visibility of variable?



Visibility of a variable in C:


Visibility means accessibility. Up to which part or area of a program, we can access a variable, that area of part is known as visibility of that variable.


Scope of a variable in C :


Meaning of scope is to check either variable is alive or dead. Alive means data of a variable has not destroyed from memory. Up to which part or area of the program a variable is alive, that area or part is known as scope of a variable.

In the above figure scope of variable a represented outer red box i.e. it is dead then variable must not be visible.


There are four type of scope in C :



1. Block scope

2. function scope

3. File scope

4. Program scope


This will be clear when all the topics are covered.

Type of Storage class "auto"

Leave a Comment
Automatic variables or auto variables are default storage class of local variable. An auto variable cannot be declared globally.

Properties of auto storage class


1 Default initial value of auto variable is garbage.

For example :

#include<stdio.h>
int main()
{
int i;
auto char c;
float f;
printf("%d %c %f",i,c,f);
return 0;
}

Output: Garbage Garbage Garbage

2. Visibility of auto variable is within the block where it has declared.

For example:

(a)

#include<stdio.h>
int main()
{
int a = 10;
{
int a = 20;
printf("%d",a);
}
printf("%d",a);
return 0;
}

Output: 20 10

Explanation: 
Visibility of variable a which has declared inside inner has block only within that block.

(b)

#include<stdio.h>
int main()
{
{
int a = 20;
printf("%d",a);
}
printf("%d",a);     // 'a' is not visible here
return 0;
}

Output: Compilation error

Explanation:
Visibility of variable a which has declared inside inner block has only within that block.


3. Scope of 'auto' variable is within the block where it has declared.

For Example:

#include<stdio.h>
int main()
{
int i;
for(i=0;i<4;i++)
{
int a = 20;
printf("%d",a);
a++
}
return 0;
}

Output: 20 20 20 20

Explanation: Variable declared inside the for loop block has only within that block.
After the first iteration variable a becomes dead and it looses it's incremented value. In second iteration variable 'a' is again declared and initialize and so on.


4. From above example it is clear 'auto' variable initialize each time.


5. An 'auto' vriable gets memory at run time.

Data Type Introduction

Leave a Comment

Introduction


Every programming language deals with some data. For example to print any message it requires charter or string type of data. To solve any mathematics 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

Built-in data types or primitive data types :


  1.  int
  2.  float
  3.  double
  4.  char
  5.  void

int - data type


int is used to define integer numbers.

{
int count;
Count = 5;
}


float - data type


float is used to define floating point numbers.

{
float Miles;
Miles = 4.6;
}


double - data type


double is used to define BIG floating point numbers.
It reserves twice the storage for the number. On PCs this is likely to be 8 bytes;

{
double Atoms;
Atoms = 25000000;
}


char - data type 


char defines characters.

{
char Letter;
Letter = 'a';
}

Derived data types available are 


  1.  array
  2.  pointers
  3.  functions


User defined data types


  1.  structure 
  2.  union
  3.  enumerations


Derived data types and User defined data types will be explained in next sessions.

Data Type Modifiers

Leave a Comment

Modifiers 


The data types explained previously have the following modifiers : 

  1.  short 
  2.  long
  3.  signed
  4.  unsigned


The modifiers define the amount if storage allocated to variable. The amount of storage allocated is not cast in stone. ANSI has the following rules:

short int <= int <= long int

float <= double <= long double


What this means is that a 'short int' should assign less than or the same amount of storage as an 'int' and the 'int' should be less or the same bytes than a 'long int'.

What this means in the real word is:

Bytes:


2 short int
2 unsigned short int
4 unsigned int
4 int
4 long int
1 signed char
1 unsigned char
4 float
8 double
12 long double


Ranges


short int             -32,768 to +32,767

unsigned short int    0 to +65,535

unsigned int         0 to +65,535

int                  -32,768 to +32,767

long int             -2,147,483,648 to + 2,147,483,647

signed char          -128 to +127

unsigned char        0 to +255

float                3.4*10^-38 to 3.4*10^38

double               1.7*10^-308 to +1.7*10^308


Note: One simple way to remember the limits is :
Every 1 byte = 8 bits
so range is form (2^(n-1))  to  (2^(n-1))-1
where 'n' is the no. of. bits

e.g.
for int takes 2 bytes
2 bytes = 16 bits
so n = 16
so range is from (2^15) to (2^15)-1


Qualifiers 


A type qualifier is used to refine the declaration of a variable, a function, and parameters, by specifying whether:

The value of a variable can be changed. The value of variable must always be read from memory rather than from a register.

Standard C language recognizes the following two qualifiers:

1. const :


The const qualifiers is used to tell C that the variable value can not changed after initialisation.

const float pi = 3.24259;

Now pi cannot be changed at a later time within the program. Another way to define constants is with the # define preprocessor which has the advantage that it does not use any storage.

2. volatile :


The volatile qualifier declares a data type that can have its value changed in ways outside the control or detection of the compiler ( such as a variable updates by the system clock or by another program).
This prevents the compiler from optimizing code referring to the object by storing the objects value in a registered re-reading it from there, rather than from memory, where it may have changed. You will use this qualifier once you will become expert in 'C'. So for now just proceed.