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);}  
        }  
 }