Skip to main content

Posts

Showing posts from October, 2011

C Program to Simulate Linux Cat command

/*  * C program to Simulate Implementation of  Linux *cat* command  * This program will not handle redirection  * you can try to improve this program to handle redirection of file contents  */ #include<stdio.h> #include<string.h> #define MAX_FILE_NAME_CHARS 255 int main(int argc, char *argv[]) {  FILE *fp;  char file_name[MAX_FILE_NAME_CHARS], ch;  int i;  /*   * after creating a.out, rename it as mycat for our own cat command   * and it usage is same as standard cat command   */  if(argc<1){     printf("Usage mycat <filename> \n");     return 0;  }  /*   * cat handles more than one file   * so we need this loop to handle all the files provided   * on command line   */  for(i=1; i<=argc;i++){     /* no need of copy but for understanding purpose, i have created      * string for each file name      */     strncpy(file_name, argv[i], MAX_FILE_NAME_CHARS);     fp=fopen(file_name, "r");     if(fp == NULL) {   

Simple program to illustrate variadic macros In C

/*  * C99 has  support of variable arguments in macros statement  * (called variadic macro) just like variable argument function, Macros also   * can be of variable number of augments  * This program illustrate variadic macros  */ #include <stdio.h>   /*   * Simple Macro definition   * which allows to print whatever the way you want   *  in Macro definition "..." indicates variadic macro, OP() definition is    * mixture of named args and variable number of aguments, it is possible   * to have macro of complete variable number of args   * like DISPLAY in below example   */ #define OP(format, exp, ...)  printf(format, exp, ##__VA_ARGS__) #define DISPLAY(...)    printf(__VA_ARGS__) int main() {  int a=3, b=4, c=5;  OP("%s %d\n", "Result after add", a+b);  OP("%s %d %s %d\n", "Result after add", a+b+c, "Result after sub", a-b);  OP("%s\n", "This is test program");  OP("%s %f\n", "

C Program to find Armstrong numbers between range m and n

/*  * Program to print Armstrong number between range m and n  * Armstrong  number is nothing but sum of cubes of digits of number is  * equal to the number  * ex: 153 is Armstrong number since 1^3+5^3+3^3=153  * there are very few numbers hold above property; execute   * below program to find all numbers  * b/w m and n  */ #include <stdio.h> void is_armstrong(int num) {   int sum=0, n, d;   n=num;   while(n){   d=n%10;  /* extract digits of number*/   sum=sum+(d*d*d);  /*take sum of them */   n=n/10;  }  if(sum == num)  /*sum is same as number then it is Armstrong number*/     printf("%d\n", sum); } int main() { int i,n,m; printf("Enter range \n"); scanf("%d%d", &m, &n);  for(i=m;i<=n;i++){    is_armstrong(i);  } return 0; }

Interactive C program to Swap bits in 32 bit integer

/*  * Program to swap bit in given 32 bit Integer number  */ #include<stdio.h> int main() {  int a;  int b1, b2;  printf("Enter numbers \n");  scanf("%d", &a);  printf("Enter bit numbers to be swapped\n");  scanf("%d%d", &b1, &b2);  if(b1 >=31 || b2>=31){     printf("Invalid bit positions(valid are 0 - 31)\n");     return 0;  }    if((a&(1<<b1)) && !(a&(1<<b2))){     /* bit number b1 is set but b2 is not      * to swap just unset b1 and set b2      */       a=a & ~(1<<b1);       a=a | (1 << b2);  } else if(!(a&(1<<b1)) && (a&(1<<b2))){        /* bit b1 is not set(zero) and bit b2 is set         * so to swap set b1 and clear b2         */       a=a | (1<<b1);       a=a & ~(1<<b2);  } else {      /* given both bits are set/clear in number so nothing to be done*/  } printf("Number after bit swap is %d\n", a); return 0;

Find size of structure variable(any variable) without using sizeof operator in C

This is one of the interview question, beginners could note, below is one of the way how we can find size of structure without using sizeof operator in C It is explained with simple program as follows #include<stdio.h> struct test {  int a;  char c;  double d; }; int main() { struct test *p; int size=0; /*  * initialize struct pointer with  * zero after type casting it to of type (struct test *)  */ p=(struct test *)0; /* by adding one to p gives size of struct */ size =(int)(p+1); printf("without using sizeof %d\n", size); /* verify result with sizeof operator */ printf("with using sizeof %d\n", sizeof(struct test)); return 0; } same way we can find size of other variables as well :-)