Friday, November 14, 2014

C Function Of Reference



Program for C function

#include
#include
float square ( float x );                               
int main( )               
{
 
        float m, n ;
        clrscr();
        printf ( "\nEnter some number for finding square \n");
        scanf ( "%f", &m ) ;
        n = square ( m ) ;                      
        printf ( "\nSquare of the given number %f is %f",m,n );
}
float square ( float x )   
{
        float p ;
        p = x * x ;
        return ( p ) ;
}

Output

Enter some number for finding square
2
Square of the given number 2.000000 is 4.000000

Program for C function

#include
#include
void swap(int a, int b);          
int main()
{
    int m = 22, n = 44;
    clrscr();
    printf(" values before swap  m = %d \nand n = %d", m, n);
    swap(m, n);                         
}
 
void swap(int a, int b)
{ 
    int tmp;
    tmp = a;
    a = b;
    b = tmp;
    printf(" \nvalues after swap m = %d\n and n = %d", a, b);
    getch();
}

Output

values before swap m = 22
and n = 44
values after swap m = 44
and n = 22

Program for C function using call by reference

#include
#include
void swap(int *a, int *b); 
int main()
{
 
    int m = 22, n = 44;
    printf("values before swap m = %d \n and n = %d",m,n);
    swap(&m, &n);         
}
void swap(int *a, int *b)
{
    int tmp;
    tmp = *a;
    *a = *b;
    *b = tmp;
    printf("\n values after swap a = %d \nand b = %d", *a, *b);
}

Output

values before swap m = 22 
and n = 44
values after swap a = 44
and b = 22

Program for memset() function in C

#include 
#include
#include 
#include 
int main()
{
   int i;
   char *a = (char *) malloc(5*sizeof(char));
   printf("Values before memset\n");
   for (i = 0; i < 5; ++i)
       printf("  a[%d] = %d ,", i,    a[i]);
   memset(a, 3, 5*sizeof(char));
      printf("\nValues after memset\n");
   for (i = 0; i < 5; ++i)
       printf("  a[%d] = %d ,", i,    a[i]);
      free(a);
   return 0;
}

 Output

Values before memset
a[0] = 0 , a[1] = 0 , a[2] = 0 , a[3] = 0 , a[4] = 0
Values after memset
a[0] = 3 , a[1] = 3 , a[2] = 3 , a[3] = 3 , a[4] = 3

No comments:

Post a Comment