GoogelAddUnit1

Wednesday 16 March 2011

C Program Compilation.

Regarding C programming, we can now briefly discuss the process of compilation.
Its purpose is to provide an intuitive way for humans to provide instructions that can be easily converted into machine code that is comprehensible to a microprocessor. The compiler is what takes this code, and translates it into the machine code.
To those new to programming, this seems fairly simple. A naive compiler might read in every source file, translate everything into machine code, and write out an executable. This could work, but has two serious problems. First, for a large project, the computer may not have enough memory to read all of the source code at once. Second, if you make a change to a single source file, you would rather not have to recompile the entire application.
To deal with these problems, compilers break their job down into steps; for each source file (each .c file), the compiler reads the file, reads the files it references with #include, and translates it to machine code. The result of this is an "object file" (.o). Once every object file is made, a "linker" collects all of the object files and writes the actual program. This way, if you change one source file, only that file needs to be recompiled and then the application needs to be re-linked.

Processor Directives.

Many times you will need to give special instructions to your compiler. This is done by inserting preprocessor directives into your code. When you begin compiling your code, a special program called the preprocessor scans the source code and preforms simple substitution of tokenized strings for others according to predefined rules. The preprocessor is not a part of the C language.

Syntax Checking.

This step ensures that the code is valid and will sequence into an executable program. Under some compilers, you may get messages or warnings indicating potential issues with your program.When an error is detected in the program, the compiler will normally report the filename and line that is giving trouble.

Object Code.


The compiler produces a machine code equivalent of the source code that can then be linked into the final program. The code itself can't be executed yet, as it has to complete the linking stage.
It's important to note after discussing the basics that compilation is a "one way street". That is, compiling a C source file into machine code is easy, but "decompiling"  is not. Decompilers for C do exist, but they rarely create useful code.

Linking.

Linking combines the separate object codes into one complete program by integrating libraries and the code and producing either an executable program or to Library.

Automation.

many programmers choose to automate compilation, both in order to reduce user interaction requirements and to speed up the process by only recompiling modified files.











Thursday 10 March 2011

C Program to delete the element from the array.


/*  delete an element from the array */                              
#include<stdio.h>
#include<conio.h>
void main()
{
   int a[10],i,k,n,x,f=0;
   printf("\n enter the size");
   scanf("%d",&n);
   printf("\n enter the elements");
   for(i=0;i<n;i++)
     scanf("%d",&a[i]);
   printf("\n enter the element to be deleted");
   scanf("%d",&x);

   for(i=0;i<n;i++)
   {
     if(a[i]==x)
     {
     f=1;
     for(k=i;k<n;k++)
     {
       a[k]=a[k+1];
       n--;
     }
     }
   }
     if(f==0)
       printf("\ element not found");
     else
       {
             for(i=0;i<n;i++)
              printf("%d",a[i]);
       }
       getch();
}

C Program on implementation of Arrays.


/*  insert an element into array  */                                              
#include<stdio.h>
#include<conio.h>
void main()
{
   int a[10],i,k,n,x,p;
   printf("\n enter the size");
   scanf("%d",&n);
   printf("\n enter the elements");
   for(i=0;i<n;i++)
     scanf("%d",&a[i]);
   printf("\n enter the element to be inserted");
   scanf("%d",&x);
   printf("\n enter the position");
   scanf("%d",&p);
   n++;
   for(k=n;k<n;k++)
   {
     a[k]=a[k-1];
     a[p]=x;
   }
   printf("\after insert element");
   for(i=0;i<n;i++)
      printf("%d",a[i]);
      getch();
}

C Program on implementation of for loop.


/*    program for   ex   */                                          
#include<stdio.h>
#include<conio.h>
void main()
{
  float  f,sum,x;
  int i,n,p,t;
  printf("\n enter  x, n");
  scanf("%d%d",&x,&n);
  t=1;
  sum=1;
  for(i=1;i<=n;i++)
    {
      p=i;
      t=t*(x/p);
      sum=sum+t;
    }
    printf("%d",sum);
    getch();
}

C Program on fibonacci suing Recursion.?

/* Fibonacci series using recursion */                 
#include<stdio.h>
#include<conio.h>
int a=0,b=1,c;
main()
{
   int n,k;
   clrscr();
   printf("\n enter the value of n");
   scanf("%d",&n);
   printf("\n %d %d",a,b);
   fib(n,a,b);
   getch();
}
fib(int n,int a,int b)
{
    int c,i=2;
    while(i<n)
    {
      c=a+b;
      printf("  %d",c);
      a=b;
      b=c;
      i++;
      return fib(n-1,a,b);
    }
}

C Program on employee details using structures.

/*  employee details using stuctures  */           
#include<stdio.h>
#include<conio.h>
struct emp
{
 int empno;
 char ename[10];
 int esal;
} ;
main()
{
    struct emp e[10];
    int n,i;
    clrscr();
    printf("\n how many records");
    scanf("%d",&n);
    printf("\n enter the records");
    for(i=0;i<n;i++)
    scanf("%d%s%d",&e[i].empno,e[i].ename,&e[i].esal);
     printf("\n entered records are");
    for(i=0;i<n;i++)
    printf("\n %d\n %s\n %d",e[i].empno,e[i].ename,e[i].esal);
    getch();
}

C Program to check whether the given number is strong or not.?


/*  checking for strong number  */                                                        
#include<stdio.h>
#include<conio.h>
void main()
{
   int n,s,fact=1,sum=0,m,r;
   printf("\n enter the value");
   scanf("%d",&n);
   s=n;
   while(n>0)
   {
      m=n%10;
      fact=1;
      while(m>0)
      {
            fact=fact*m;
            m--;
      }
      sum=sum+fact;
      n=n/10;
   }
   printf("\n strong no...%d",sum);
   if(s==sum)
     printf("\n the given no is strong");
   else
    printf("\n given no is not strong");
      getch();
}

C Program on Student details using structure.


/* Student details using structure */                                                      

#include<stdio.h>
#include<conio.h>
struct stu
{
 int stuno;
 char sname[10];
 int mar;
} ;
void main()
{
    struct stu s[10];
    int n,i;
    clrscr();
    printf("\n how many records");
    scanf("%d",&n);
    printf("\n enter the records");
    for(i=0;i<n;i++)
    scanf("%d%s%d",&s[i].stuno,s[i].sname,&s[i].mar);
     printf("\n entered records are");
    for(i=0;i<n;i++)
    printf("\n %d\n %s\n %d",s[i].stuno,s[i].sname,s[i].mar);
    getch();
}

c Program occurrence of given element in an array.


/* occurrence of given element in an array */                                               

#include<stdio.h>
#include<conio.h>
main()
{
  int a[10],n,m,i,p,count=0;
  printf("\n enter the size of the array");
  scanf("%d",&n);
  printf("\n enter the elements into the array");
  for(i=0;i<n;i++)
    scanf("%d",&a[i]);
  printf("\n enter the element to find");
  scanf("%d",&p);
  for(i=0;i<n;i++)
  {
   if(p==a[i])
    count++;

  }
  printf("\n  %d is exists   %d times in the array",p,count);
  getch();
}

C Program to concatinate two string with out using Builtin functions.


/*  string concatnation without using build functions  */                                
#include<stdio.h>
#include<conio.h>
main()
{
  int i,j,k;
  char a[20],b[20],c[20],d[20];
  clrscr();
  printf("\n enter the three strings");
  scanf("%s%s%s",a,b,c);
  for(i=0;a[i]!='\0';i++)
  {
   d[i]=a[i];
   d[i]=' ';
  }
  for(j=0;b[j]!='\0';j++)
  {
   d[i+j+1]=b[j];
   d[i+j+1]=' ';
  }
  for(k=0;c[k]!='\0';k++)
  {
   d[i+j+k+2]=c[k];
   d[i+j+k+2]='\0';
  }
  printf("\n the combined string is....%s",d);
  getch();
}

C Program to calculate Amstrong number.?


/* Armstrong number */                                                                    

#include<stdio.h>
#include<conio.h>
main()
{
    int n,d,s=0,k;
    printf("\n enter the value");
    scanf("%d",            &n);
    k=n;
    while(n!=0)
    {
     d=n%10;
     n=n/10;
     s=s+(d*d*d);
    }
    if(k==s)
      printf("\n it is armstrong");
    else
      printf("\n it is not armstrong");
    getch();
}

C Program to add two elements using recursion.


/*   adding two elements using recursion  */                                        
#include<stdio.h>
#include<conio.h>
main()
{
    int a,b,c;
    printf("\n enter the a,b values");
    scanf("%d%d",&a,&b);
    c=add(a,b);
    printf("\n %d",c);
    getch();
}
add(int a,int b)
{
   if(a==0)
     return(b);
   else
    return(add(--a,++b);
}

C Program to Calculate the Unique Number.


/* unique number */                                                                                


#include<stdio.h>
#include<conio.h>
main()
{
   int n,d,rev=0,k;
   printf("\n enter the value ");
   scanf("%d",&n);
   k=n;
   while(n!=0)
   {
     d=n%10;
     rev=(rev*10)+d;
     n=n/10;
   }
   printf("\n  %d",rev);
   if(k==(rev*9))
     printf("\n given no is unique");
   else
     printf("\n given is not unique");
     getch();
}

C Program to counting of no of vowels ,words whites, digits, others.

/*   counting of no of vowels ,words whites, digits, others */ 
         
#include<stdio.h>
#include<conio.h>
main()
{
  char a[80],c;
  int i=0,ove=0,con=0,dig=0,whit=0,words=0,othe=0;
  printf("\n enter the string ");
  scanf("%s",a);
  while((c==tolower(a[i++])!='\0'))
  {
    if((c=='a')||(c=='e')||(c=='i')||(c=='o')||(c=='u'))
      ove++;
    else if((c>='a')&&(c<='z'))
      con++;
    else if((c>='0')&&(c<='9'))
     dig++;
     else if(c=='\0')
      {
       whit++;
       words++;
       while((a[i++]==' ')||(a[i++]=='\t'))
       {
    whit++;
    i++;
       }
      }
    else
      othe++;
  }
  whit++;
  printf("\n ove=%d\ncon=%d\ndig=%d\nwhit=%d\nwords=%d\nothe=%d\n",ove,con,dig,whit,words,othe);
  getch();
}

C Program to Calculate Employee Gross and Net Salary.

#include<stdio.h>                           
#include<conio.h>
main()
{
int eno,bs;
float hra=0,da=0,gs=0,pf=0,it=0,ns=0;
char name[20];
clrscr();
printf("Enter the eno,name,bs");
scanf("%d%s%d",&eno,name,&bs);

        if(bs<=5000)
        {
        hra=(bs*10)/100;
        da=(bs*15)/100;
        gs=bs+hra+da;
        pf=(gs*3)/100;
        it=(gs*1)/100;
        ns=gs-(pf+it);
        }
        if((bs>5000)&&(bs<=10000))
        {
        hra=(bs*30)/100;
        da=(bs*20)/100;
        gs=bs+hra+da;
        pf=(gs*5)/100;
        it=(gs*2)/100;
        ns=gs-(pf+it);
        }
        printf("\n Name is =%s", name);
        printf("\n Employee No is =%d", eno);
        printf("\n Basic Salary is =%d", bs);
        printf("\n Gross Salary is =%f", gs);
        printf("\n PF is =%f", pf);
        printf("\n IT is =%f", it);
        printf("\n NET Salary is =%f", ns);
        getch();
        }

C Program to find the Standard Deviation.

/*STANDERD DEVIATION*/     
                 
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int n,x[100],sum,i;
float mean,vsum,ksum,sd;
clrscr();
printf("Enter the n value\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&x[i]);
}
for(i=0;i<n;i++)
sum=sum+x[i];
mean=sum/n;
for(i=0;i<n;i++)
vsum=vsum+((x[i]-mean)*(x[i]-mean));
ksum=vsum/n;
sd=sqrt(ksum);
printf("%f",sd);
getch();
}

C Program to Tower of Honai Problem.

/* tour of hanai */
#include<stdio.h>                           
#include<conio.h>
void main()
{
int n,
char sn='l';
char in='c';
char dn='r';
honai();
printf("Enter number of disks");
scanf("%d",&n);
printf("The tour of honai problem with%d disks",n);
honai(n,sn,in,dn);
printf("\n");
}
honai(int n,int sn,int in,int dn)
{
int n;
char sndl,indl,dndl;
{
if(n!=0)
honai(n-1,sndl,dndl,indl);
printf("move disk %d from %c to %c",n,sndl,dndl);
honai(n-1,indl,sndl,dndl);
}
}

Wednesday 9 March 2011

C Language Short Answer Questions.


Q.1
A __________ is a place on the disk where a group of related data is stored.
(a) disk (b) file
(c) both (d) none
Q.2
Basic operations performed on files is/are__________
(a) opening a file (b) reading data from file
(c) closing a file (d) all the above
Q.3
getw()  is used to__________
(a) read an integer from a file (b) read a string from a file
(c) none
Q.4
ftell() is used to__________
(a) give the current position of a file
(b) give  the specified character position
(c) none
Q.5
fopen() is used to__________
(a) create a new file (b) open an existing file
(c) both (d) none
Q.6
What does FILE *fp indicate?
(a) fp as a pointer to datatype FILE (b) FILE as a pointer to datatype fp
(c) none
Q.7
The mode r is used to open a file for__________
(a) read only (b) write only
(c) none
Q.8
The mode a is used to open a file for__________
(a) read (b) adding data
(c) none
Q.9
The mode w+ is used for__________operations.
(a) reading (b) writing
(c) both (d) none
Q.10
If a non-existing file is opened in read mode then__________occurs.
(a) Error     (b) new file is created
(c) none
Q.11
Standard i/o routines are __________
(a) fopen() (b) getw()
(c) rewind() (d) all of these
Q.12
When end of file is reached getc() returns__________
(a) -1 (b) EOF
(c) both
Q.13
The function putc() is used for__________ purpose
(a) read a character (b) both
(c) write a character (d) none
Q.14
The first argument in fprintf() is used for__________
(a) file pointer (b) file name
(c) both (d) none
Q.15
Status-inquiry library functions are__________
(a) feof (b) ferror
(c) both (d) none
Q.16
Which of the following is an error situation?
(a) Device overflow
(b) opening file with invalid file name
(c) Attempting to write to a write-protected file
(d) all the above
Q.17
The feof() function is used to test__________condition
(a) end of file (b) start of file
(c) none
Q.18
feof() returns a non-zero integer value if__________
(a) error occurs (b) all the data has been read
(c) both
Q.19
The ferror() function is used to report__________
(a) status of file indicated (b) status of error in file
(c) none
Q.20
ferror() takes__________as argument.
(a) pointer (b) file name
(c) file pointer (d) any one of these
Q.21
If an error is detected by ferror(),then it returns __________
(a) zero integer (b) non zero integer
(c) can't say
Q.22
fopen() returns__________ value when file can't be opened using fopen().
(a) NULL (b) -1
(c) 0 (d) can't say
Q.23
fseek(fp,0L,1)  indicates __________
(a) stays at beginning (b) stays at end
(c) stays at current position
Q.24
fseek(fp,-m,1) indicates __________
(a) Go backward by m bytes (b) Go forward by m bytes
(c) none
Q.25
fseek() returns __________ value when file pointer moves beyond the file bound-
aries.
(a) error0 (b) -1
(c) 0 (d) none
Q.26
rewind() is used for__________
(a) reset the position to start of file (b) reset the position to end of file
(c) none
Q.27
The first byte in a file is numbered  __________
(a) 1 (b) 0
(c) can't say
Q.28
fseek()s used for__________
(a) sets the position to start of file (b) sets the position to end of file
(c) sets the position to desired point in file (d) none
Q.29
getc() is used to__________
(a) Read a character (b) read a string
(c) none
Q.30
To store data in a file in the secondary memory __________is/are needed to OS.
(a) Filename (b) Data Structure
(c) Purpose (d) All these

Answers

1. (b) 2. (d) 3. (a) 4. (a)
5. (c) 6. (a) 7. (a) 8. (b)
9. (c) 10. (a) 11. (d) 12. (b)
13. (c) 14. (a) 15. (c) 16. (d)
17. (a) 18. (b) 19. (a) 20. (c)
21. (b) 22. (a) 23. (c) 24. (a)
25. (b) 26. (a) 27. (b) 28. (c)
29. (a) 30. (d)