Friday, 1 February 2013

Downloading MinGW and setting Environment variable (path)

Steps followed





  • After downloading, I installed the MinGW and selected only c and c++ option during my installation option, which downloaded some files from internet and showed the information in cmd prompt.
  • MinGW is successfully installed in "C:\MinGW".
    • Now the next step is to set the path variable in my Environment.

    • My Computer - Right click - Properties - Advanced system settings.

    • Environment variable - path 


    • Select path and press edit.

    • Add the MinGW bin path, so that the command line interpreter will know where to find them.
    • Add - ";C:\MinGW\bin" .
      Make sure you add ";" at the beginning of the path.

    Modulus operator using * and /

    //  Program to implement modulus operator using * and /
    #include <stdio.h>
    int my_mod( int a, int b){
        return ( a - (b*(a/b)));
    }

    int main(){
        int a = 11;
        int b = 2;
        printf("Remainder = %d\n", my_mod(a,b));
        getch();
        return 0;
    }

    Reverse the digits using C

    // Program to reverse the digits
    
    #include <stdio.h>
    #include <time.h>
    
    int reverseDigit(int num){
        int reverseNum = 0;
        while(num > 0){
            reverseNum = (reverseNum << 3)+(reverseNum << 1)+num%10;
            num = num/10;
        }
        return reverseNum;
    }
    
    int reverseDigitForLoop(int num){
        int res;
        for(res = 0;num>0; res=(res*10)+num%10, num/=10);
        return res;
    }
    
    int main(){
        int num = 3456;
        printf("Reverse of %d is %d\n", num, reverseDigit(num));
        getch();
        return 0;
    }
    
    // Note: (reverseNum << 3) + (reverseNum <<1) == reverseNum * 10; 

    STRCAT implementation in C

    Method 1:
    //Program to implement strcat
    #include <stdio.h>
    void my_strcat( char *dest, const char *src){ (*dest)? my_strcat(++dest, src):(*dest++ = *src++)?my_strcat(dest, src):0; } int main(){     char dest[30] = "Umesh ";     char *src = "Joga";     my_strcat(dest, src);     printf("Dest = %s\n", dest);     getchar();     return 0; }
    #if 0 Explanation: 1. my_strcat(dest, src) copies data of src to dest. 2. To do so, it first reaches end of the string dest using recursive calls my_strcat(++dest, src). 3. Once end of dest is reached, data is copied using (*dest++ = *src++)?  my_strcat(dest, src). #endif
    Method 2
    #include <stdio.h>
    char *my_strcat(char *dest, const char *src){     int i, j;     for(i = 0; dest[i] != '\0'; i++);     //printf("Val of i = %d\n", i);     for(j = 0; src[j] != '\0'; j++)         dest[i+j] = src[j];     //printf("Val of j = %d\n", j);     dest[i+j] = '\0';     return dest; } Method 3 //Using pointer char *mystrcat(char *dest, const char *src){ while(*dest) dest++; while(*dest++ = *src++); return dest; } int main(){     char *src = "Joga";     char dest[20] = "Umesh ";     printf("Dest = %s\n", my_strcat(dest, src));     getchar();     return 0; }

    SIZEOF implementation in C

    // Program to implement sizeof operator
    #include <stdio.h> #define my_sizeof(type) (char *)(&type + 1)-(char *)(&type)
    int main(){     char x;     printf("%d %d \n",(char *)(&x + 1), (char *)(&x));     printf("%d", my_sizeof(x));     getchar();     return 0; }

    STRREV implementation in C

    // Program to implement string reverse
    #include <stdio.h>
    void my_strrev( char *src ){     int i, srcLen = 0;     char temp;     srcLen = strlen(src) - 1;     for(i = 0; i < strlen(src)/2; i++){         temp = src[i];         src[i] = src[srcLen];         src[srcLen--] = temp;     } }
    int main(){     char srcString[30] = "Umesh Joga";     my_strrev(srcString);     printf("Reversed sting = %s\n", srcString);     getch();     return 0; }