Skip to main content

Posts

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 :-)

C program to find size of text file

/*  *Program to find size of text file  */ #include<stdio.h> #define LINE 80 int main(int c, char *argv[]) { /* fp is file pointer, once we open file, it is pointer to opened file */  FILE *fp;  char ch;  unsigned int size=0; /* Open file for reading   * argv[0] is executable program name and argv[1] is first   * argument given after executable file  */  fp=fopen(argv[1],"r");  /* if we get NULL as return value from fopen()   * it means, program is not able to open file or   * file may not existed ? or given file path will be wrong   */  if(fp == NULL){     printf("Can't open file\n");     return 0;  }  /*   * read characters from file and check size   */  while((ch = fgetc(fp)) != EOF){  /* we can add 1 to size every time but better to use sizeof to avoid issues */        size=size+sizeof(ch);  } printf("%u\n", size);  /*   * once we done with file we need to close it   */ fclose(fp); return 0; }

Program to Swap Adjecent nodes in single linked list

shiv@ubuntu:~/ds/list$ cat   altxchangenode.c /*  * program to Xchange or swap all the adjecent nodes(not node data) in the single linked list  */ #include<stdio.h> #include<stdlib.h> #define MAX 10 struct list {  int d;  struct list *next; }; /*  * function to swap 2 adjecent nodes(linkes) in the list  */ void xchangenodes(struct list **head, struct list **ne, struct list **tm) {   struct list *temp, *x1, *x2;   if(*tm == NULL){      return;   }   if((*tm)->next && (*tm)->next->next){       /*next node when more than 3 nodes present in the list */      temp=(*tm)->next->next;   } else if((*tm)->next){       if(*head != NULL) {   /* only two nodes present in the list */          (*ne)->next = (*tm)->next;          (*ne)->next->next = *tm;          (*tm)->next = NULL;       } else {           /* last iteration where last 2 nodes we need to swap */          *head=(*ne)->next;          (*head)->next =*tm;          (*tm)->next

C program to delete node from single linked list when just address of the previous node to be deleted is given

shiv@ubuntu:~/ds/list$ cat deln.c /*  * C Program to delete node in the single linked list, when address of the  *previous  node to be deleted is given  */ #include <stdio.h> #include <stdlib.h> struct list {     int d;     struct list *next; }; /*  * function to delete node when just address of the node to be  * deleted is given in the single linked list  */ void deletenode(void **ptr) {   struct list **temp, *del;   if(*ptr == NULL) {      printf("Invalid pointer\n");      return;   }   temp= (struct list **)ptr;   printf("The address of previous node of the node to be deleted is %p data %d\n", *temp, (*temp)->d);   if((*temp)->next && (*temp)->next->next){      /* any node apart from node before last or last node in the list */      (*temp)->d = (*temp)->next->d;      del = (*temp)->next;      (*temp)->next = (*temp)->next->next;      free(del);   } else if((*t

C Program to create sorted single linked list from given unsorted array

shiv@ubuntu:~/ds/list$ cat  tarr_to_sortlist.c  /*  * Program to create sorted list from given unsorted array  */ #include<stdio.h> #include<stdlib.h> #include<malloc.h> #define MAX 10 struct list { int d; struct list *next; }; int main() {  /* example array to test program */  int a[MAX]={4,1,5,6,10,9,3,2,7,8};  int  i;  struct list *head=NULL, *temp, *insert; /*loop to get all array elements*/ for(i=0;i<MAX;i++) { temp= malloc(sizeof(struct list)); if (temp == NULL) {         printf("Can't allocate memory\n");         exit(0); } temp->d = a[i]; temp->next = NULL;    /* first node in list */    if( head == NULL) {       head = temp;    } else {        / * second node in the list */        if( head->next == NULL){            if(head->d > temp->d){               temp->next = head;               head= temp;            } else {                   head->next = te

Simple program to create Zombie @ Linux

shiv@ubuntu:~/ds/unix$ cat zmb.c /*  * simple program to create a Zombie  * ps command display Zombie with Z under flags in ps output  */ #include<stdio.h> #include<unistd.h> #include<stdlib.h> int main() {  pid_t pid; /* create child process */  pid=fork();   if(pid == 0){    /* child process exits and becomes Zombie     */    exit(0);  } sleep(20); printf("Done ....\n"); return 0; }