Monday, February 21, 2011

How to Excute System Commands in C

 /* system example : DIR */  
 #include <stdio.h>  
 #include <stdlib.h>  
 int main ()  
 {  
  int i;  
  //printf ("Checking if processor is available...");  
  if (system(NULL)) puts ("Ok");  
  else exit (1);  
  printf ("Executing command dir...\n");  
  i=system ("dir"); // put ls if you are using linux  
  printf ("The value returned was: %d.\n",i);  
  return 0;  
 }  

Thursday, February 10, 2011

QUICK SORT IN C

STRUCTURE IN STRUCTURE EXAMPLE PROGRAM IN C

 #include<stdio.h>  
 struct date  
 {  
      int year;   
      int month;  
      int day;  
 };  
 struct student   
 {  
      char name[20];  
      struct date dob;   
 };  
 main()  
 {  
      struct student s1;   
      printf("Enter student Name\n");  
      scanf("%s",s1.name);  
      printf("Enter Date of Birth\n");  
      scanf("%d%d%d",&(s1.dob.year),&(s1.dob.month),&(s1.dob.day));  
      printf("%s\n%d-%d-%d\n",s1.name,s1.dob.day,s1.dob.month,s1.dob.year);       
 }  

POINTER STRUCTURES EXAMPLE PROGRAM IN C

 #include <stdio.h>  
 #include <stdlib.h>   
 #include <memory.h>  
 struct mystruct {  
      int a;  
 };  
 typedef struct mystruct * pmystruct;  
 pmystruct getpstruct()  
 {  
      pmystruct temp=(pmystruct)malloc(sizeof(pmystruct*));  
      return temp;  
 }  
 int main(int argc, char* argv[])  
 {    
      int * fred;  
      pmystruct b= getpstruct();  
      b->a = 8;  
      ++b->a;  
      printf ("The value of b->a is %i \n",b->a);  
      free(b);  
      fred = (int *) malloc(sizeof(int));  
      memset(fred,0x100,sizeof(int));  
      printf("Value of *fred = %i\n",*fred);  
      free(fred);  
      return 0;  
 }  

STRUCTURES EXAMPLE PROGRAM IN C

 /*  
 */  
 #include<stdio.h>  
 struct student  
 {  
      char name[20];  
      char id[10];  
      char class[5];   
      float marks;   
 };   
 main( )  
 {  
   struct student std[10];   
   int i;   
   for(i=0;i<10;i++)  
        {  
             scanf("%s%s%s",std[i].name,std[i].id,std[i].class);  
             scanf("%f",&std[i].marks);  
        }  
  printf("Name\tId\tclass\tmarks\n");  
   for(i=0;i<10;i++)  
        {  
             printf("%s\t%s\t%s",std[i].name,std[i].id,std[i].class);  
             printf("\t%0.2f\n",std[i].marks);  
        }  
   printf("Students Details of More than 5 Marks:\n");  
   printf("Name\tId\tclass\tmarks\n");  
   for(i=0;i<10;i++)  
        {  
             if(std[i].marks>=5){  
             printf("%s\t%s\t%s",std[i].name,std[i].id,std[i].class);  
             printf("\t%0.2f\n",std[i].marks);}  
        }  
 }  

Friday, January 28, 2011

Double Linked List Program in C

 /*  
      dlinkedlist.c: This programs is implementation of doubly linked list.   
      @uthor: itsafiz@gmail.com   
      Date: 11Oct2010.   
 */  
 #include<stdio.h>   
 typedef struct   
      {  
           int data;   
           struct dnode *next;   
           struct dnode *prev;   
      }dnode;  
 typedef dnode *list;   
 list head, tail;   
 main()  
 {  
      list temp,paste;   
      head=NULL;   
      int ch, n;   
      while(1)  
      {  
      printf("\t##Double Linked list ##\n\t1.Add element\n\t2.Display\n\t3.Delete\n\t4.Exit\n");  
      scanf("%d",&ch);  
      switch(ch)  
           {  
           case 1:   
           temp=(list)malloc(sizeof(dnode));  
           printf("Enter Data = ");  
           scanf("%d",&n);  
           temp->data=n;  
           if(head==NULL)  
           {  
           temp->prev=NULL;   
           head=temp;   
           }  
           else  
           {  
           temp->prev=tail;  
           tail->next=temp;  
           }  
           temp->next=NULL;  
           tail=temp;   
           break;   
           case 2:   
                paste=head;  
                for(;paste!=NULL;paste=paste->next)  
                     {  
                          printf("%d\t",paste->data);  
                     }  
                printf("\n");  
           break;   
           case 3: break;   
           case 4: exit(1);   
           }  
      }  
 }