Friday, November 14, 2014

Stricture of Variable Function to C Programe



Program passing structure to function in C by value

#include 
#include
#include 
struct student 
{
            int id;
            char name[20];
            float percentage;
};
 
void func(struct student record);
 
int main() 
{
            struct student record;
 
            record.id=1;
            strcpy(record.name, "Raju");
            record.percentage = 86.5;
 
            func(record);
            return 0;
}
 
void func(struct student record)
{
            printf(" Id is: %d \n", record.id);
            printf(" Name is: %s \n", record.name);
            printf(" Percentage is: %f \n", record.percentage);
}

Output

Id is: 1
Name is: Raju
Percentage is: 86.500000

Passing structure to function in C by address

#include 
#include
#include 
struct student 
{
           int id;
           char name[20];
           float percentage;
};
void func(struct student *record);
int main() 
{
          struct student record;
          record.id=1;
          strcpy(record.name, "Raju");
          record.percentage = 86.5;
          func(&record);
          return 0;
}
void func(struct student *record)
{
          printf(" Id is: %d \n", record->id);
          printf(" Name is: %s \n", record->name);
          printf(" Percentage is: %f \n", record->percentage);
}

Output

Id is: 1
Name is: Raju
Percentage is: 86.500000

Structure variable as global in C Program

#include 
#include 
#include 
 
struct student 
{
            int id;
            char name[20];
            float percentage;
};
struct student record; 
void structure_demo();
int main() 
{
            record.id=1;
            strcpy(record.name, "Raju");
            record.percentage = 86.5;
            structure_demo();
            return 0;
}
void structure_demo()
{
            printf(" Id is: %d \n", record.id);
            printf(" Name is: %s \n", record.name);
            printf(" Percentage is: %f \n", record.percentage);
}

Output

Id is: 1
Name is: Raju
Percentage is: 86.500000


No comments:

Post a Comment