Friday 29 May 2009

function which is returning pointer


Return type of function can be pointer to char, pointer to int, pointer to float, pointer to double and pointer to void.



Examples:


 a.   Function returning pointer to character.

  b.   Function returning pointer to integer

  c.   Function returning pointer to float.

  d.  Function returning pointer to double.

  e.  Function returning pointer to void i.e. generic pointer. 











function which is returning void data type

void ass();void main(){    clrscr();    ass();    getch();}void ass(){    int i,j;    for(i=0;i<5;i++){         for(j=0;j

function which is returning double data type

#includedouble math();void main(){    float num;    num=math();    clrscr();    printf("%.lf",num);    getch();}double math(){    double d=2.1;    d=ceil(d);    return d;}Output: 3

function which is returning float data type

float find();void main(){    float num;    num=find();    clrscr();    printf("%f",num);    getch();}float find(){    float f=.005f;    f=(double)f;    return f;}Output: 0.005000

Function returning pointer to array in c programming.

char (*pa())[4];void main(){    char(*p)[4]=pa();    clrscr();    printf("%d",**p);    getch();}char (*pa())[4]{       static char arr[]={'\11','\12','\13','\14'};       return &arr;}Output: 9ONE MORE EXAMPLE:int* insertion_sort();void main(){ int *arr,i; arr=insertion_sort(); clrscr(); for(i=0;i<=7;i++) printf("%d ",arr[i]); getch();}int * insertion_sort(){static int

Function returning pointer to enum in c programming.

typedef enum color{a,b,c,d,e}co;enum color eee(){    static co x;    x=b+c/2;    return x;}void main(){    int num;    num=eee();    clrscr();    printf("%#d",num);    getch();}Output:2ONE MORE EXAMPLEenum color display();void main(){ int c; c=display(); printf("%d",c); getch();}typedef enum color{ black,blue,red,green }symbol; symbol display(){ symbol x=black+green;

Function returning pointer to union in c programming.

typedef union novel{    int count;    double volume;}*P,*Q,T;P iim(){    static T t={625};    Q q=&t;    return q;}void main(){    T *r;    r=iim();    clrscr();    printf("%-d",r->count);    getch();}Output:625typedef union employee{ char *name; int id;}EMP;EMP display(); void main(){ EMP list; list=display(); clrscr(); printf("%s ",list.name); getch();}EMP display(){

Function returning pointer to structure in c programming.

typedef struct film{    int size;    int pixel;    float price;}xyz,pqr;struct film *jadu(){    static xyz one={231,12,900.0},*p=&one;    return p;}void main(){    pqr *ptr;    ptr=jadu();    clrscr();    printf("%d",ptr->pixel);    getch();}Output:12ONE MORE EXAMPLE:typedef struct book{ char *name; int id; float price;}BOOK;BOOK show(); void main(){ BOOK list; list=show();

Function which is returning pointer to function in c programming.

#include double (*lic())(double);double(*p)(double);void main(){    p=lic();    clrscr();    printf("%.2lf",(*p)(M_PI_4));    getch();}double (*lic())(double){       p=&sin;       return p;}Output:0.71ONE MORE EXAMPLE:float calculate();float (*show())();void main(){ float b; clrscr(); b=(*show())(); printf("%f",b); getch();}float calculate(){ float a=5.5; a+=5<=5;

Function returning int data type in c programming.

void main(){ int max; max=max_value(); printf("Max of three: %d",max); getch(); } int max_value(){ int a=5,b=15,c=10; int max; max=(a>=b?a:b)>=c?(a>=b?a:b):c; return max; } Output: 15

Function which is returning char data type in c programming.

char find_character(); void main(){ char character; character=find_character(); printf("Character : %c",character); getch(); } char find_character(){ char * string="cquestionbank",temp; string+=3; temp=*string; return temp; } Output: e

Name of function cannot be register Pseudo variables




Register pseudo variables are reserved word of c language. So we cannot use these word as a name of function otherwise it will cause of compilation error.



#include

int main(){

   int c;

   c=_AL();

   printf("%d",c);

   return 0;

}





int _AL(){

   int i=5,j=5;

   int k=++j + ++j+ ++j;

   i=++i + ++i+ ++i;

   return k+i;;

}



Name of function is case sensitive in c programming




C programming language is case sensitive. Function is not exception of this. 



Example:



#include

int main(){

   CQUESTIONBANK();

   return 0;

}

int cquestionbank(){

   printf("BLOCK 1");

   return 0;

}

int CQUESTIONBANK(){

   printf("BLOCK 2");

   return 1;

}



Output: BLOCK 2




Definition of function in c
Name of

Name of function cannot be exactly same as of name of other function or identifier within the scope of the function.




(1) What will be output of following c program?

#include



int main(){

   int number,val;

   number=5<<2+5>>2;

   val=++number;

   val=number(val);

   printf("%d",val);

   return 0;

}

int number(int val){

   val=~val;

  return val;

}




Output: Compilation error


Explanation: number is function name as well as name of

Name of function cannot be global identifier: Example


  

In c programming language name of function cannot be global identifiers. We function name will global identifiers then it will cause of compilation error.



(1) Program to find area of any circle.



#include 

#include 

float __radius__(float);



int main(){

   float r,a;

   printf("Radius of circle ");

   scanf("%f", &r);

   a=__radius__(r

Name of function cannot be any keyword of c program




In c programming language a function name cannot be any keyword of c language. Function name can be keyword of c++ but it is bad practice. If function name is keyword of c language we will cause of compilation error.



Example:



#include



int main(){

   int num,count;

   printf("\nEnter a number:");

   scanf("%d",&num);

   count=interrupt(num);



First character of name of any function must be either alphabets or underscore in c




First letter or character of a function's name must be either an alphabet or underscore. If function name start from any digits or any other special character then we will get compilation error.



Example 1:



#include

int main(){

   int min=1,max=100;

   int sum;

   sum= _digit_(min,max);

   printf("sum = %d",sum);

   return 0;

}

int _digit_(int min,int max){

   int total;

Name of function includes only alphabets, digit and underscore in c




A function name in c can  have only alphabet,digits and underscore. If we will use any other characters in the function name then we will get a compilation error.

    

Example:



#include

int main(){

   int min=1,max=100;

   int sum;

   sum=sum ofdigit(min,max);

   printf("sum = %d",sum);

   return 0;

}

int sum of digit(int min,int max){

  int