Wednesday, August 29, 2012

Difference between UDS and kwp2000


http://automotiveconceptsblog.wordpress.com/2012/06/06/kwp2000-and-uds-difference/

Tuesday, August 14, 2012

Wednesday, July 25, 2012

Little and BIG Endian




# include <stdio.h>
void main()
{
unsigned char test=0x01;
test=test<<1;
if(test)
{
     printf("This processor is little Endian");
}
else
{
    printf("This processor is Big Endian\n");
}
}
for more details visit
http://people.cs.umass.edu/~verts/cs32/endian.html

Sunday, May 13, 2012

About extern and static

About Extern and static .


Extern is the keyword give the linkage to another file to use the same variable .

Where as static is the keyword limit the linkage to internal . If we used the function name begin with static the scope of the variable is limited to internal.


#include <stdio.h>

int bar=34;

int main()

{

barTest();

printf("\n Bar in main---%d", bar);

return 0;

}// main file name is static1.c


#include <stdio.h>

extern int bar;

void barTest()

{

printf("\n Bar in barTest---%d", bar);

}// add.c subfile


output will be

Bar in barTest---34

Bar in main---34



Now change the program little bit


#include <stdio.h>

int bar=34;// changed

int main()

{

barTest();

printf("\n Bar in main---%d", bar);

return 0;

}// main file name is static1.c


#include <stdio.h>

static int bar=90;// changed hear

void barTest()

{

printf("\n Bar in barTest---%d", bar);

}// add.c subfile


output will be

Bar in barTest---90

Bar in main---34


int bar in add.c is limited to local to that file

now without both static as well as extern

At the time of linkage the this will produce error

Now come to function using static


#include <stdio.h>

int bar=34;

int main()

{

barTest();

printf("\n Bar in main---%d", bar);

return 0;

}// main file name is static1.c


#include <stdio.h>

extern int bar;

static void barTest()

{

printf("\n Bar in barTest---%d", bar);

}// add.c subfile

Now the void barTest is implicit to the file add.c if called from other file .

it will restrict access the compiler will give the error as “undefined reference to barTest”

Diiference between MACRO and CONST ?

Diiference between MACRO and CONST ?

MACRO
1.It is a replaces the test with a value
2.It value can't be altered but it is changed using pre-
processor derivative  #undef  .
3.once it is defined it is can't be localized like  normal value
(same name can't used inside like local)
4.It is preprocessor directive it process all the value before it compile

CONST
1.it stored in ROM. we can using to other files of C using extern
 that is not possible in MACRO.
2. It have address so we can use pointer as well as size of operator
3.It can be localized  in side a function
4. it is optimized by compiler

Wednesday, May 9, 2012

STRUCTURE POINTER

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

typedef struct tag{
    char name[20];
    char emailid[20];
    int age;

}ADDRESS_BOOK;

void show_name(ADDRESS_BOOK *emp);  /* function prototype */

int main(void)
{
     ADDRESS_BOOK ragu,*emp;
     emp=&ragu;
    sprintf(ragu.name,"praveen");
    sprintf(ragu.emailid,"veenrick@gmail.com");
    ragu.age=25;
     show_name(emp);          /* pass the pointer */
    return 0;
}

void show_name(ADDRESS_BOOK *p)
{
    printf("\n name %s ", p->name);  /* p points to a structure */
    printf("\n email %s ", p->emailid);
    printf("\n age %d", p->age);
}

Tuesday, May 8, 2012

WHAT IS THE USE OF typedef in C

what is the use of typedef in C?

struct _test{
unsigned int count;
unsigned char name[67];

}access_test;
Now you can access this structure using access_test.
If you want to create another copy of this structure then you
have to go to the structure decleared  place and add extra
accessing variable 
struct _test{
unsigned int count;
unsigned char name[67];

}access_test0,access_test1;

Now i want create a library file that is only usable .Not readable
(like some library file )
then how will you add one more copy of that structure in that place
typedef will give a easy solution for this

typedef struct _test{
unsigned int count;
unsigned char name[67];

}access_test;

Above will be hidden in library file, but  with the help of
access_test you can create your own copy of structure

access_test access_test1;


while using macro or typedef using capital(Upper case) letter is good practice

typedef struct _test{
unsigned int count;
unsigned char name[67];

}ACCESS_TEST;

ACCESS_TEST access_test1;


/*
 * File:   main.c
 * Author: praveen fedrick
 *
 * Created on May 8, 2012, 10:27 PM
 */

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



typedef struct _test{
unsigned int count;
unsigned char name[67];

}ACCESS_TEST;

ACCESS_TEST access_test1;



int main()
{
   
access_test1.count=10;
sprintf(access_test1.name,"praveen fedrick");
printf("Name %s and his count is %d",access_test1.name,access_test1.count);

}


Friday, May 4, 2012

structure padding and pointer inside structure

#include <stdio.h>

struct pint{
int *pintx;
char *pch;
long *plong;
}point;
struct pint1{
char pch;
int pintx;
long plong;

}point1;
void main()
{

    printf("\n---------Hello-------------\n");
    printf("\nsizeof structure point %dbytes--",sizeof(point));
    printf("\nsizeof point.pch %dbytes--",sizeof(point.pch));
    printf("\nsizeof point.pintx %dbytes--",sizeof(point.pintx));
    printf("\nsizeof point.plong %dbytes--",sizeof(point.plong));
    printf("\nsizeof structure point1 %dbytes--",sizeof(point1));
    printf("\nsizeof point1.pch %dbytes--",sizeof(point1.pch));
    printf("\nsizeof point1.pintx%dbytes--",sizeof(point1.pintx));
    printf("\nsizeof point1.plong%dbytes--",sizeof(point1.plong));

}


Here see the output
Structure point  having Different Data types but the size of the each
variable is same even they are different  data types, why because all
 the variabled are pointer variable .size is common to hold the address.

In Structure point 1  sum of size of the individual variable is 9.But in
actual it shows 12 how ?.  Because of structure padding .it occupy
three more bytes.



Wednesday, May 2, 2012

Function pointer Example 2

#include <stdio.h>
void (*print)(char *message) = NULL;

i_will_print(char *message){
printf("\n I will finish this job by doing following thing \n");
printf("---------%s-----------",message);
}
void main()
{
    unsigned char message[100];
    scanf("%s",&message);
    print=i_will_print(message);
   
}

Function pointer in C

#include<stdio.h>
int (*ptr)(int,int);//pointing to function with two integer
int fun(int a,int b)
{
int res;
res = a+b;
printf("Result after passing %d",res);
}

int main()
{
printf("The address of the function is %u\n",fun);
ptr = fun;// passing function to function pointer
(*ptr)(2,3);
}


Friday, April 27, 2012

use of volatile keyword and constant


Volatile is the used to tell the compiler to modify at any instance ;
constant is used to tell the compiler the value is not going to be changed
constant volatile both is used in same variable it indicate that the variable
is not modified through the program
' but it modified by external interrupts are inputs other than via program
this confirm that the program not modified using program.

Set and clear bit using MACRO


#include "<"stdio.h">"
#define CLRBIT(byte,_bit) byte&=~(1<<_bit)//clear bit in a byte _bit is the position want to clear
#define SETBIT(byte,_bit) byte|=(1<<_bit)
main()
{ unsigned char byte=0xff;
printf("\n The value before clear=0x%x",byte);
byte=CLRBIT(byte,1);
printf("\n The value after clear=0x%x",byte);
byte=SETBIT(byte,1);
printf("\n The value after set=0x%x",byte);
} //output
//The value before clear=0xff;
//The value after clear=0xfd;
//The value after set=0xff;

Bitfield inside union or Accessing bitfield inside union


// This is the program to access bits using union and structure #include "<"stdio.h">"
union _bitf
{
unsigned int _byte;
struct bitacc
{
unsigned char _bit0:1;
unsigned char _bit1:1;
unsigned char _bit2:1;
unsigned char _bit3:1;
unsigned char _bit4:1;
unsigned char _bit5:1;
unsigned char _bit6:1;
unsigned char _bit7:1;
unsigned char _bit8:1;
unsigned char _bit9:1;
unsigned char _bit10:1;
unsigned char _bit11:1;
unsigned char _bit12:1;
unsigned char _bit13:1;
unsigned char _bit14:1;
unsigned char _bit15:1;
} stracc;
} access;
int main()
{
printf("\n---0x%x",access._byte);
access.stracc._bit0=1;
access.stracc._bit3=0;
printf("\n---0x%x",access._byte);
access.stracc._bit4=0;
access.stracc._bit7=0;
printf("\n---0x%x",access._byte);
access.stracc._bit11=0;
access.stracc._bit15=1;
printf("\n---0x%x",access._byte);
if(access._byte==0x8001)
printf("\n It is possible");
}
/*This program is created to use bitfield and check the flag commonly */
using this you can reduce the variable no . and decrease the size of the variable.