Pages

Saturday, 23 February 2019

Storage Classes in C

             Storage Classes are used to describe about the features of a variable/function. These features basically include the scope, visibility and life-time which help us to trace the existence of a particular variable during the runtime of a program.

C language uses 4 storage classes, namely:
1. auto
2. static
3. register
4. extern

1.Program to illustrate auto variable:
#include<stdio.h>
void autoStorageClass(void); // function declaration
int main()
{
    printf("\nA program to demonstrate auto Storage Classess in C");
    autoStorageClass(); //calling function
    return 0;
}

void autoStorageClass() //called function
{
  auto int a = 1;
   {
    auto int a = 2;
     {
      auto int a = 3;
      printf("\n auto int Value of the variable 'a' is : %d",a);
      }
     printf("\n auto int Value of the variable 'a' is : %d",a);
    }
  printf("\n auto int Value of the variable 'a' is : %d",a);
}

output:


2.Program to illustrate static variable: 
#include<stdio.h>
void staticStorageClass(void); // function declaration
int main()
{
  printf("\nA program to demonstrate static Storage Classess in C");
  staticStorageClass(); //calling function
  return 0;
}

void staticStorageClass() //called function
{
    int i = 0;
    printf("\nLoop started:\n");
    for (i = 1; i < 5; i++)   
    {
      static int y = 5;
      int p = 10;
      y++;
      printf("\nAt iteration %d The static int y value is %d\n",i, y);
      p++;
      printf("\nAt iteration %d The non static int p value is %d\n",i, p)
   }
    printf("\nLoop ended:\n");
}

output:

 3.
Program to illustrate register variable: 
#include<stdio.h>
void registerStorageClass(void); // function declaration
int main()
{
 
printf("\nA program to demonstrate register Storage Classess in C");
  registerStorageClass(); //calling function
  return 0;
}

void registerStorageClass() //called function
{
 
printf("\nDemonstrating register class\n\n");
  register char b = 'A' ;
  printf("\n Value of the register variable 'b' : %d", b); // prints ASCII value of A 
  register char x = 'a' ;
  printf("\n Va
lue of the register variable 'x': %d\n",x); // prints  ASCII value of a
}

output:


4. Program to illustrate extern variable:
consider two programs in two separate files  named by externfile1.c and externfile2.c

Program in externfile1.c
// A C program to demonstrate extern storage class
#include<stdio.h>
#include"externfile2.c"
int x=20; // global declaration
void externStorageClass(void); // function declaration
int main()
{
 
int x=10;
  printf("\n The value assigned with out extern to variable  x is :%d",x);
  externStorageClass(); //calling function
  printf("\n The value assigned with extern to variable 
                  x after sub program  is :%d",x); 
 return 0;

}

Program in externfile2.c
#include<stdio.h>
extern int x;
void externStorageClass(void) //called function
{
  printf("\n\n The value assigned with extern to variable  x is :%d",x);
}
 
Now compile the program one i.e externfile1.c the the output of extern storage class is  


Program to illustrate Fibonacci series using recursion

 Program to illustrate Fibonacci series using recursion
//fibonacci series of a given number using recursion
#include<stdio.h>
int s,x,a,b,r;  // global declaration
int fib(int); // function declaration
int main()
{
 int i,n,rev;
 printf("enter any number:\n");
 scanf("%d",&n);
 for(i=0;i<n;i++)
 {
  s=fib(i); // calling function
  printf("%d\t",s);
 }
}

int fib (int x)  // called function
 {
  int a,b;
  if(x <= 1)
    return (x);  // base case
  else
  {
   a=fib(x-1);
   b=fib(x-2);
   return (a+b); // recursive calling
  }
}
output:

Program to illustrate reverse of a given number using recursion

Program to illustrate reverse of a given number using recursion 

//reverse of a given number using recursion
#include<stdio.h>
int sum,r;  // global declaration
int reverse(int); // function declaration
int main()
{
 int n,rev;
 printf("enter any value:\n");
 scanf("%d",&n);
 rev=reverse(n);
 printf("%d reverse is = %d",n,rev);
 return 0;
}

int reverse(int n)
 {
 
  if(n != 0)
    {
     r= n % 10;
     sum=sum * 10 + r;
     n = n / 10;
     reverse(n);
    }
   else
     return sum;
}

output:

Friday, 22 February 2019

Program to find multiplication using recursion.

Program to find multiplication using recursion.

mul(a,b) = a                        b=1 // Base case
              = a + mul(a,b-1)    b>1 // Recursive case

// recursion program on multiplication
#include<stdio.h>
int a,b,c;   /* global variable */
int mul(int,int); /* function declaration */
void main()
{
 int c;
 printf("\n enter any two numbers:\n");
 scanf("%d%d",&a,&b);
 c=mul(a,b);
 printf("multiplication of %d and %d = %d",a,b,c);
}

int mul(int a,int b)
 {
  if(b==1)
  return a;
  else
  c=a+mul(a,b-1);
  return (c);
 }

output:

Program to find factorial using recursion.


Recursion:

            Recursion is the process by which a function calls itself repeatedly.

Program to illustrate factorial of a given number using recursion :

fact(n) = 1                         n=0 // Base case
            = n *  fact ( n -1)    n>0 // Recursive case
// Recursion program on factorial we can have the value upto n=15
#include<stdio.h>
int fact; // global declaration
int factorial(int); // function declaration
void main()
{
int n ;
printf("enter any value:\n");
scanf("%d",&n);
fact=factorial(n); // calling function
printf("factorial of %d = %d",n,fact);
}


int factorial(int n) // called function
{
if(n==0)
return 1;
else
fact=n*factorial(n-1);
return (fact);
}
output:


Program to illustrate Call By Value and Call By Reference

C provides two ways of passing arguments to a function.
 1. Call by value or Pass by value.
 2. Call by reference.

1. Call by value or Pass by value:
            In this method a copy of each of the actual arguments is made first then these values are assigned to the corresponding formal arguments.
            This means that the changes made by the called function have no effect on the values of actual arguments in the calling function.

Program to illustrate call by value or pass by value
#include<stdio.h>
#define pf printf
#define sf scanf
void cbv(int,int); // fuction declaration or prototype
void main()
{
 int a,b;
 pf("\n Enter a value :");
 sf("%d",&a);
 pf("\n Enter b value :");
 sf("%d",&b);
 pf("\n Initial values of a=%d and b=%d\n",a,b);
 pf("\n Calling the function \n");
 cbv(a,b);   // calling function
 pf("\n Final   values of a=%d and b=%d",a,b);
}

void cbv(int p,int q)  // called function
 {
  int temp;
  temp=p;
  p=q;
  q=temp;
  pf("\n Value of p (inside function) = %d\n ", p);
  pf("\n Value of q (inside function) = %d\n", q);
 }

output:


2. Call by reference :
To use call by reference we need to do two things:
 
1. Pass the addresses of the actual arguments instead of passing values to the function.
2. Declare the formal arguments of the function as pointer variables of an appropriate type.
Program to illustrate Call By Reference
#include<stdio.h>
#define pf printf
#define sf scanf
void cbr(int *,int *); // fuction declaration or prototype
void main()
{
 int a,b;
 pf("\n Enter a value :");
 sf("%d",&a);
 pf("\n Enter b value :");
 sf("%d",&b);
 pf("\n Initial values of a=%d and b=%d\n",a,b);
 pf("\n Calling the function \n");
 cbr(&a,&b);   // calling function
 pf("\n Final   values of a=%d and b=%d",a,b);
}

void cbr(int *p,int *q)  // called function
 {
  int temp;
  temp=*p;
  *p=*q;
  *q=temp;
  pf("\n Value of p (inside function) = %d\n ", *p);
  pf("\n Value of q (inside function) = %d\n", *q);
 }

output:

Wednesday, 20 February 2019

Program to illustrate Functions in c

Functions in c 
A function is a set of statements that takes input, do some specific computation and produces output.

Function Declaration
Function declaration tells compiler about number of parameters function takes, data-types of parameters and return type of function. 

Parameter Passing to functions
The parameters passed to function are called actual parameters ,and the parameters received by function are called formal parameters.

There are two most popular ways to pass parameters.
Pass by Value: In this parameter passing method, values of actual parameters are copied to function’s formal parameters and the two types of parameters are stored in different memory locations.
Pass by Reference Both actual and formal parameters refer to same locations, so any changes made inside the function are actually reflected in actual parameters of caller.



Types of Functions in C
  • Function with no argument and no Return value.
  • Function with argument and No Return value.
  • Function with no argument and Return value.
  • Function with argument and Return value

Type 1 Program:
Function with no argument and no return value

// program to illustrate Function with no argument and no Return value.
#include<stdio.h>
#define pf printf
#define sf scanf
void sum (void); // prototype or function declaration    
int main()
 {
  sum(); //calling funtion used parameters are called actual arguments
  return 0;
 }

void sum(void) // called function used parameters are called formal arguments
 {
  int a, b ,r;
  pf("\n Enter a nd b values : ");
  sf("%d %d",&a,&b);
  r=a+b;
  pf("\n sum is %d",r);
 }

output:


Type 2 Program:
Function with argument and no return value

// program to illustrate Function with argument and no Return value.
#include<stdio.h>
#define pf printf
#define sf scanf
void sum (int , int); // prototype or function declaration   
int main()
 {
  int a, b;
  pf("\n Enter a nd b values : ");
  sf("%d %d",&a,&b);
  sum(a,b); //calling function ,used parameters are called actual arguments
  return 0;
 }

void sum(int p , int q) // called function ,used parameters are called formal arguments
 {
  int r;
  r=p+q;
  pf("\n sum is %d",r);
 }


output:


Type 3 Program:
Function with no argument and return value

// program to illustrate Function with no argument and Return value.
#include<stdio.h>
#define pf printf
#define sf scanf
int sum (void); // prototype or function declaration    
int main()
 {
  int c;
  c=sum(); //calling funtion ,used parameters are called actual arguments
  pf("\n sum is %d",c);
  return 0;
 }

int sum (void) // called function ,used parameters are called formal arguments
 {
  int a,b,r;
  pf("\n Enter a nd b values : ");
  sf("%d %d",&a,&b);
  r=a+b;
  return r;
 }

output:


Type 4 Program:
Function with argument and return value

// program to illustrate Function with argument and Return value.
#include<stdio.h>
#define pf printf
#define sf scanf
int sum (int , int); // prototype or function declaration    
int main()
 {
  int a,b,c;
  pf("\n Enter a nd b values : ");
  sf("%d %d",&a,&b);
  c=sum(a,b); //calling function ,used parameters are called actual arguments
  pf("\n sum is %d",c);
  return 0;
 }

int sum (int p,int q) // called function ,used parameters are called formal arguments
 {
  int r;
  r=p+q;
  return r;
 }

output:


Programs in turboc3 : Files

File Handling in C File Handling concept in C language is used for store a data permanently in computer. Using this concept we can store our...