You are on page 1of 19

Computing Fundamentals

Dr. Muhammad Yousaf Hamza


Deputy Chief Engineer, PIEAS
STRUCTURES

Dr. Yousaf, PIEAS


//Structures //Collection of various types of variables
#include <stdio.h>
struct student //declares a structure
{
int rollnumber;
int semnumber;
float age_years;
}; // See the use of semicolon
int main()
{
struct student student1;/*declares a variable student1 of type structure*/
student1.rollnumber = 15;
student1.semnumber = 1;
student1.age_years = 18.5;
//Print the structure members
printf("Roll Nomber = %d\n",student1.rollnumber);
printf("Semesetr Number = %d\n",student1.semnumber);
printf("Age = %f\n",student1.age_years);
getchar(); return 0; } Dr. Yousaf, PIEAS
Structures
• They make your programming easier.
• You can extend the language - for example, if
you use complex numbers a lot then you can
write structures and functions which deal
with complex numbers.
• Structures are C’s way of grouping collections of
data into a single manageable unit.
– This is also the fundamental element of C upon which
most of C++ is built (i.e., classes).
– Similar to Java's classes.

Dr. Yousaf, PIEAS


Structures
• struct is used to describe a new data type.
• int, double and char are types of variables.
With struct you can create your own. It is a
new way to extend the C programming language.
• If programmers do a good job of this, the
end user doesn't even have to know what
is in the data type.

Dr. Yousaf, PIEAS


Structures
• An example:
– Defining a structure type:
struct student //declares a structure
{
int rollnumber;
int semnumber;
float age_years;
};
• This defines a new type struct student.

Dr. Yousaf, PIEAS


Structures
#include <stdio.h>
//group things together
struct database
{
int id_number;
int age;
float salary;
};

int main()
{
struct database employee;
employee.age = 22;
employee.id_number = 1;
employee.salary = 12000.21;
}
Dr. Yousaf, PIEAS
Structures
#include <stdio.h>
//group things together
struct database • Members of the structure
{
int id_number;
int age; • No space in memory
float salary;
};

int main()
{
struct database employee; • Accessing the members of
employee.age = 22;
employee.id_number = 1; structure using Dot operator
employee.salary = 12000.21;
}
Dr. Yousaf, PIEAS
// Program using Concept of
//Structures //Print the structure members
#include <stdio.h> printf("Roll Nomber =
%d\n",student1.rollnumber);
struct student //declares a structure
{ printf("Semesetr Number =
int rollnumber; %d\n",student1.semnumber);
int semnumber; printf("Age = %f\n",
float age_years;
}; // See the use of semicolon student1.age_years);
student2.rollnumber = 22;
int main() student2.semnumber = 1;
{
struct student student1; //Print the structure members
/*defines a variable student1 of printf("\n\nRoll Nomber =
type structure */
struct student student2; %d\n",student2.rollnumber);
printf("Semesetr Number =
student1.rollnumber = 15; %d\n",student2.semnumber);
student1.semnumber = 1;
student1.age_years = 18.5; getchar(); return 0;
}
Dr. Yousaf, PIEAS
/*More features of structures //Print the structure members
Initialization of member variables
Program using Concept of printf("Roll Nomber =
Structures*/ %d\n",student1.rollnumber);
#include <stdio.h> printf("Semesetr Number =
struct student //declares a structure %d\n",student1.semnumber);
{ printf("Age =
int rollnumber; %lf\n",student1.age_years);
int semnumber; student2 = student1;
float age_years;
}; // See the use of semicolon //Print the structure members
int main() printf("\n\nRoll Nomber =
{ %d\n",student2.rollnumber);
struct student student1 = printf("Semesetr Number =
{15,1,18.5}; /*defines a variable %d\n",student2.semnumber);
student1 of type structure */ printf("Age =
struct student student2; %f\n",student1.age_years);
getchar(); return 0;
}

Dr. Yousaf, PIEAS


/*More features of structures //Print the structure members
Initialization of member variables
Program using Concept of printf("Roll Nomber =
Structures*/ %d\n",student1.rollnumber);
printf("Semesetr Number =
#include <stdio.h> %d\n",student1.semnumber);
printf("Age =
struct student //declares a structure %f\n",student1.age_years);
{
int rollnumber; student2 = student1;
int semnumber; //Print the structure members
float age_years; printf("\n\nRoll Nomber =
} student1, student2; %d\n",student2.rollnumber);
printf("Semesetr Number =
int main() %d\n",student2.semnumber);
{ printf("Age =
student1.rollnumber = 15; %f\n",student1.age_years);
student1.semnumber = 1; getchar();
student1.age_years = 18.5; return 0;
}

Dr. Yousaf, PIEAS


Structures
• Define struct variables:
struct coord
{
int x;
int y ;
} first, second, third;
• Another Approach:
struct coord
{
int x
int y ;
};
...............
struct coord first, second, third; /* declare variables */
Dr. Yousaf, PIEAS
Structures
• Access structure variables by the dot (.) operator
• Generic form:
structure_var.member_name
• For example:
first.x = 50 ;
first.y = 23 ;

second.x = 9;
Second.y = 87;
• These member names are like the public data
members of a class in Java (or C++).
– No equivalent to function members/methods.
• struct_var.member_name can be used anywhere a
variable can be used:
– printf ("%d , %d", second.x , second.y );
– scanf("%d, %d", &first.x, &first.y);

Dr. Yousaf, PIEAS


Structures
• You can assign structures as a unit with =
first = second;
instead of writing:
first.x = second.x ;
first.y = second.y ;
• Although the saving here is not great
– It will reduce the likelihood of errors and
– Is more convenient with large structures
• This is different from Java where variables are
simply references to objects.
first = second;
makes first and second refer to the same object.

Dr. Yousaf, PIEAS


/* Addition of lengths using puts("Enter the value of feet
concept of structures*/ for first length");
#include <stdio.h> scanf("%d", &d1.feet);
struct AddLength
{ puts("Enter the value of inches
int feet; for first length");
float inches; scanf("%f", &d1.inches);
}; // See the use of semicolon
int main() puts("Enter the value of feet
{ for second length");
struct AddLength d1, d2, d3; scanf("%d", &d2.feet);
int carry;
puts("This program adds two puts("Enter the value of inches
values of lengths"); for second length");
puts("Enter the value of length in scanf("%f", &d2.inches);
feet and inches"); // continued to next slide
Dr. Yousaf, PIEAS
d3.inches = d1.inches + d2.inches;
carry = 0;
if (d3.inches >=12.0)
{
d3.inches -= 12.0;
carry++;
}
d3.feet = d1.feet + d2.feet + carry;
//Print the structure members
printf("First length = %d feet %f inches\n", d1.feet,d1.inches);
printf("Second length = %d feet %f inches\n", d2.feet,d2.inches);
printf(“Sum of lengths = %d feet %f inches\n", d3.feet,d3.inches);
getchar(); return 0;
}
Dr. Yousaf, PIEAS
C Program to add, subtract, multiply and divide
complex numbers: This program performs basic
operations on complex numbers i.e addition,
subtraction, multiplication and division. This is
a menu driven program in which user will have
to enter his/her choice to perform an
operation. User can perform operations until
desired. To easily handle a complex number a
structure named complex has been used, which
consists of two integers first integer is for real
part of a complex number and second for
imaginary part.

Dr. Yousaf, PIEAS


// C programming code for addition printf("Enter c and d where c + id
//of complex numbers is the second complex number.");
#include<stdio.h> printf("\nc = ");
#include<stdlib.h> scanf("%d", &c2.real);
struct complex printf("d = ");
{ scanf("%d", &c2.img);
int real; c3.real = c1.real + c2.real;
int img; c3.img = c1.img + c2.img;
}; if ( c3.img >= 0 )
int main() printf("Sum of two complex
{ numbers = %d +
struct complex c1, c2, c3; %di",c3.real,c3.img);
printf("Enter a and b where a + ib is else
the first complex number."); printf("Sum of two complex
printf("\na = "); numbers = %d
scanf("%d", &c1.real); %di",c3.real,c3.img);
printf("b = "); getchar(); return 0;
scanf("%d", &c1.img); }
Dr. Yousaf, PIEAS
Solve a Problem

To represent a complex number, write a


structure complexNum, decide the members
of the structure and their types, create two
variables of type complexNum and initialize
them from user input, check whether both
the number are equal or not?
Dr. Yousaf, PIEAS

You might also like