Tuesday, January 13, 2015

Reading system value through system function

# include <stdio.h>
# include <stdlib.h>
# include <string.h>
# include <time.h>

int main()
{
        //Variable declaration
        FILE *fp;
        char hc1,hc2,mc1,mc2;
        int hi1,hi2,mi1,mi2,hour,minute;
        //Reading system value through system function 
        system("echo %time% >time.txt");
        fp=fopen("time.txt","r");
        // Sanity Check
        if(fp==NULL)
           exit(1) ;
        //Assigning individual character information into char varialbels
        hc1=fgetc(fp);
        hc2=fgetc(fp);
        fgetc(fp); // to avoid : symbol from integer calculation
        mc1=fgetc(fp);
        mc2=fgetc(fp);
        fclose(fp);
        remove("time.txt");
       
        hi1=hc1;
        hi2=hc2;
        mi1=mc1;
        mi2=mc2;
        // Char to Integer conversion
        hi1-=48;
        hi2-=48;
        mi1-=48;
        mi2-=48;
        // adding characters for double digit
        hour=hi1*10+hi2;
        minute=mi1*10+mi2;
        //Final output
        printf("Current time is %d:%d\n",hour,minute);


        return 0;
}

Output:
Current time is 11:40

Friday, January 9, 2015

Macros in conditonal operation and data mismatch issue

/*
 * Debugger.c
 *
 *  Created on: 09.01.2015
 *      Author: karthik
 */


# include <stdio.h>
# include <stdlib.h>
# define CEILby2(X) ((X+1)/2)

int main()
{
    int nCycle,nCount=0;
    printf("Understanding of Macros and TypeCasting\n");
    printf("Enter the Cycle in even number for a scenario\n");
    scanf("%d",&nCycle);

    while(nCount <CEILby2(nCycle))
    {
        nCount++;
    }
    printf("Number of cycle executed: %d", nCount);

    return 0;
}

**************************************
Output:
Understanding of Macros and TypeCasting
Enter the Cycle in even number for a scenario
5
 Number of cycle executed:3 ------ expected only 2.


Note: Integer is compared with floating(Real numbers)

Saturday, June 29, 2013

To Write and Read the 32 bit Variable using 4 Byte Array

/*
 * Read_Array.c
 *
 *  Created on: Jun 29, 2013
 *      Author: Karthik
 */


/* To Write the 32 bit variable using 4 Byte Array*/

#include "stdio.h"
#include "stdlib.h"

int main()
{
    /* variable Initialization */

    int nArray[4] = {1,0,0,0};
    //int nOutVar = 0;
    unsigned int nlOutVar=0;

    printf("%d\n",nArray[0]);
    printf("%d\n",nArray[1]);
    printf("%d\n",nArray[2]);
    printf("%d\n",nArray[3]);
    nlOutVar = nArray[0];
    nlOutVar = nlOutVar << 8;
    printf("2byte usage%u\n",nlOutVar);
    nlOutVar = nlOutVar + nArray[1];
    nlOutVar = nlOutVar << 8;
    printf("3byte usage%u\n",nlOutVar);
    nlOutVar = nlOutVar + nArray[2];
    nlOutVar = nlOutVar << 8;
    printf("4byte usage%u\n",nlOutVar);
    nlOutVar = nlOutVar + nArray[3];

    printf("4byte content looks like %u\n",nlOutVar);

    nlOutVar = nlOutVar >> 16;
    printf("MSB first 2byte content reading %u\n",nlOutVar);

    nlOutVar = 4294967295;
    printf("maximum value %u\n",nlOutVar);

    return 0;

}

Output:

1
0
0
0
2byte usage256
3byte usage65536
4byte usage16777216
4byte content looks like 16777216
MSB first 2byte content reading 256
maximum value 4294967295

Thursday, April 11, 2013

Sine Wave Generation in C Program

/*
 * SineWave.c
 *
 *  Created on: Apr 11, 2013
 *      Author: Karthik
 */

#include
<stdio.h>
#include<conio.h>
#include<math.h>
#include <windows.h>


#define pi 3.142
int main()
{
    FILE *fp;
    fp = fopen("Sinewave.xls","w");

    if(fp == NULL)
    {
        MessageBox(0,"Error","File Creation",16);
        exit(0);

    }
 int nfreq,nTime,nAmplitude=0;
 double dSample, dCycle=0;
 double dStopTime=0;
 printf("enter the desired frequency of the signal:\n");
 scanf("%d",&nfreq);
 printf("enter the desired sampling Time:\n");
 scanf("%d",&nTime);
 printf("enter the amplitude of the signal:\n");
 scanf("%d",&nAmplitude);
 printf("Enter the Stop time:\n");
 scanf("%lf",&dStopTime);

for (;dCycle <= dStopTime; )
{
    dSample = nAmplitude * sin(2*pi*nfreq*dCycle);

    fprintf(fp,"%lf\n",dSample);

    dCycle = dCycle + ((double)nTime/100);
}

    fclose(fp);
    getch();
    return 0;
 }

Saturday, December 15, 2012

Example for Dot and arrow usage of structural pointer and array.

# include <stdio.h>
# include <stdlib.h>
# include <string.h>


typedef struct database
{
    int age;
    char name[10];

}DATA;

DATA Str;
DATA *Ptr;




int main()
 {


    /*********** Dot format to Arrow format representation********/

     //Enter the values for database
    printf("1. Enter the values for database\n");
    printf("Enter your Age\n");
    scanf("%d",&(Str.age));
    scanf("%s",Str.name);

    printf("Your Age:%d is updated in database\n",(&Str)->age);
    printf("Your Name:%s is updated in database\n",(&Str)->name);
    /**************************************************************/



    Ptr =(DATA *)malloc(sizeof(DATA));
    /*********** Arrow format to Dot format representation *********/

        printf("2. Enter the values for database\n");
        printf("Enter your Age\n");
        scanf("%d",&(Ptr)->age);

        scanf("%s",Ptr->name);

        printf("Your Age:%d is updated in database\n",((*Ptr).age));
        printf("Your Name:%s is updated in database\n",(*Ptr).name);
    /**************************************************************/

     return 0;
 }


Output:


F:\workspace\pointer_C\Debug>pointer_C.c.exe
1. Enter the values for database
Enter your Age
24
kumar
Your Age:24 is updated in database
Your Name:kumar is updated in database
2. Enter the values for database
Enter your Age
23
Ramesh
Your Age:23 is updated in database
Your Name:Ramesh is updated in database

F:\workspace\pointer_C\Debug>

Difference between Dot(.) and Arrow (*) in C language


Difference between Dot(.) and Arrow (*) in C language
 






 
Representation 
Dot (.)
Arrow (*)
The Dot (.) operator can't be overloaded.
 Arrow (->) operator can be overloaded.
ex:(*foo).bar()
  ex:foo->bar()
 






 
Note1: The parenthesizes above are necessary because of the binding strength of the * and . operators.
Note2: *foo.bar() wouldn't work because Dot (.) operator binds stronger and is executed first. 
 






 
Possible way of dot and arrow usage for non pointer  element in C.
 
 






 
ptr->fld == (*ptr).fld





 
str.fld == (&str)->fld              

Friday, December 14, 2012

How to read element in Structural Array and pointer in C

/*
 * pointer.c
 *
 *  Created on: Dec 15, 2012
 *      Author: Karthik
 */

# include <stdio.h>
# include <stdlib.h>

struct database
{
    int nstArray[10];
    int *nstpointer;
}STDBase;


int main()
{
    // Initialization
    int nArray[10];
    char cArray[10];
    int *nPointer;
    int nLoop;

    //Memory allocation
    nPointer = calloc(1,10);

    // Initialization for int, char arrays and int pointer
    for(nLoop = 0; nLoop < 10; nLoop++)
    {
        // To int array
        nArray[nLoop] = nLoop;
        printf("%d\t",nArray[nLoop]);

        // To char array
        cArray[nLoop] = nLoop+48;
        printf("%c\t",cArray[nLoop]);

        // To int pointer
        *nPointer    = nLoop+2;
        printf("%d\n",*nPointer);
        nPointer++;

    }
   nLoop--;
    STDBase.nstArray[nLoop] =10;
    STDBase.nstpointer = (STDBase.nstArray);

    printf("%d\n",STDBase.nstArray[nLoop]);
    printf("%d\n",STDBase.nstpointer[nLoop]);


    return 0;
}


Output:
 0    0    2
1    1    3
2    2    4
3    3    5
4    4    6
5    5    7
6    6    8
7    7    9
8    8    10
9    9    11
10
10





Saturday, October 13, 2012

Difference Between Build and Compilation in C/C++


Build is the process to compile and link only the source files that have changed since the last build (i.e. based on file creation date and time).

Rebuild is the process to compile and link all source files regardless of whether they changed or not. Build is the normal thing to do and is faster. Sometimes the versions of project target components can get out of sync and rebuild is necessary to make the build successful.

Compilation is compiles the source file currently being edited.

The compilation of code involves several steps:

  • Parsing of the statements for syntax (language based)
  • Translation of the statements into machine language
  • Setting up the addresses of all variables
  • Optimization of the code (if desired)

Thursday, October 4, 2012

Bitwise Operators and Byte to Bit Conversion

# include "stdio.h"
# include "string.h"

const char* byte_to_binary( int x );
int main()
{

    int nInput=0, nOutput=2, nBitPos;

    // To set the bit in particular position.
    nInput |= (1 << nOutput);
    printf("To set the %d Position from 1bit of %s\n",nOutput, byte_to_binary(nInput));

    // To Reset/Clear the bit in particular position.
    nInput &= ~(1 << nOutput);
    printf("To Reset/Clear the %d Position from 1bit of %s\n",nOutput, byte_to_binary(nInput));

    // To toggle the bit in particular position.
    nInput =5; // 00000101 expected like 00000001
    nInput ^= (1 << nOutput);
    printf("To toggle the %d Position from 1bit of %s\n",nOutput, byte_to_binary(nInput));

    // To state of the bit in particular position.
    nInput =5; // 00000101 expected like 00000101
    nBitPos = nInput &(1 << nOutput);
    printf("To state of the %d Position from 1bit of %s:: state of bit: %d\n",nOutput, byte_to_binary(nInput), nBitPos);


return 0;
}



//To Byte to Binary Conversion
const char* byte_to_binary( int x )
{
    static char b[9] = {0};
    int z, y;

    for (z=128,y=0; z>0; z>>=1,y++)
    {
        b[y] = ( ((x & z) == z) ? '1' : '0');
    }

    b[y] = 0;

    return b;
}



Output:
To set the 2 Position from 1bit of 00000100
To Reset/Clear the 2 Position from 1bit of 00000000
To toggle the 2 Position from 1bit of 00000001
To state of the 2 Position from 1bit of 00000101:: state of bit: 4