Posts

Showing posts with the label Using

Using malloc () and realloc ()

 #include<stdio.h> #include<stdlib.h> int main(){     int *ptr;     ptr = (int*) malloc(5*sizeof(int));     for(int i=0; i<5;i++){         printf("Enter the value of %d element: \n", i);         scanf("%d", &ptr[i]);     }     for(int i=0; i<5;i++){         printf("The value of %d element is: %d \n", i, ptr[i]);      }     ptr = realloc(ptr, 10*sizeof(int));     for(int i=0; i<10;i++){         printf("Enter the value of %d element: \n", i);         scanf("%d", &ptr[i]);     }     for(int i=0; i<10;i++){         printf("The value of %d element is: %d \n", i, ptr[i]);      }     return 0; }

Using realloc()

 #include<stdio.h>  #include<stdlib.h>  int main(){     int *ptr;      ptr = (int *) malloc(6 * sizeof(int));      for(int i=0; i<6;i++){         printf("Enter the value of %d element: \n", i);          scanf("%d", &ptr[i]);     }     for(int i=0; i<6;i++){         printf("The value of %d element is: %d\n", i, ptr[i]);      }     // Reallocate ptr using realloc()     ptr = realloc(ptr, 10*sizeof(int));          for(int i=0; i<10;i++){         printf("Enter the value of %d element: \n", i);          scanf("%d", &ptr[i]);     }     for(int i=0; i<10;i++){         printf("The value of %d element is: %d\n", i, ptr[i]);      }     return 0; }

Using free()

 #include<stdio.h>  #include<stdlib.h>  int main(){     int *ptr;     int *ptr2;     // Sizeof operator in C     // printf("The size of int on my pc is %d\n", sizeof(int));     // printf("The size of float on my pc is %d\n", sizeof(float));     // printf("The size of char on my pc is %d\n", sizeof(char));     ptr = (int *) malloc(600 * sizeof(int));      for(int i=0; i<600;i++){         ptr2 = (int *) malloc(600000 * sizeof(int));          printf("Enter the value of %d element: \n", i);          scanf("%d", &ptr[i]);         free(ptr2);     }     for(int i=0; i<6;i++){         printf("The value of %d element is: %d\n", i, ptr[i]);      }     return 0; }

Using calloc()

#include<stdio.h>  #include<stdlib.h>  int main(){     int *ptr;      int n;     printf("How many integers do you want to enter:\n");     scanf("%d", &n);     ptr = (int *) calloc(n, sizeof(int));      for(int i=0; i<n;i++){         printf("Enter the value of %d element: \n", i);          scanf("%d", &ptr[i]);     }     for(int i=0; i<n;i++){         printf("The value of %d element is: %d\n", i, ptr[i]);      }     return 0; }

File read using fgetc ()

 #include<stdio.h> int main(){     FILE *ptr;     char c;     ptr = fopen("getcdemo.txt", "r");     c = fgetc(ptr);     while(c!=EOF){         printf("%c", c);         c = fgetc(ptr);     }     return 0; }

Using fgetc() and fputc()

 #include<stdio.h> int main(){     FILE *ptr;     // fgetc demo for reading a file     // ptr = fopen("getcdemo.txt", "r");     // char c = fgetc(ptr);     // printf("The value of my character is %c\n", fgetc(ptr));     // printf("The value of my character is %c\n", fgetc(ptr));     // printf("The value of my character is %c\n", fgetc(ptr));     // printf("The value of my character is %c\n", fgetc(ptr));     // printf("The value of my character is %c\n", fgetc(ptr));     ptr = fopen("putcdemo.txt", "w");     putc('c', ptr);     putc('c', ptr);     putc('c', ptr);     fclose(ptr);     return 0; }

Using typedef

 #include<stdio.h> #include<string.h> typedef struct employee{     int code;     float salary;     char name[20]; } emp; void show(emp emp1){     printf("The Code of employee is: %d\n", emp1.code);     printf("The Salary of employee is: %f\n", emp1.salary);     printf("The Name of employee is: %s\n", emp1.name);      } int main(){     // Declaring e1 and ptr     emp e1;     emp *ptr;     // pointing ptr to e1     ptr = &e1;      // Set the member values for e1     ptr->code = 101;     ptr->salary = 11.01;     strcpy(ptr->name, "Harry");      show(e1);     return 0; }

Using Pointer to structure

 #include<stdio.h> #include<string.h> struct employee{     int code;     float salary;     char name[20]; }; int main(){     struct employee e1;     struct employee *ptr;     ptr = &e1;     //(*ptr).code = 101; //or you can also write: ptr->code = 101;     ptr->code = 101;     printf("%d", e1.code);      return 0; }

Using array + structure

 #include<stdio.h> #include<string.h> struct employee{     int code;     float salary;     char name[20]; }; int main(){     struct employee facebook[100];     facebook[0].code = 100;     facebook[0].salary = 100.45;     strcpy(facebook[0].name, "sunil");     facebook[1].code = 101;     facebook[1].salary = 90.45;     strcpy(facebook[1].name, "Bhaskar");     facebook[2].code = 102;     facebook[2].salary = 110.45;     strcpy(facebook[2].name, "SkillKhiladi");     printf("Done");     return 0; }

Using fflush

 #include<stdio.h> #include<string.h> int main(){     char st1[34];     char st2[34];     char c;     int i =0;     printf("Enter the value of first string\n");     scanf("%s", st1);      printf("Enter the value of second string character by character\n");          while(c!='\n'){          fflush(stdin);         scanf("%c", &c);          st2[i] = c;         i++;     }     st2[i-1]= '\0';     printf("The value of st1 is %s\n", st1);     printf("The value of st2 is %s\n", st2);     printf("strcmp for these strings returns %d", strcmp(st1, st2));     return 0; }

Using strcmp

 #include<stdio.h> #include<string.h> int main(){     char st1[45] = "Hel";     char *st2 = "Helo";     int val = strcmp(st1, st2);     printf("Now the val is %d", val);     return 0; }

Using strcat

#include<stdio.h> #include <string.h> int main(){     char st1[45] = "Hello";     char *st2 = "sunil";     strcat(st1, st2);     printf("Now the st1 is %s", st1);     return 0; }

Using strcpy

 #include<stdio.h> #include <string.h> int main(){     char *st = "This";     char st2[45];     strcpy(st2, st);     printf("Now the st2 is %s", st2);     return 0; }

Using strlen

 #include<stdio.h> #include <string.h> int main(){     char *st = "sunil";     int a = strlen(st);     printf("The length of string st is %d", a);     return 0; }

Using gets() and puts()

 #include<stdio.h> int main(){     char s[34];     printf("Enter your name: ");     gets(s);      puts(s);      // printf("Your name is %s", s);     return 0; }

Using continue function

 #include<stdio.h> int main(){     int skip=5, i=0;     while(i<10){         i++;         if(i!=skip){             continue;         }         else{             printf("%d\n", i);         }     }     return 0; }

Using break function

 #include<stdio.h> int main(){     int i = 0;     do{         printf("The value of i is %d\n", i);         if(i==4){             break;         }         i++;     }while(i<10);     return 0; }

Using do while loop

 #include<stdio.h> int main(){     int i = 220;     do{         printf("The value of i is %d\n", i);         i++;     }while(i <10);     return 0; }

Using while loops

 #include<stdio.h> int main(){     int a;     scanf("%d", &a);     while(a<10){     //     a = 11;     // while(a>10){ ---> These two lines will lead to an infinite loop         printf("%d\n", a);         a++;     }     return 0; }

Using switch statement

 #include<stdio.h> int main(){     int rating;     printf("Enter your rating (1-5)\n");     scanf("%d", &rating);     switch(rating){         case 1:             printf("Your rating is 1\n");             break;         case 2:             printf("Your rating is 2\n");             break;         case 3:             printf("Your rating is 3\n");             break;         case 4:             printf("Your rating is 4\n");             break;         case 5:             printf("Your rating is 5\n");             break;        ...