C

Saturday, 8 July 2017

Program to show nesting of macros

#include<stdio.h>
#define SQU(x)((x)*x)
#define CUBE(x)(SQU(x)*x)
int main()
{         
          int x,y;
          clrscr();
          x=SQU(3);
          y=CUBE(3);
          printf("\nSquare of 3 : %d",x);
          printf("\nCube of 3 : %d",y);
          return(0);

}

Program to demonstrate pointer to function

#include<stdio.h>
main()
{
          void mess();
          void (*ptr)();
          ptr=mess;
          clrscr();
          printf("Address of the function is %d\n",ptr);
          (*ptr)();
}             
      void mess()
      {
          printf("I am in the subprogram\n");

      }

Program to demonstrate enumerated data types.

#include<stdio.h>
enum month {Jan,Feb,Mar,Apr,May,June,July,Aug,Sep,Oct,Nov,Dec,Next};
main()
{
          int m;
          float pay,sum=0;
          printf("\n");
          for(m=Jan;m<=Dec;m++)
          {
                   printf("Enter the pay of month %d :",m+1);
                   scanf("%f",&pay);
                   sum+=pay;
          }
          printf("Total Salary =%f",sum);

}

Program to demonstrate dynamic memory allocation

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
main()
{
          char *mem_allocation;
          clrscr();
          mem_allocation= malloc( 20* sizeof(char));
          if(mem_allocation==NULL)
          {
           printf("couldn't able to allocate requested memory\n");
          }
          else
          {
                   strcpy(mem_allocation,"Besties");
      }
      printf("Dynamically allocated memory                                  content:""%s",mem_allocation);
           free(mem_allocation);

}

Program to demonstrate use of #define

 #include<stdio.h>
#define NAME "vokytoky"
#define AGE 15

int main()
{       clrscr();
          printf("%s is over %d years old.\n",NAME,AGE);
          return 0;

}

Program to demonstrate conditional compilation.

#include<stdio.h>
int main()
{              
          int i=2;
          clrscr();
          #ifdef DEF
          {
                   i*=i;
          }
          #else
          {
                   printf("The answer is %d\n",i);
          }
          #endif
          return 0;
}

Program to demonstrate array of strings

#include<stdio.h>
#include<string.h>
void main()
{                  
          char name[35][21];
          int n,i;
          clrscr();
          printf("Enter numbers of students");
          scanf("%d",&n);
          printf("Enter the %d students name\n",n);
          for(i=0;i<=n;++i)
          {
                   gets(name[i]);
          }
     printf("\n List of students is");
          for(i=0;i<=n;++i)
          puts(name[i]);
}

Write a program to draw a circle using mid-point circle drawing algorithm.

#include<iostream.h> #include<graphics.h> #include<conio.h> void drawcircle(int x0, int y0, int r) {         ...