Sunday, January 1, 2012

What IsDangling Pointer ?

DAnging Pointer :::::::
If any pointer is pointing the memory address of any variable , but after sometime the variable is deleted  while the pointer is still pointing such memory location. Such pointer is known as dangling pointer .



  









e.g :
  

#include<stdio.h>

int *call();
void main(){

int *ptr;
ptr=call();

fflush(stdin);
printf("%d",*ptr);

}
int * call(){

int x=25;
++x;

return &x;
}


Output :::::
 Garbage Value

variable x is local variable. Its scope and lifetime is within the function call hence after returning address of x variable x became dead and pointer is still pointing ptr is still pointing to that location.

Saturday, December 24, 2011

C Program to demonstrate the use of static variables

#include<stdio.h>
#incl.ude<conio.h>
void stat();
void main()
{
    int c;
    clrscr();
     for(c=1;c<=3;c++)
     stat();
     getch();
}

void stat()
{
  static int sv=0;
  sv=sv+1;
  printf("%d\n",sv);
}

Thursday, December 15, 2011

C Program to find Hcf and Lcm Of numbers

int hcf(int n, int m) 
{ 
 
    int temp; 
    while( m > 0) 
{ 
        temp = n; 
        n = m; 
        m = temp % m; 
    } 
    return n; 
} 
 
int lcm(int n, int m) { 
    int product = n*m; 
    int hcf = hcf(n,m); 
    int lcm = product/hcf; 
    return lcm; 
} 
int main() 
{ 
    int n, m; 
    scanf("%d%d",&n,&m); 
    printf("%d\n",lcm(n,m)); 
    return 0; 
}
Mr. Denis Ritchie was found dead on October 12, 2011, at the age of 70 at his home in Berkeley Heights, New Jersey, where he lived alone. First news of his death came from his former colleague, Rob Pike. The cause and exact time of death have not been disclosed. He had been in frail health for several years following treatment for prostate cancer and heart disease. His death came a week after the death of Steve Jobs, although Ritchie's death did not receive as much media coverage.Computer historian Paul E. Ceruzzi said after his death: " Ritchie was under the radar. His name was not a household name at all, but... if you had a microscope and could look in a computer, you'd see his work everywhere inside.

!!.... Hats Off to One of the GEMS of the Computer World....!!
.....May his Soul REST IN PEACE...................

Sunday, November 27, 2011

C Program for Short Instant Quiz


#include<stdio.h>
#include<conio.h>
#include<string.h>
void prn();
int questions(char []);
int m1=0;
int m=0;
void main()
{
int i;
long ht;
char name[20];
char f1[10];
char ch[50]=" STATE TRUE OR FALSE . EACH CARRIES ONE MARK ";
clrscr();
prn();
 gotoxy(5,4);
 printf(" Student Name : ");
 scanf("%s",&name);
 gotoxy(5,6);
 printf(" HALL TICKET NUMBER : ");
 scanf("%ld",&ht);
clrscr();
prn();
gotoxy(2,5);
printf("%s",ch);
gotoxy(2,6);
for(i=0;i<strlen(ch);i++)
printf("-");
printf("\n\n");
questions(f1);
clrscr();
prn();
printf("\n\n");
printf(" STUDENT NAME  : %s",name);
printf("\n HALL-TICKET NUMBER  : %ld",ht);
printf("\n TOTAL MARKS  : %d",m);
getch();
}


void prn()
{
gotoxy(25,2);
printf(" C EXAMINATION ");
gotoxy(25,3);
printf(" ----------- ");
}




int questions(char f[])
{
printf(" 1. Structure is not a data type : ");
scanf("%s",&f[0]);
if(f[0]==102 || f[0]==70)
m++;
printf("\n");


printf(" 2. C Program is a collection of functions : ");
scanf("%s",&f[1]);
if(f[1]==116 || f[1]==80)
m++;
printf("\n");


printf(" 3. In an array we can accept more than one value of same type : ");
scanf("%s",&f[2]);
if(f[2]==116 || f[2]==84)
m++;
printf("\n");


printf(" 4. String is a character array : ");
scanf("%s",&f[3]);
if(f[3]==116 || f[3]==84)
m++;
printf("\n");


printf(" 5. Any function cannot be called from any other function  : ");
scanf("%s",&f[4]);
if(f[4]==102 || f[4]==70)
m++;
printf("\n");


printf(" 6. fflush() is a macro  : ");
scanf("%s",&f[5]);
if(f[5]==102 || f[5]==70)
m++;
printf("\n");


printf(" 7. When the file is opened in 'a+' mode modification is possible  : ");
scanf("%s",&f[6]);
if(f[6]==102 || f[6]==84)
m++;
printf("\n");


printf(" 8. Break is a reverse word  : ");
scanf("%s",&f[7]);
if(f[7]==116 || f[7]==84)
m++;
printf("\n");


printf(" 9. STRCHR finds the first occurence of a given character in a string  : ");
scanf("%s",&f[8]);
if(f[8]==116 || f[8]==84)
m++;
printf("\n");


printf(" 10. exit() fn is present in iomanip.h  : ");
scanf("%s",&f[9]);
if(f[0]==102 || f[0]==70)
m++;


return m;
}

Wednesday, November 2, 2011

C Program to find The Acromatic Of a String

#include<stdio.h>
#include<conio.h>
void main()
{
    char ch[100];
    int i;
    clrscr();
    printf(" Enter any String : ");
    /* to accept multi world string gets() is used */
    gets(ch);
    i=0;
    printf("\n%c",ch[0]);
    while(ch[i]!='\0')
    {
      if(ch[i]==' ')
      {
          i++;
      printf("%c",ch[i]);
      }
      i++;
     }
     getch();
}

Tuesday, October 11, 2011

C Program to find twin prime numbers

#include<stdio.h>
#include<conio.h>
void main()
{
  int n,i,k,r,a[50],x;
  clrscr();
  printf(" Enter Range : ");
  scanf("%d",&r);
  i=1;
  x=0;
  while(i<=r)
  {
     k=0;n=1;
     while(n<=i)
     {
        if(i%n==0)
            k++;
            n++;
     }
     if(k==2)
     {
        a[x]=i;
        x++;
     }
     i++;
  }

  for(n=0;n<x;n++)
  {
      if(a[n+1]-a[n]==2)
      printf("\n %d and %d are twin prime numbers ",a[n],a[n+1]);
  }
  getch();
}