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);
}