• No results found

Static Storage Class

In document 'C' Notes (Page 35-44)

}

/* Function to print a matrix */

matprint(int x[][] ) {

for(i=0;i<n;i++) {

for(i=0;i<n;i++) b[i][j]=a[i][j];

} }

Static Storage Class

Static storage class concerns itself with the permanence of variables. Static variables are declared within a function hence the scope of such variables is the same as that of automatic variables. However as the name suggests. The value of static variables persists unit the end of the program. As a result if a function is exited and is re-entered again at a later stage. The static variables defined within will retain their last values. This feature allows functions to retain their values permanently throughout the execution of the program. A variable can be declared within the function as static using the keyboard static, for example

static int m;

static float n;

A static variable is initialized only once when the program is complied. It is never initialized again and again. In the case of usage of automatic, external and static variables with the same name, local variables are given higher precedence and their values are used instead of the other variables. Initial values can be included to static variables. The rules associated with such declarations are:

1. Only constants should be used to initialize static variables and not experssions.

2. The initial values are retained only as long as the values are not changed in the body of the function. If the values are changed then the latest value is retained .

3. All static variables are automatically initialized to zeros.

Example 1: A program to generate N fibonacci number using static vatriables.

# include < stdio.h>

main {

int i,n;

int fibo ( int );

print f(“Enter number of elements in the series \n”);

scanf (“%d”,&n);

printf (“\n Fibonacci numbers \n\n”);

for (i=1;i<=n;i++)

printf (“%d\t”,fibo (i);

}

/* Function to find fibonacci numbers */

int fibo (int k) {

static int n1=1;

static int n2=1;

int n3;

if (k= =1) n3=0;

else if ( k==2) n3=1;

else {

n3=n1+n2;

n1=n2;

n2=n3;

}

return (n3);

}

Register Variables

In the normal process of execution the value of a veriable is got from the memory to the central processing unit and after the processing is done it is returned back to the memory. However if the value of a variable is repeatedly required like in a looping statement, a lot of computer time is wasted in the process. All this can be taken care of by telling the compiler that a vatriable should be kept in one of the central processing units registers. instead of keeping in the memory. since a register access is much faster than a memory access.

keeping the frequently accessed variables in the register will lead of faster execution of programs. This is done as follows:

register int count;

Example 1: A program to display the number and its square from 1 to 10 using register variable.

# include <stdio.h>

main ( ) {

register int count,sqr;

for ( count =1;count <=10;count++) {

sqr =count *count;

printf (“\n%d”,count, sqr);

} }

Suppose, some of the variables have been declared as register and if these variables are not the correct data type such as char or int and if there are not enough registers available, then the c compiler will automatically ignore the register data type and it keeps them in the memory. It will be treated as automatic variable.

Example 2 : To display that the register variable is created in the CPU.

# include <stdio.h>

main ( ) {

register int y;

printf (“The address of y =%u\n”,&y);

}

Note : The above example generates an error message as the variable is created in the register if the CPU and not in the memory. CPU registers do not have address.

Pointers

Definition of

pointer:-“A pointer is a special variable that can hold the address of the general variables, structures and functions that are used in the program. It contains only the memory location of the variable rather than its contents”.

Pointers are used with

following:- Basic data type variable.

 Array subscript variable.

 Function names.

 Structure & union Names.

Advantage of pointers:

 Pointers are Pointers are used to point to different data types and structures.

 Manipulation of data at different memory locations is easier.

 To achieve a clarity and simplicity.

 More compact and sufficient coding.

 To return multiple value via functions.

 Dynamic memory allocations.

Declaring a pointer variable:

Pointers are declared similar to normal variables, but we must specify when we declare them and what they are going to point to. If we declare a pointer to point an integer, then it can not be used to point a floating- point value.

Pointer operators:

To declare and refer to a pointer variable, C provides two special operators as folowing

:- Address Operator (ampersand) : &

 In-directional operator (Value at operator) : *

& - This operator gives the memory address of the variable.

* - This operator gives the value of variable at that memory address which holds the pointer.

For example:-int a, *p;

p=&a;

*p=100;

Types of pointer variable declaration:

Example :

char *cptr; pointer to character type variables int *iptr; *num pointer to integer type variables float *fptr pointer to float type variables char *name [ 15 ] point to character array Note : * Symbol is part of the variables type.

Example: long int *x, *y;

float *avg, *ratio;

Example: Program to assign the pointer values. (using operator & and *)

# include<stdio.h>

#include <conio.h>

main ( ) {

int x,y; /* xis pointer to integer variable */

clrscr ( );

y=10;

x= &y; /* y value stored in pointer x.*/

printf (“ Address of y=%d\n”,&y);

printf (“value of y=%d\n”,y);

printf(Address of y =%d\n”,x);

printf (“value of y=%d\n”,*x);

} Output

Address of y =65555 Value of y=10 Address of y =65555 Value of y =10

Note: (1) 65555 is a address of x & y it should be unsigned +ve.

(2) Last statement value of y indirectly by using *x. *x-value at address stored by x.

Therefore * is called indirection operator when used in conjunction with pointers.

1006

100 1006

a p

Example: Program to assign the values using operator * and &.

Note: Variable y is assigned to value at the address stored in ipt. since ipt contains address of x, the value at address of x is 8, so ipt is equal to 10.

Example : Program to use arithmetic operations with pointers.

#include <stdio.h>

printf(“value of m = %d\n”,m);

printf(“value of n= %d\n”,n);

printf( “value of k=%d\n”,k);

}

Pointers and Arrays

There is a close association between pointers and arrays, array elements can be accessed using pointers.

Example: Program to reads 10array elements & prints the elements using pointer technique.

#include < iostream.h>

printf (“Enter array values\n”);

for (i=0;i<10;i++) scanf (“%d\n”,&a[i]);

/*arpt points to array */

arpt =a;

/*printing by technique 1 */

for (i=0;i<10;i++) printf (“%d\n arpt +i);

/* printing by technique 2 */

for (i=0;i<10;i++) printf (“%d”, *(arpt ++);

}

Note: arpt is a pointer variable, in the first technique, in the for loop *(arpt +i) it start from element i.e.

*(arpt =0).

In the second technique (* arpt=0) in first cycle then increment operation j is used with the pointer instead of adding loop index to the pointer.

Example: Program to read n number of elements and find the biggest elements among them.

# include <iostream.h>

#include <conio.h>

main ( ) {

int a [100], *arpt, i, big, n;

clrscr ( );

printf (“Enter number of elements: \n”);

sacnf (“%d”, &n);

printf (“Enter number of elements:\n”);

for (i=0;i<n;i++) scanf (“%d”,&a[i]);

/*the first elements address stored in arpt */

arpt =a;

big =*arpt /* first elements value stored in big */

for (i=1;i<n;i++) {

if big <*(arpt +i) big =*(arpt+i);

}

printf (The biggest among the elements =\n”,big);

}

Pointer used in function

It is mechanism by which pointer can be passed as arguments to the function. Thus,the data item of the calling program can be accessed by the called program. No values is copied when pointers are passed as arguments, as in the called by value method. Another important point is that, if the value are changed in the function this will modify the original contents of the actual parameters, this is not true of case of call by value method. when the pointers are are passed as an arguments we must follow the following points .

a. In the calling program,the function is invoked with a function name and addresses of actule parameters enclosed within the parenthesis.

Example:

< function Name >(&var1, &var2, &var3 ...&var n) var –all are actual parameters.

Example: Write a program to find the sum of 5 elements static in nature using pointers with function.

main ( ) {

static int array [5]={200,400,600,800,1000} ; int addnum (int * ptr ); /* function prototype */

int sum ;

sum = addnum (arrays) ;

printf (“Sum of all array elements= %d \n”, sum);

}

int addnum (int *ptr) {

int total =0,index;

for (index =0;index<5;index ++) total +=(ptr +index);

return (total);

}

Structures

Definition :

Structure is a meaningful organized Collection of data items of different type under a unique name we called as structure name. In ‘C’ declaration of such related data items or fields of different types by using reserve word ‘struct’.

Declaration of structure:

Each and every structure must be defined or declared before it appears or using in program.

Syntax:

struct <structure name>

{

<type 1> <field /data 1>

<type 2> <field /data 2>

<type 3 > field /data3>

...

...

<type n > < field /data n >

};

Example :

struct student {

int rollno;

char name [30];

char address [30];

char city[15];

flot markes;

};

Initialization of structure:

Initializing a structure description of structure member is similar to initializing static type declaration.

Example: struct student s1 = {369,”Umesh”, “Pratap Nagar.”, “Jaipur”, 560};

Embedded Structure declaration: [Nested]

It means that, Structure within the another structure is called an embedded structure.

This type of structure declared mainly in two ways that are:

a) Structure may completely defined within the another structure.

b) There may be a separate structure, the embedded structure declared first and the other structure declare declared next.

Example:

The process of structure is mainly concerned with the accessing structure member. Each member of a structure is accessed with dot (.) operator to access a particular member of the structure; the dot operator must be placed between the name of the structure & the name of the structure & the name of the structure member.

Note:- Here e1 is known as a structure variable.

 If structure variable is general variable then data member can access by (.) dot operator.

 If structure variable is pointer variable then data member can access by (->) dot operator.

Examples:

1. Write a program to accept the student details as roll_no, name, city marks using structure and print the

scanf(“%d”,&st.rollno);

printf(“enter the name \n”);

scanf(“%s”,st.name);

printf (“enter the city\n”) scanf(“%d”,st.city);

printf(“enter the Marks\n”);

scanf(“%d”,&st.marks);

/* printing details */

printf (“Roll Number :%d”,st.rollno);

printf(”Name : %s”,st. name);

printf (“City :%s”,st.city );

printf (“Marks: %d”,st.marks) }

Structure used with an array:

However we know that deferent type of data sets cannot be stored an array, so, to overcome this disadvantage structure can be stored along with its members in array structure.

Example: Storing 10-student details structure in an array.

std (array name)

In document 'C' Notes (Page 35-44)

Related documents