Tuesday, 1 March 2016

Compilation and execution of programs

                                    Compilation and execution 

  • There are many steps involved in converting a C program into an executable form, these all steps are called "build process".
  • Build process are representing in the following graphically diagram : 



If you don't understand the build process, don't worry,read following explanations of build process i.e. how a source program make executable program. It is all internal process of machine,that's done in neno seconds.

Lets now understand the steps mentioned in above figure in detail.
  1. Editor-Type your program in editor(source code).
  2. Preprocessing-During this step, the C source code is expanded based on the preprocessor directives like as #include#ifdef#define etc. The expanded source code is stored in an intermediate file with .i extension.
  3. Compilation- The expanded source code is then passed to the compiler, which identifies the syntax error in the expanded source code. If the expanded source code is error free, then the compiler transfer the expanded source code in C, into an equivalent assembly language program. The assembly code is typically stored in .ASM file. So our first.c file would be changed and stored in first.ASM.
  4. Assembling - Assembler translate the .ASM program into Relocatable Object code. Thus assembler translate our first.asm file into first.OBJ. .OBJ file is one of the binary file. This object file contained header and several section.
  5. Linking - Linking is the final step of build process i.e. creating an executable program. It's do following works :
    • Find definition of all external function
    • Find definition of all global variables
    • Combine Data Section
    • Combine Code Section
  6. Loading - Once the .EXE file is created and stored on the disk,it is ready for execution. when we execute it, it is first brought from the disk into the memory (RAM) by an OS component called Program Loader.

Simple interest

Q. Write an interactive program to calculate simple interest.

Ans.

#include<stdio.h>
#include<conio.h>
int main()
{
 float p,rate,time,si;
 printf("Enter principal amount : ");
 scanf("%f", &p);
 printf("Enter rate of interest : ");
 scanf("%f", &rate);
 printf("Enter time period in  year : ");
 scanf("%f", &time);
/*calculating simple interest*/
 si=(p*time*rate)/100;
 printf("\nSimple Interest = %2f",si);
 getch();
 return 0;

}

Output:

Enter principal amount : 1000
Enter rate of interest : 2
Enter time period in  year : 1

Simple Interest =20

Compound Interest

Q. Write an interactive program to calculate compound interest.

Ans.

#include<stdio.h>
#include<math.h>
#include<conio.h>
int main()
{
 float p,rate,time,ci;
 printf("Enter principal amount : ");
 scanf("%f", &p);
 printf("Enter interest rate : ");
 scanf("%f", &rate);
 printf("Enter time period in year : ");
 scanf("%f", &time);
 //calculate ci
 ci=p*(pow((1+rate/100),time)-1);
 printf("\nCompound interest = %f",ci);
 return 0;

}

Output:

Enter principal amount : 48500
Enter interest rate : 6
Enter time period in year : 2

Compound interest = 5994.600098

Remove vowel in string

Q. write a C program for remove vowel in string and find the length of resulted string.

Ans.

#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
 int len=0,i,j=0;
 char str[40],new_str[30];
 printf("Enter any string : ");
 gets(str);
 for(i=0; i<=strlen(str); i++)
 {
   if((str[i]=='a' || str[i]=='e' || str[i]=='i' || str[i]=='o' || str[i]=='u') ||(str[i]=='A' || str[i]=='E' || str[i]=='I' || str[i]=='O' || str[i]=='U' ))
   {
     str[i]=' ';
   }
   else
   {
     len++;
     new_str[j++]=str[i];
   }
 }
 //Add null in new string
 new_str[j]='\0';
 printf("New vowel less string : %s",new_str);
 printf("\nLength of new string = %d",len);
 getch();
 return 0;

}

Output:

Enter any string :John Cena Is Awesome
New vowel less string: Jhn Cn s wsm

Length of new string = 13

Power Function

Q. Write a C program to create function power and calculate the power of any number.
For example:
Assume number = 5
Power = 3
Result = 5*5*5 = 125


Ans:

#include<stdio.h>
int power(int , int );
int main()
{
 int num,pow,res;   //res = result
 printf("Enter any number : ");
 scanf("%d", &num);
 printf("Enter power of number : ");
 scanf("%d", &pow);
 res = power(num,pow);
 printf("%d's power %d = %d",num,pow,res);
 return 0;
}

int power(int n, int p)
{
 int r=1;
 for(; p>=1; p--)
    r = r*n;
 return r;

}


Find students grades through structure


Q. Write a C program using structure to find student grades in a class. Make necessary assumption.

Ans.

#include<stdio.h>
#include<conio.h>
struct stud
{
  char nam[20];
  int obtain_mark;
  int per;
  char grad[5];
};
struct stud s[5];
int i;
int main()
{
 for(i=1; i<=5; i++)
 {
  printf("Enter %d student name : ",i);
  scanf("%s",&s[i].nam);
  printf("Enter %d student obtained marks = ",i);
  scanf("%d",&s[i].obtain_mark);
  fflush(stdin); 
 }
 for(i=1; i<=5; i++)
   s[i].per=s[i].obtain_mark/5;
 for(i=1; i<=5; i++)
 {
  if(s[i].per>=80)
    strcpy(s[i].grad,"A");
  else if(s[i].per>=60)
    strcpy(s[i].grad,"B");
  else if(s[i].per>=50)
    strcpy(s[i].grad,"C");
  else if(s[i].per>=40)
    strcpy(s[i].grad,"D");
  else
    strcpy(s[i].grad,"F");
 }
 for(i=1; i<=5; i++)
  printf("\n%d student %s has obtained grade %s ",i,s[i].nam,s[i].grad);
 getch();
 return 0;
}

Output:
Enter 1 student name : John
Enter 1 student obtained marks : 250
Enter 2 student name : Robert
Enter 2 student obtained marks : 410
Enter 3 student name : Isabell
Enter 3 student obtained marks : 386
Enter 4 student name : Adem
Enter 4 student obtained marks :197
Enter 5 student name :Harry
Enter 5 student obtained marks : 490 

1 student Jhon has obtained grade C 
2 student Robert has obtained grade A
3 student Isabell has obtained grade B
4 student Adem has obtained grade F
5 student Harry has obtained grade A


Search Max & Min numbers


Ans:


#include<stdio.h>
#include<conio.h>
int main()
{
 int arr[10];
 int i,num,min,max;
 for(i=0; i<10; i++)
 {
   printf("Enter %d Number : ",i+1);
   scanf("%d",&arr[i]);
 }
 min=max=arr[0];
 for(i=0; i<10; i++)
 {
   if(arr[i] < min)
      min=arr[i];
   else if(arr[i] > max)
      max=arr[i];
 }
 printf("\nMaixmum number is %d",max);
 printf("\nMinimum number is %d",min);
 getch();
 return 0;
}


Output:
Enter 1 Number : 55
Enter 2 Number : 36
Enter 3 Number : 45
Enter 4 Number : 78
Enter 5 Number : 156
Enter 6 Number : 11
Enter 7 Number : 1256
Enter 8 Number : 2
Enter 9 Number : 596
Enter 10 Number : 367

Maximum number is : 1256                       

Minimum number is : 2   

Ads Inside Post