11-Structs – Unions – Bit Manipulation and Counting Constants in C Programming
C Programming Lessons, Learn C Programming, Introduction to C Language, How to Program with C
Structures are collections of related variables under a name.
While arrays contain elements of the same data type, structures can contain elements of different data types. Structures are used to create records to be kept in the file (will be mensioned further subjects).
Structs are derived data types that are created using other types of objects.
struct card {
char * side;
char*team;
};
The “struct” keyword starts the structure definition. The card identifier is the structure tag.
The struct card definition contains two elements (side and team) of type char*.
struct card a = {“two”, “heart”}; // This declaration creates a predefined variable a of type struct card. It assigns the variable “two” to the side element of this variable and “heart” to the team element.
struct worker{
char name[20];
char surname[20];
int age;
char gender;
double hourlyWage;
struct worker person; // fault , struct worker contains an example of itself
struct worker *ePtr; //correct, ePtr is a pointer shows the struct worker type
};
Two operators are used to access the structural elements; struct operator (.) and struct pointer operator (->)
printf(“%s”, a.team);
printf(“%s”, aPtr->team);
Typedef
The typedef keyword provides a mechanism to create synonyms for previously defined data types.
typedef struct card Card;
Creates a new type called Card, which is synonymous with struct card.
*Creating a new name with a typedef does not create a new type, typedef creates new type names that can be used as synonyms for an already existing type name.
card deck[52];
Example Struct
struct student{
char name[20];
char surname[20];
char department[50];
int number;
int Class;
float average;
}stu1; //by adding stu2, stu3.. student number can be increased
int main()
{
strcpy(stu1.name,"Frodo");
strcpy(stu1.surname,"Baggins");
strcpy(stu1.department,"Management and Information Systems");
stu1.number = 123456;
stu1.Class = 7;
stu1.average = 3.55;
printf("Name : %s\n", stu1.name);
printf("Surname : %s\n", stu1.surname);
printf("Department : %s\n", stu1.department);
printf("Number : %d\n", stu1.number);
printf("Class : %d\n", stu1.Class);
printf("Average : %.2f\n", stu1.average);
}

Example Typedef
#include <stdio.h>
#include <string.h>
// struct with typedef person
typedef struct Person {
char name[50];
double perNo;
float salary;
} person;
int main()
{
// create Person variable
person p1;
// assign value to name of p1
strcpy(p1.name, "Frodo Baggins");
// assign values to other p1 variables
p1.perNo = 1234;
p1. salary = 3000;
// print struct variables
printf("Name : %s\n", p1.name);
printf("Peronal No. : %.f\n", p1.perNo);
printf("Salary : %.2f", p1.salary);
return 0;
}

Pointers to Struct in C
Let’s have a look at how to create pointers for structs.
struct Student {
student1;
student2;
};
int main()
{
struct Student *ptr, Frodo;
}
An Example for Pointer to Struct:
#include <stdio.h>
struct Student
{
int number;
float height;
};
int main()
{
struct Student *stdntPtr, student1;
stdntPtr = &student1; //address of student1 is stored in stdntPtr
printf("Enter student number: ");
scanf("%d", &stdntPtr->number);
printf("Enter height: ");
scanf("%f", &stdntPtr->height);
printf("Show values : \n");
printf("Number: %d\n", stdntPtr->number);
printf("height: %.2f", stdntPtr->height);
return 0;
}

Struct to Function in C
#include <stdio.h>
struct Person {
char name[50];
int per,num;
}P1; //can be defined also below (struct Person P1)
void display(int z, int x, int y);
int main()
{
//struct Person P1;
printf("Enter name: ");
gets(P1.name);
printf("Enter the personal number: ");
scanf("%d",&P1.num);
printf("Enter percentage: ");
scanf("%d", &P1.per);
display(P1.name,P1.num,P1.per);
return 0;
}
void display(int z, int x, int y )
{
printf("\nDisplaying information\n");
printf("Person Name: %s\n", z);
printf("Roll number: %d", x);
printf("\nPercentage: %d\n", y);
}

Returning Structure from a Function
#include<stdio.h>
struct salary{
char name[50];
int wage;
};
struct salary employee();
int main()
{
struct salary emp;
emp = employee();
printf("\nWage details of the employee\n");
printf("Name : %s",emp.name);
printf("\nWage : %d\n",emp.wage);
return 0;
}
struct salary employee()
{
struct salary emp1;
printf("Enter the name of the employee : ");
scanf("%s",emp1.name);
printf("\nEnter the wage : ");
scanf("%d",&emp1.wage);
return emp1;
}

Passing Struct by Reference
#include<stdio.h>
struct Employee
{
char name[20];
int age;
int year;
};
void print_struct(struct Employee *);
int main()
{
struct Employee p1;
printf("Enter the personal name : ");
scanf("%s",p1.name);
printf("\nEnter the personal's age : ");
scanf("%d",&p1.age);
printf("\nEnter the starting year : ");
scanf("%d",&p1.year);
print_struct(&p1);
return 0;
}
void print_struct(struct Employee *Ptr)
{
printf("\n---Details---\n");
printf("Name: %s\n", Ptr->name);
printf("Age: %d\n", Ptr->age);
printf("Year Started: %d\n", Ptr->year);
printf("\n");
}

Unions
Unions (like structs) are derived data types. A union is derived with the ‘union‘ keyword in the same way as structs.
While unions can only hold one member value at a time, structures allocate enough space to store all their members.
union number {
int x;
double y;
};
This code indicates that number is type of union and has int x and double y elements.
Union definitions precede main() in a program.
The size of a union variable will always be the size of its largest element.
#include <stdio.h>
union uPerson
{
//defining a union
char name[32];
float wage;
int persNo;
} uPerson;
struct sPerson
{
//defining a struct
char name[32];
float wage;
int persNo;
} sPerson;
int main()
{
printf("size of union = %d bytes", sizeof(uPerson));
printf("\nsize of structure = %d bytes\n", sizeof(sPerson));
return 0;
}

Counting Constants – Enum (Enumeration)
A count is introduced with the enum keyword and is a set of integer constants represented by identifiers.
These counting constants are symbolic constants whose values are determined automatically. Values in the enum start with 0 and are incremented by 1 unless otherwise stated.
enum months {JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC}
Creates a new type enum months. This counting makes the identifiers integers from 0 to 11.
#include <stdio.h>
enum months {JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC};
int main()
{
// creating this_month variable of enum months type
enum months this_month;
this_month = MAY;
printf("%d. Month\n",this_month+1);
return 0;
}

Bit Manipulation – Bitwise Operators
Bitwise operator works on bits and perform bit by bit operation.
Assume if B = 60; and B = 13; Now in binary format they will be as follows:
A = 0011 1100
B = 0000 1101
