18 October 2014

Structures and Unions in C Program


  1. Structure in C groups different data type under a common name
  2. struct is a keyword to declare a structure
  3. The size of the structure depends on the size of the different data types
  4. Structure declaration always ends with a semicolon
Example:
struct employee
{
char name[50];
int empid;
float salary;
};
The total size occupied by the above structure is 50 + 2 + 4 = 56 bytes (on TC compiler) and 50 + 4 + 4= 58 (on other compilers like GCC, etc). Again the size depends on the compiler.
GCC compiler always increase the size of structure which is divisible by 4 for faster code generation, so the above structure takes 60 bytes of memory
  1. Declaring a structure does not consume any memory, only when the structure object is created memory occupies
  2. Variables initialization will not be done during the structure declaration
The above syntax declaration does not consume memory, but the following do
int main()
{
struct employee e1;
}
Now e1 consumes memory of 60 bytes.
Simple Structure to get some details and print them to the screen
// GCC Compiler under windows 64bit
#include <stdio.h>
#include <conio.h>
/*  structures are declared outside any functions,
so that all the functions in the program can have access to it
*/
struct employee
{
       char name[50];
       int empid;
       float salary;
};
int main()
{
    struct employee e1;
    printf("enter the name, id and salary");
    scanf("%s %d %f",e1.name, &e1.empid, &e1.salary); 
    printf("The details entered are \n");
    printf("%s %d %f",e1.name, e1.empid, e1.salary);
    getch();
    return 0;
}
In the above program name, empid and salary are part of employee, so it should be associated with the member of structure, that’s why e1.name, e1.empid and e1.salary.

Array of Structures
//the following program gets the inputs of 3 employees like name, id and salary and compute the total salary taken by all the employees
#include <stdio.h>
#include <conio.h>
/*  structures are declared outside any functions,
so that all the functions in the program can have access to it
*/
struct employee
{
       char name[50];
       int empid;
       float salary;
};
int main()
{
    struct employee e[3];
    int i;
    float sum=0;
    printf("enter the name, id and salary");
    for(i=0;i<3;i++)
    {
    scanf("%s %d %f",e[i].name, &e[i].empid, &e[i].salary);
    sum=sum+e[i].salary;
    }
   
   
    printf("The details entered are \n");
    for(i=0;i<3;i++)
    printf("%s %d %f",e[i].name, e[i].empid, e[i].salary);
    printf("Total Salary of all 3 employees is %f",sum);
    getch();
    return 0;
}
Sample Output
Clipboard02

Arrays within Structures
//The following program gets the 2 student inputs like 3 subject marks and compute the total of all three subject marks
#include <stdio.h>
#include <conio.h>
/*  structures are declared outside any functions,
so that all the functions in the program can have access to it
*/
struct student
{
       int subjects[3];
       float total;
};
int main()
{
    struct student s[2];
    int i,j;
    printf("enter the 3 subject marks");
    for(i=0;i<2;i++)
    {
    s[i].total=0;
    for(j=0;j<3;j++)
    {
    scanf("%d",&s[i].subjects[j]);
    s[i].total += s[i].subjects[j];
    }
    printf("The total marks scored by student%d is %f",i+1,s[i].total);
    }
   
    getch();
    return 0;
}
Sample Output
clip2
 T S Pradeep Kumar