×
 Introduction
Introduction to Newbie Programmer Series Introduction to Computer The Computer Programming Language Setting up the Computer for Programming
 Lets Get Started !
Beginning C Programming Variables a Handbag ! Basic Variable types in C Basic Formatted Output Basic Formatted Input
 Loops
Looping Repeating Concept Different Kinds of Loops Advanced concepts and trick with Loops
 Operators
Operators, An Introduction Arithmetic Operators Relational Operators Logical Operators Bitwise Operators Miscellaneous Operators and Operators Precedence
 Controlling the Flow
Flow Charts Conditional Statements Using Goto and Label Break and Continue the loops Switch the Cases Some Practical Applications of Control Flow
 The program structure
The program Structure Functions More Concepts of Functions Scope of the Variables across functions How the C language itself Works ? Scope of the Variables across files Static Variables Auto and Register Variables #define PreProcessor Some more PreProcessors
 Arrays and Structures
Introduction to Arrays Arrays, some more Concepts Arrays, Cool Examples Structures Introduction Structure, Some more Concepts Structure, Some Examples
 File Management
Basics of File management in C Steps Involved with File Handling in C
 Miscellaneous
Inside Logic Gates – The Electronic Logic Binary Number System Mathematical Reasoning
Newbie Programmer
full book coming soon
by Shubham Ramdeo

That's how we can create structures of different types of data ! Like real world objects !

 

structureWelcome to the Newbie Programmer Series. With the last part [click here], we have completed arrays. Here we will start with a new and interesting topic called Structures. Why there is a need to use them and how to use them. So if you are new to this series, please go to the index [click here] and read out all the previous parts so that you can easily understand what is happening down below.


We have discussed arrays. That is how same type of many variables get packed into a single big variable like a table. For example, for some employee salary program, when we say

float person[10];

We make an array, called 'person' that can store 10 'float' values.

We can do:

person[2]=4000.50;

This can be interpreted as the person number 3 has a salary of 4000.50$. (remember? arrays starts from zero?)

But that is not the complete solution. A 'person' is not limited by his 'number' and 'salary'. A person has his age, name, grade, family members and many other properties. These all are different kind of properties. And all needs to be managed individually. Also, all these different properties of a person are if different types.

A salary is a float type of variable, age and family members are integer, his name is a string and grade is a character.

To make such a package with arrays is not possible. That's why C language has a feature called 'structure'.

We want to make 'person' variable a complete structure of different kinds of properties and variables.

So to make it a structure, we will do:

struct {
 //properties
} struct_name;

So the above example of person can be created as :

struct {
 int age;
 int familymembers;
 float salary;
 char grade;
 char[50] name;
} person;

Now we have created person as a structure.

To use a structure, use dot ( . ) as :

struct_name.property = value

So in the above example, to set the age of this person :

person.age = 32;

It's simply saying "age of person is 32". This is so easy to understand.

To print its value, you can use same dot :

printf(" Age is %d \n", person.age);

You are not limited to a single structure. You can do something like this to create more :

struct {
 int pages;
 float cost;
} book1, book2, book3;

This will create 3 structure variables. book1, book2, book3. And each of these can be used individually having same kind of properties. Like you can do :

book1.pages = 100;
book2.pages = 256;
book3.cost = 7.99;

Let us make a complete program

#include <stdio.h>
main()
{
 struct {
 int pages;
 int SellingCost;
 }book1, book2;
 
 float costperpage1, costperpage2;
 
 printf("Enter number of pages of Book 1 : ");
 scanf("%d", &book1.pages);
 printf(" Enter selling cost of Book 1 : ");
 scanf("%f", &book1.SellingCost);
 printf("Enter number of pages of Book 2 : ");
 scanf("%d", &book2.pages);
 printf(" Enter selling cost of Book 2 : ");
 scanf("%f", &book2.SellingCost);
 
 costperpage1 = book1.SellingCost/book1.pages;
 costperpage2 = book2.SellingCost/book2.pages;
 
 if(costperpage1 == costperpage2)
 {
 printf("Both books have same cost per page \n");
 }
 else if(costperpage1 > costperpage2)
 {
 printf("Book 1 has more cost per page \n");
 }
 else if(costperpage2 > costperpage1)
 {
 printf("Book 2 has more cost per page \n");
 }
 else 
 {
 printf("There is some ERROR in DATA \n");
 }
 printf("Thanks for using...\n");
 printf("Press Enter to EXIT\n");
 getchar();
 return 0;
}

OUTPUT :

 Enter number of pages of Book 1 : 400
 Enter selling cost of Book 1 : 20
 Enter number of pages of Book 1 : 600
 Enter selling cost of Book 1 : 30
 Both books have same cost per page
 Thanks for using...
 Press Enter to EXIT

Structure as objects

Don't you think that the above examples of 'person', 'book' are all practically an object in real life ? They all are a type of their own and have their own properties ? I mean, we have integer type of variable in mathematics and programming but why can we a 'person' type of variable too ? Yes we can. We can create our own types of variable with C. We are not limited to 'int', 'float' etc only, we can create our own types. This is the basis of Object Oriented Programming. Well C language does not support so direct approach to object oriented programming but we can have a simple introduction with structs.

We can use a keyword 'typedef', to define a new type. We can use this keyword with any data types but we will do those advanced stuffs in the second book, here let us play with structs only.

typedef struct name {
 properties
 };

So now to use book as an individual object, a real life data type,

typedef struct book {
 int pages;
 float SellingCost;
 }

And then to make any variable a book, we can do it directly, simply :

book NewbieProgrammer;

Just as writing int x; Makes 'x' a integer variable, the above statement will make 'NewbieProgrammer' a book.

Also now you can access its properties directly :

NewbieProgrammer.pages = 250;

Using this style, we can make more programming more practical.

For example :

typedef struct person {
 char name[50];
 int age;
 float income;
 int child;
 };

person john, max;
john.income = 40000;
max.income = 2 * john.income;

It clearly tells us that "max's income is two times of john's income".

I think its enough for this part. We have discussed a brief introduction about structures of C. In the next part, we will do some more important concepts about structs so stay connected :)

 

Please share this as much as you can so that we can get connected with more and more people who want to learn programming.

© Shubham Ramdeo

Site Map
with
© Shubham Ramdeo