Saturday 25 March 2017

Pointer



What are Pointers?

pointer is a variable whose value is the address of another variable, i.e., direct address of the memory location. Like any variable or constant, you must declare a pointer before using it to store any variable address. The general form of a pointer variable declaration is −
type *var-name;
Here, type is the pointer's base type; it must be a valid C data type and var-name is the name of the pointer variable. The asterisk * used to declare a pointer is the same asterisk used for multiplication. However, in this statement the asterisk is being used to designate a variable as a pointer. Take a look at some of the valid pointer declarations −
int    *ip;    /* pointer to an integer */
double *dp;    /* pointer to a double */
float  *fp;    /* pointer to a float */
char   *ch     /* pointer to a character */
The actual data type of the value of all pointers, whether integer, float, character, or otherwise, is the same, a long hexadecimal number that represents a memory address. The only difference between pointers of different data types is the data type of the variable or constant that the pointer points to.

How to Use Pointers?

There are a few important operations, which we will do with the help of pointers very frequently. (a) We define a pointer variable, (b) assign the address of a variable to a pointer and (c) finally access the value at the address available in the pointer variable. This is done by using unary operator * that returns the value of the variable located at the address specified by its operand. The following example makes use of these operations −

Pointer variables

In C, there is a special variable that stores just the address of another variable. It is called Pointer variable or, simply, a pointer.
Declaration of Pointer
data_type* pointer_variable_name;
int* p;
EXAMPLE PROGRAM FOR POINTERS IN C:
#include
#include
int main()
{
   int *ptr, q;
   q = 100;
   /* address of q is assigned to ptr */
   ptr = &q;
   /* display q's value using ptr variable */
   printf("%d", *ptr);
   return 0;
}

POINTERS IN C – PROGRAM OUTPUT:

100

NULL Pointers

It is always a good practice to assign a NULL value to a pointer variable in case you do not have an exact address to be assigned. This is done at the time of variable declaration. A pointer that is assigned NULL is called a null pointer.
The NULL pointer is a constant with a value of zero defined in several standard libraries. Consider the following program −
#include
int main () 
{
int  *ptr = NULL;
printf("The value of ptr is : %x\n", ptr  );
   return 0;
}
Output :
The value of ptr is 0.
-In most of the operating systems, programs are not permitted to access memory at address 0 because that memory is reserved by the operating system. However, the memory address 0 has special significance; it signals that the pointer is not intended to point to an accessible memory location. But by convention, if a pointer contains the null (zero) value, it is assumed to point to nothing.
To check for a null pointer, you can use an 'if' statement as follows −
if(ptr) /*succeeds if p is not null*/
if(!ptr) /*succeds if p is null*/
void Pointers:

we can declare a pointrer to be a of any data type void any cannot hold any value.so we have to type cast the pointer variable from one type to other type.


int x=27;

void *p;
p=&x;
printf("%d",(((int*)p));

Output :

The statement (int*)p makes p to become an interger type

Example:


#include stdio.h

#include conio.h
main()

{
     int x=27;
void*p;
p=&x;
printf("value is=%d",*((int *)p));
}

Output:

Value is 27

Pointer and Functions

1.Pointers can be passed as arguments to a functions definition.

2.Passing pointer to a functions is called call by value. 

3.In call by reference what ever changes are made to  formal arguments of the functions definition will affect the actual arguments in calling functions.

4.In call by reference the actual arguments must be pointers or references or addresses.

5.Pointer arguments are use full in functions,because they allow accessing the orginal data in the calling program.

Ex1 :
void swap(int*,int*);
main()
{
int a=10,b=20;

swap(&a,&b);
printf("%d,%d",a,b);
}
void swap(int*x,int*y)
{
int t;
t=*x;
*x=*y;
*y=t;
}


Output: 20  10

Friday 24 March 2017

Patterns

Star Pattern 8

Program :


#include
#include

void main() 

{

int i,j;

clrscr();

    for(i=1;i<=10;i++)
{
    for(j=1;j<=10;j++)
                     {
printf(" * ");
<10 j="" p=""> }
printf("\n");
}
getch();
}



Output :

**********
**********
**********
**********
**********
**********
**********
**********
**********
**********

Star Pattern 7

Program :

#include
#include
void main()
 {
int i,j,k,s=1;
clrscr();
for (i=1; i<=8; i++)
             {
for (k=s; k<=8; k++) 
               {
printf(" ");
}
             for (j=0; j< i; j++)
                             {
           printf("*");
                      }
s = s + 1;
printf("\n");
}
s = 1;
for (i=7; i>=1; i--) {
for (k=s; k>=0; k--)
                  {
printf(" ");
}
             for (j=i; j>=1; j--) 
                           {
              printf("*");
             }
s = s + 1;
printf("\n");
}
getch();
}

Output :
        
              *
            **
          ***
        ****
      *****
    ******
  *******
********
  *******
    ******
      *****
        ****
          ***
            **
              *












Monday 20 March 2017

C program to implement simple if-else in c

Syantax :

if(condition)
{
 statement1;
 statement2;
 . 
 .
 .
 statement n;
}
else
{
 statement1;
 statement2;
 . 
 .
 .
 statement n;
}

Program :

#include
void main()
{
    int num;
    printf(" <<< c program to implement simple if-else >>>\n\n");
    printf("\n Enter the number: ");
    scanf("%d",&num);
    if(num%2==0) 
    {
        printf("\n Entered number is Even Number \n");
    }
    else
    {
      printf("\n Entered number is Odd Number \n\n");
    }
}

Output :



C program for pre increment and post increment

Program :

#include
main()
{
 int i=10,j;

 j=i++; /* j value 10 , i value 11*/

 /*post increment*/

 printf("\n After post-increment: j value:  %d , i value: %d \n",j,i);

 j=++i; /* j value 12, i value 12*/

 /*pre-increment*/

 printf("\n After pre-increment: j value: %d, i value : %d \n",j,i);

 return(0);
}

Output :


C program for pre decrement and post decrement

Program :

#include
main()
{
 int i=20,j;

j=i--;          /* j value 20 , i value 21*/

 /*post decrement*/

 printf("\n After post-decrement: j value: %d , i value:%d \n",j,i);

 j=--i;         /* j value 22, i value 22*/
  
/*pre-decrement*/

printf("\n After pre-decrement: j value: %d, i value:%d \n",j,i);

 return(0);

}

Output :


C in size operator

Program :

#include
void main()
{
  printf("\n size of int is: %d",sizeof(int));
  printf("\n size of float is: %d",sizeof(float));
  printf("\n size of double is: %d",sizeof(double));
  printf("\n size of char is: %d",sizeof(char));

}

Note: In borland C or Turbo C compilers size of int is 2.

Output :


Sum of Series 1+2+3+..+n

Program :

#include
void main()
{
  int sum=0,i,n;
  printf("\n>>> C Program to print Sum of Series 1+2+3+..+n <<<\n");
  printf("\n Enter the number of terms:");
  scanf("%d",&n);
  for(i=1;i<=n;i++)
  {
      sum+=i;
  }
  printf("\n Sum of series upto %d is :%d \n\n",n,sum);

}


Output :




C program the division operation

Program :

#include

#include
int main()
{
     int x,y;
     int result1,result2;
     printf("\n Enter the dividend : ");
     scanf("%d",&x);
     printf("\n Enter the divisor: ");
     scanf("%d",&y);
     result1 = x/y;          
     result2 = x%y;        
     printf("\n The Quotient is: %d ",result1);
     printf("\n The Remainder is: %d ",result2);
     getch();
     return 0;

}


Output ;




Calculate Simple interest

/*    Calculate Simple interest    */

#include

int main()
{
 float p;
 float ir,t;
 float in;

  printf("Type your principle amount : ");
  scanf("%f",&p);

  printf("\n Type your rate of intrest : ");
  scanf("%f",&ir);

  printf("\n Type time (in year) : ");
  scanf("%f",&t);

  in=p*ir*t/100;

  printf("\n You will get intrest : %.2f",in);
  return 0;
}

Output:



Swap the values of two Variables using three variables

/*    Swap the values of two Variables using three variables    */

#include
int main()
{
 int p;
 int q;
 int r;
 clrscr();

 printf("Enter your value of P : ");
 scanf("%i",&p);

 printf("\n Enter your value of Q : ");
 scanf("%i",&q);

r=p;
p=q;
q=r;

 printf("P : %i",p);
 printf("\nQ : %i",q);

 getch();
 return 0;
}

Output:



Simple if statement

Syantax:

if(condition)
{
 statement1;
 statement2;
 . 
 .
 .
 statement n;

}

Program :

#include
void main()
{
    int age;
    printf(" >>> c program to implement simple if<<< \n\n");
    printf("\n Enter the age of the person: ");
    scanf("%d",&age);
    if(age>=18)
    {
        printf("\n Eligible for Licence \n");
    }
    printf("\n program ends \n\n");
}


Output:






C program to sort an array using function

Program :

#include
#include
#define MAX 100     // maximum no of elements of array
int sortArray(int);
int array[MAX];
int main()
{
  int i,size;
  printf("\n>>>> PROGRAM TO SORT ARRAY USING FUNCTION <<<<\n\n");
  printf("\n Enter the size of array: ");
  scanf("%d",&size);
  printf("\n Enter the %d elements of array: \n",size);
  for(i=0;i
  {
   printf("\n array[%d]=",i);
   scanf("%d", &array[i]);
  }
  sortArray(size);    //calling sortArray() function
   printf("\n The Sorted elements of array are:");
   for(i=0;i
   {
     printf(" %d",array[i]);
   }
   getch();
   return 0;
}

sortArray(n)   // function for sorting array elements

{
  int temp=0,i,j;   // temp var is temporary variable used for swapping
  for(i=0;i
  {
    for(j=i+1;j
    {
      if(array[i]>array[j])
      {
        temp=array[i];   //swapping for the array to be sorted
        array[i]= array[j];
        array[j]=temp;
      }
    }
  }

}


Output :



Two Variable of sum

/* Two Variable of sum  */

#include

 int main()
 {
  int var1;
  int var2;
  int var3;

  printf("Enter your frist value to add : ");
   scanf("%i",&var1);

  printf("Enter your second value to add : ");
   scanf("%i",&var2);

  var3 =var2+var1;

   printf("Enter of your value =  %i",var3);

   getch();
   clrscr();  
   return 0;
 }

Output:


Palindrome number using for loop

/* Palindrome number using for loop */

#include

int main()

{
 int n, r, t;
 clrscr();

 printf("Please enter a number: ");
 scanf("%d",&n);

 t=n;

 for(r=0;n>0;n=n/10)
 {
  r = r * 10;
  r = r + n%10;
 }

  if(t==r)
  printf("Number is Palindrome");

  else
  printf("Number is not Palindrome");

  getch();
  return 0;
}

Output:



Palindrome number using while loop

/* Palindrome number using while loop */

#include

int main()
{

 int n, s = 0,t;
 clrscr();

 printf("Please Enter a number: ");
 scanf("%d",&n);

 t=n;

while (n > 0)
 {
  s = s * 10;
  s = s + n%10;
  n = n/10;
 }

 if(t==s)

 printf("Number is Palindrome");

  else
  printf("Number is not Palindrome");
  getch();

}

Output:




Fibonacci Series using while loop

/* Fibonacci Series using while loop */
// 0 1 1 2 3 5 8 13...

#include


int main()

{
    int n,i,p,q,r;
    clrscr();
    
    printf("Enter a number: ");
    scanf("%d",&n);
    
    i=1;
    p=0;
    q=1;
    
    while(i<=n)
    {
        printf("%d ",a);
        r = p + q;
        p = q;
        q = r;
        i++;
    }
        getch();

return 0;
}


Output:



Even And Odd Numbers

#include
  • Two methods available in c programing...

Program
#include

#include

int main()
{
    int i;

    printf("Enter an integer: ");
    scanf("%d", &i);

    if(i % 2 == 0)
        printf("%d is even.", i);
    else
        printf("%d is odd.", i);

    return 0;
};



Output :



(2) Methods Condition Operator



Program

#include 
#include

int main()
{
    int A;

    printf("Enter an integer: ");
    scanf("%d", &A);

 (A % 2 == 0) ? printf("%d is even.", A) : printf("%d is odd.", A);

    return 0;
}
























Output :