Posts

store a table in dynamic array and then change the size of array dynamically to store more numbers

  #include <stdio.h> #include <stdlib.h> int main() {     int *ptr;     ptr = ( int *)malloc( 10 * sizeof ( int ));         printf( "The multiplication table of 7 is:\n" );     for ( int i = 0 ; i < 10 ; i++)     {         ptr[i]= 7 *i;         printf( "7 X %d = %d\n" ,i+ 1 ,ptr[i]);     }    ptr = realloc(ptr, 15 * sizeof ( int ));    printf( "\nThe multiplication table of 7 till 15 is:\n" );     for ( int i = 0 ; i < 15 ; i++)     {         ptr[i]= 7 *i;         printf( "7 X %d = %d\n" ,i+ 1 ,ptr[i]);     }     free(ptr);     return 0 ; }

using realloc function in c to dynamically relocate the memory location

  #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 element %d:\n" , i + 1 );         scanf( "%d" , &ptr[i]);     }     //free(ptr);     for ( int i = 0 ; i < 6 ; i++)     {         printf( "The value of element %d is:%d\n" , i + 1 , ptr[i]);     }     ptr=realloc(ptr, 10 * sizeof ( int ));         for ( int i = 0 ; i < 10 ; i++)     {         printf( "Enter the value of element %d:\n" , i + 1 );         scanf( "%d" , &ptr[i]);     }     //free(ptr);     for ( int i = 0 ; i < 10 ; i++)     {         pri...

free dynamic memory allocation

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

dynamic memory using malloc

  #include <stdio.h> #include <stdlib.h> int main(){ //printf("The size of int in your computer is %d\n",sizeof(int)); // printf("The size of float in your computer is %d\n",sizeof(float)); // printf("The size of char in your computer is %d\n",sizeof(char)); int * ptr; int n; printf( "Enter the size of array:\n" ); scanf( "%d" ,&n); ptr=( int *) malloc(n* sizeof ( int )); for ( int i= 0 ;i<n;i++) {     printf( "Enter the value of element %d:\n" ,i+ 1 );     scanf( "%d" ,&ptr[i]); } for ( int i= 0 ;i<n;i++) {     printf( "The value of element %d is:%d\n" ,i+ 1 ,ptr[i]);     }   return 0 ; }

allocating dynamic memory using calloc

  #include <stdio.h> #include <stdlib.h> int main(){ //printf("The size of int in your computer is %d\n",sizeof(int)); // printf("The size of float in your computer is %d\n",sizeof(float)); // printf("The size of char in your computer is %d\n",sizeof(char)); int * ptr; int n; printf( "Enter the size of array:\n" ); scanf( "%d" ,&n); ptr=( int *) calloc(n, sizeof ( int )); for ( int i= 0 ;i<n;i++) {     printf( "Enter the value of element %d:\n" ,i+ 1 );     scanf( "%d" ,&ptr[i]); } for ( int i= 0 ;i<n;i++) {     printf( "The value of element %d is:%d\n" ,i+ 1 ,ptr[i]);     }   return 0 ; }

Entering values in an array using malloc function

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

snake game Version-2 (using random number)

  #include <stdio.h> #include <stdlib.h> #include <time.h> char comp_choice() { int i = 1 , num, gnum;     srand(time( 0 ));     num = rand() % 100 + 1 ;     if (num < 33 )     {         return 'w' ;     }     else if (num< 66 )     {         return 'g' ;     }     else if (num< 100 )     {         return 's' ;     }     } int snakegame( char you, char comp) {     if (comp == you)     {         return 0 ;     }     if (comp == 'w' && you == 's' )     {         return 1 ;     }     else if (comp == 's' && you == 'w' )     {         return - 1 ;     }     if (comp == 'w' && yo...

Snake, gun, water game in C(Version 1)

  #include <stdio.h> int snakegame( char you, char comp) {         if (comp == you)     {         return 0 ;     }     if (comp == 'w' && you == 's' )     {         return 1 ;     }     else if (comp == 's' && you == 'w' )     {         return - 1 ;     }     if (comp == 'w' && you == 'g' )     {         return - 1 ;     }     else if (comp == 'g' && you == 'w' )     {         return 1 ;     }     if (comp == 'g' && you == 's' )     {         return - 1 ;     }     else if (comp == 's' && you == 'g' )     {         return 1 ;     } } int main() { ...

updating number in a file through C program

  #include <stdio.h> int main(){ int num; FILE * ptr; ptr=fopen( "updatefile.txt" , "r" ); fscanf(ptr, "%d" ,&num); printf( "The value of num is %d" ,num); fclose(ptr); ptr=fopen( "updatefile.txt" , "w" ); fprintf(ptr, "%d" , 2 *num);   return 0 ; }

taking input from user and writing in a file in C

  #include <stdio.h> int main(){ FILE *ptr; float sal1,sal2; char name1[ 30 ],name2[ 30 ]; ptr=fopen( "employee.txt" , "w" ); printf( "Enter name of employe 1:\n" ); scanf( "%s" ,name1); printf( "Enter the salary of employee 1:\n" ); scanf( "%f" ,&sal1); printf( "Enter name of employee 2:\n" ); scanf( "%s" ,name2); printf( "Enter the salary of employee 2:\n" ); scanf( "%f" ,&sal2); printf( "%f %f %s %s" ,sal1,sal2,name1,name2); fprintf(ptr, "%s,%f\n" ,name1,sal1); fprintf(ptr, "%s,%f\n" ,name2,sal2); fclose(ptr);   return 0 ; }

reading a character from one file writing it twice in another file

  #include <stdio.h> int main(){ FILE *ptr1,*ptr2; ptr1=fopen( "file1.txt" , "r" ); ptr2=fopen( "file2.txt" , "w" ); char c; c=fgetc(ptr1); while (c!= EOF ) {     fputc(c,ptr2);     fputc(c,ptr2);     c=fgetc(ptr1);     } fclose(ptr1); fclose(ptr2);   return 0 ; }

writing a table in a file through c program

  #include <stdio.h> void table(FILE *ptr, int a) { for ( int i= 1 ;i<= 10 ;i++) { fprintf(ptr, "%d X %d = %d\n" ,a,i,(a*i)); } } int main(){ FILE * ptr; int a= 8 ; ptr=fopen( "tables.txt" , "w" ); fprintf(ptr, "The table of %d:\n" ,a); table(ptr,a); fclose(ptr);   return 0 ; }

reading 3 integers from a file in C

  #include <stdio.h> int main(){ FILE * ptr; ptr=fopen( "integer_file.txt" , "r" ); int num1,num2,num3; fscanf(ptr, "%d %d %d" ,&num1,&num2,&num3); printf( "The 3 integers are %d %d %d\n" ,num1,num2,num3); fclose(ptr);   return 0 ; }

reading whole file using file functions in C

writing in a text file using c program

  #include <stdio.h> int main(){ FILE * fptr; char c; fptr=fopen( "fputcsample.txt" , "w" ); fputc( 'A' ,fptr); fputc( 'N' ,fptr); fputc( 'C' ,fptr); fputc( 'H' ,fptr); fputc( 'A' ,fptr); fputc( 'L' ,fptr); fclose(fptr);   return 0 ;  

reading a character from file using fgetc

  #include <stdio.h> int main(){ FILE * fptr; char c; fptr=fopen( "fgetcsample.txt" , "r" ); printf( "%c\n" ,fgetc(fptr)); printf( "%c\n" ,fgetc(fptr)); printf( "%c\n" ,fgetc(fptr)); printf( "%c\n" ,fgetc(fptr));   return 0 ;   }

writing in a file in C

  #include <stdio.h> int main(){ FILE * fptr; int number= 89 ; fptr= fopen( "generated.txt" , "w" ); fprintf(fptr, "The number is %d" ,number); fclose(fptr);   return 0 ; }

opening and reading a file in c

  #include <stdio.h> int main() {     FILE *ptr;     int num1, num2;     ptr = fopen( "C:\\LearnC\\sample.txt" , "r" );     fscanf(ptr, "%d" , &num1);     fscanf(ptr, "%d" , &num2);     if (ptr == NULL )     {         printf( "The file does not exist" );     }     printf( "The value of num is %d\n" , num1);     printf( "The value of num is %d\n" , num2);     fclose(ptr);     return 0 ; }

comparing time using typedef,structure and functions

   #include <stdio.h> typedef struct time{ int hour; int minute; int second; }timestruct; void display(timestruct t1,timestruct t2) {     printf( "First time is:%d:%d:%d\n" ,t1.hour,t1.minute,t1.second);     printf( "Second time is:%d:%d:%d\n" ,t2.hour,t2.minute,t2.second); } int timecomp(timestruct time1, timestruct time2) {     if (time1.hour>time2.hour)     {         return 1 ;     }     if (time1.hour<time2.hour)     {         return - 1 ;     }     if (time1.minute>time2.minute)     {         return 1 ;     }     if (time1.minute<time2.minute)     {         return - 1 ;     }     if (time1.second>time2.second)     {         return 1 ;     }     if (time1.second...

comparing timestamp using typedef,structure and fuctions

  #include <stdio.h> typedef struct timestamp{ int day; int month; int year; int hour; int minute; int second; }tsstruct; void display(tsstruct d1,tsstruct d2) {     printf( "First timestamp is:%d-%d-%d %d:%d:%d\n" ,d1.day,d1.month,d1.year,d1.hour,d1.minute,d1.second);     printf( "Second timestamp is:%d-%d-%d %d:%d:%d\n" ,d2.day,d2.month,d2.year,d2.hour,d2.minute,d2.second); } int timestampcomp(tsstruct ts1, tsstruct ts2) {         if (ts1.year>ts2.year)     {         return 1 ;     }     if (ts1.year<ts2.year)     {         return - 1 ;     }         if (ts1.month>ts2.month)       {         return 1 ;     }     if (ts1.month<ts2.month)     {         return - 1 ;     }     if (ts1.day>ts2.day)...