Friday, November 14, 2014

Union of C Progrmming



Program for C union

 #include
#include
#include
union student
{
            char name[20];
            char subject[20];
            float percentage;
};
int main()
{
    union student record1;
    union student record2;
       strcpy(record1.name, "Raju");
       strcpy(record1.subject, "Maths");
       record1.percentage = 86.50;
       printf("Union record1 values example\n");
       printf(" Name       : %s \n", record1.name);
       printf(" Subject    : %s \n", record1.subject);
       printf(" Percentage : %f \n\n", record1.percentage);
       printf("Union record2 values example\n");
       strcpy(record2.name, "Mani");
       printf(" Name       : %s \n", record2.name);
       strcpy(record2.subject, "Physics");
       printf(" Subject    : %s \n", record2.subject);
       record2.percentage = 99.50;
       printf(" Percentage : %f \n", record2.percentage);
       return 0;
}

Output

Union record1 values example
Name : Raju
Subject : Maths
Percentage : 86.500000;
Union record2 values example
Name : Mani
Subject : Physics
Percentage : 99.500000

Another way of declaring C union

#include 
#include
#include 
union student 
{
            char name[20];
            char subject[20];
            float percentage;
}record;
 
int main() 
{
 
            strcpy(record.name, "Raju");
            strcpy(record.subject, "Maths");
            record.percentage = 86.50;
            printf(" Name       : %s \n", record.name);
            printf(" Subject    : %s \n", record.subject);
            printf(" Percentage : %f \n", record.percentage);
            return 0;
}

Output

Name :
Subject :
Percentage : 86.500000

Another way of declaring C union

#include
#include
#include
union student
{
            char name[20];
            char subject[20];
            float percentage;
}record;
int main()
{

            strcpy(record.name, "Raju");
            strcpy(record.subject, "Maths");
            record.percentage = 86.50;
            printf(" Name       : %s \n", record.name);
            printf(" Subject    : %s \n", record.subject);
            printf(" Percentage : %f \n", record.percentage);
            return 0;
}

Output

Name : Raju
Subject : Maths
Percentage : 86.500000

Program for C typedef

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

Output

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

Program for C typedef

#include 
#include
#include 
int main()
{
   typedef long long int LLI;
   printf("Storage size for long long int data " \"type  : %ld \n", sizeof(LLI));
   return 0;
}

 Output

              Storage size for long int data type : 8

No comments:

Post a Comment