Saturday, October 22, 2011

c Pointer basic Basics


C   Pointer Basics Technical Questions


Hi Blog readers,
I am sabari, computer science engineer. Here I would like to Share some of my Interview preparation Question you may worked out (compile and run) from these you will come to know some interesting facts on C Pointer Basics,
Basics
Pointer is a variable that holds another variable’s address.
Pointer Example
#include
#include
int main()
{
    int *p,a;
    float *p1,b;
    p=&a;
    p1=&b;
    a=10;
    //clrscr();
    printf("a=%d\n",a);
    printf("a=%d\n",*p);
    /* pointer size*/
    printf("integer pointer size is %d\n",sizeof(p));
    printf("integer size is %d\n",sizeof(a));
    printf("integer pointer size is %d\n",sizeof(p1));
    printf("Float size is %d\n",sizeof(b));

    //printf("welcome");
    return 0;
}
// Function Pointer
#include
#include
void main()
{
int *p; //pointer for a function
int *f();//function  pointer
int a,b,c;
printf("Enter the three numbers:");
scanf("%d %d %d",&a,&b,&c);
p=f(a,b,c);
clrscr();
//printf("a address is :%u",p);
printf("VALUE OF BIG :%d",*p);
getch();
}
int *f(int x,int y,int z)
{
  static int a;
  a=(x
 // a=a*b;
 // printf("a address is :%u",a);
return(&a);
}
String copy
#include
#include
char source1[25],source2[25];
int main()
{
char *sp1,*sp2;
int r;
printf("welcome TO strcompare:\n Enter the sourc1");
scanf("%s",source1);
sp1= source1;
sp2= source2;
while(*sp1)
{
    *sp2=*sp1;
    sp1++;
    sp2++;
}
printf("the copied string is:%s",source2);
return 0;
}

//String compare
#include
#include
char source1[25],source2[25];
int main()
{
char *sp1,*sp2;
int r,c;
clrscr();
printf("welcome TO strcompare:\n Enter the sourc1");
scanf("%s",&source1);
printf("\n Enter the sourc1");
scanf("%s",&source2);
sp1= source1;
sp2= source2;
while(*sp1!='\o')
{
    if(*sp2==*sp1)
    {
    sp1++;
    sp2++;
     c=1;
    }
   else
  {
  printf("the string is not equl");
  *sp1='\o';
  }
  }
  if(c==1)
  {
     printf("the  string is equal");
     }
return 0;
}
Visit and download contents at:

c Technical basics



Hi Blog readers,
I am sabari, computer science engineer. Here I would like to Share some of my Interview preparation Question you may worked out (compile and run) you will come to know some interesting facts on C Basics,
//No of Args
#include
void main()
{                     
int n,a,b,c;
printf("ENter the 3 nos");
n=scanf("%d%d%d",&a,&b,&c);
printf("No of Args:%d",n);
getch();
}
//Ex2 No of Args
#include
#include
int main()
{
int n,a,b,c;
clrscr();
printf("ENter the 3 nos");
n=scanf("%d%d%d",&a,&b,&c);
printf("No of Args:%d",n);
n=printf(printf("First second"););
printf("No of Args:%d",n);
getch();
return 0;
}

//Digit sum(145),1+4+5=10=1+0=1;
#include
#include
void main()
{
 int n;
 clrscr();
 printf("Enter your no");
 scanf("%d",&n);
 n=n%9;
 if(n!=0)
 {
   printf("the digit sum is %d",n);
  }
  else
  {
   printf("the digit sum is %d",9);
   }
 }
 //Input formatting
 #include
#include

void main()
{
                float x = 12.34567;                              /* The keyword `float' declares the number x
                                                                                                                                   to be a real number. */
                clrscr();

                printf("%f\n", x);
                printf("%8.5f\n", x);
                printf("%8.3f\n", x);
                printf("%-8.3f\n", x);
                printf("%9.2e\n", x);            /* printing real number in exponential form */
                printf("%12.4e\n", x);
                printf("%5e\n", x);
                printf("%8.3f\n", -x);

                getch();
}
#include
#include
int main()
{
int n,f;
clrscr();
printf("Enter the NO:");
scanf("%d",&n);
f=fact(&n);
printf("factorial of given is:%d",f);
getch();
return 0;
}
int fact(int *n)
{
int fact=1,i;
 if(*n==0||*n==1)
   return 1;
  else
{
for(i=1;i<=*n;i++)
 {
   fact=fact*i;
 }
 return fact;
}
 
}
//summation
#include
#include
int main()
{
int n,f;
clrscr();
printf("Enter the NO:");
scanf("%d",&n);
f=sum(&n);
printf("summation of given is:%d",f);
getch();
return 0;
}
int sum(int *n)
{
int sum=0,i;
/*
 if(*n==0||*n==1)
   return 1;
  else
  */
{
for(i=0;i<=*n;i++)
 {
   sum=sum+i;
 }
 return sum;
}
 
}
//swapping ( Four different Techniques)
#include
void main()
{
int a,b;
printf("welcome TO swap\nEnter your two nos:\n");
scanf("%d%d",&a,&b);
swap(a,b);
swap(&a,&b);
printf("The swappwe3d nos:%d%d",a,b);
swapt(&a,&b);
printf("The swappwe3d nos:%d%d",a,b);
swapBit(&a,&b);
printf("The swappwe3d nos:%d%d",a,b);
printf("The swappwe3d nos:%d%d",a,b);
swap1(a,b);
getch();
}
//call by value
int swap(int x,int y)
{
 int t;
 t=x;
 x=y;
 y=t;
 printf("The swappwe3d nos:%d%d",x,y);
}
//call by value
int swap1(int x,int y)
{
 x=(x+y)-(y=x);
 printf("The swappwed nos:%d%d",x,y);
}
//call by Ref
int swap(int *x,int *y)
{
 int t;
 t=*x;
 *x=*y;
 *y=t;

}
int swapt(int *x,int *y)
{
 //int t;
 *x=*x+*y;
 *y=*x-*y;
 *x=*x-*y;

}
int swapBit(int *x,int *y)
{
 //int t;
 *x=*x+*y;
 *y=*x-*y;
 *x=*x-*y;

}
//text compresion
#include
#include
void main()
{
FILE *fp,*fd;
char a,b;
int i=1,j;
clrscr();
gotoxy(10,15);
fp=fopen("intxt.txt","r");
fd=fopen("outtxt.txt","w");
fscanf(fp,"%c",&b);
while(!feof(fp))
{
fscanf(fp,"%c",&a);
if(b==a)
{
i++;
}
else
{
if(i>=2)
{
printf("@");
fprintf(fd,"@");
printf("%c%d",b,i);
i=1;
}
else
{
for(j=0;j
{
printf("%c",b);
fprintf(fd,"%c",b);
} i=1;
}}
b=a;
}
if(i>2)
{
printf("@");
fprintf(fd,"%c",a);
i=0;
}
getch();
}