C Programming Lesson - Structures in C
By rajkishor09
C Structure Introduction
We used variable in our C program to store value but one variable can store only single piece information (an integer can hold only one integer value) and to store similar type of values we had to declare many variables. To overcome this problem we used array which can hold numbers of similar data type. But array too have some limitations, like in our real world application we deal with set of dissimilar data types and single array cannot store dissimilar data.
For example think about storing book information or product information, a product can have different information to store like product code (an integer), product name (a char array), product price (a float) etc. And to store 20 products information we can declare integer array for product code, 2D character array for storing product name and float array to store product price. This approach definitely achieves your goals, but try to consider these things too. What if you wanted to add more products than 20, what if you want to add more information on products like stock, discount, tax etc? It will become difficult to differentiate these variables with other variables declared for calculation etc.
To solve this problem C language has a unique data type called Structure. C structure is nothing but collection of different related data types. If we are using C structure then we are combining different related data types in one group so that we can use and manage those variables easily. Here related data type means, a structure holding information about book will contains variable and array related to book.
Syntax for C Structure declaration
struct structure_name
{
data type member1;
data type member2;
…
…
};
Example:
struct products
{
char name[20];
int stock;
float price;
};
So structure declaration begins with struct keyword and with a space we need to provide a structure name. Within open and closed curly braces we can declare required and related variable, you can see it in our example structure declaration. And most important point to remember in case of C structure is that it ends with semicolon (;).
Let’s have a complete example of structure in C language.
Example of C structure
#include<stdio.h>
#include<conio.h>
struct product
{
char name[30];
int stock;
float price, dis;
};
void main()
{
struct product p1 ={"Apple iPod Touch 32GB", 35,298.56, 2.32};
clrscr();
printf("Name=%s,\nStock=%d,\nPrice=$%.2f,\nDiscount=%.2f%.", p1.name, p1.stock, p1.price,p1.dis);
getch();
}
Related Books
![]() | Amazon Price: $35.64 List Price: $67.00 |
![]() | Amazon Price: $19.98 List Price: $44.99 |
![]() | Amazon Price: $59.31 |
![]() | Amazon Price: $10.00 List Price: $44.95 |
Code Explanation
So line no.4-9 declares a C structure named “product”, this structure contains four variables to store different information about product. In the beginning there is a character array (char name[30]) which stores name of the product, next we have integer variable (int stock) to store stock of product and last two variable are float type (float price, discount) to product price & discount on product respectively.
Guys we just declared product structure and now we have to use it in main(). Line no. 14 declares a product type variable p1. Here product type variable means, in our C program product is a structure and to use that structure we need to create its variable. Declaring a product structure variable is simple just use following syntax:
struct structure_name variable_name;
Remember struct is a C keyword, “structure_name” is name of structure you used while declaring a C structure (in above C program its product) and “variable_name” could be any name of your choice (in above C program its p1) but standard naming convention applies.
Along with declaring C structure variable p1 we have also initialized it and to initialize C structure you need to assign values in proper order. Proper order means assign value in the order they are declared in structure. For example, in our product structure we declare variable in following orders:
char name[30];
int stock;
float price, discount;
So for this structure proper order will be:
char name[30];
int stock;
float price;
float discount;
You don’t need to rewrite your structure, you just need to keep it in mind that structure variable initialization should be performed in orderly manner (top – bottom and left – right manner) otherwise it will show error or you may get strange output.
So in above program we have initialized p1 variable in following way:
struct product p1 ={"Apple iPod Touch 32GB", 35, 298.56, 2.32}; means
struct product p1 ={char name[30], int stock, float price, float discount}; //this line of code is only assumption.
Next in line no. 16 we just printed the values stores in product structure. You cannot print values stored in product structure member variable (like name, stock etc.) directly, you have to link member variable with structure variable and you can do this using (.) dot operator. For example: character array name is unknown to main() because it is declared in structure product, so to access member variable we will follow the following syntax:
structure_variable.member_variable;
Example:
p1.name;
We can rewrite printf("Name=%s,\nStock=%d,\nPrice=$%.2f,\nDiscount=%.2f%.", p1.name, p1.stock, p1.price,p1.discount); in the following manner:
printf(“Name = %s”,p1.name);
printf(“Stock = %d”,p1.stock);
printf(“Price = $%.2f”,p1.price);
printf(“Stock = %.2f”,p1.discount);
Here is full working code sample of C Structure.
#include<stdio.h>
#include<conio.h>
struct product
{
char name[30];
int stock;
float price, discount;
};
void main()
{
struct product p1 ={"Apple iPod Touch 32GB", 35,298.56, 2.32};
clrscr();
printf("Name = %s\n",p1.name);
printf("Stock = %d\n",p1.stock);
printf("Price = %.2f\n",p1.price);
printf("Discount= %.2f\n",p1.discount);
getch();
}
I hope this will help you understand C Structure, we will see more C structure example code in my next tutorial. If you have any question regarding this topic please drop comment. Thanks for dropping in.
Related C Programming Tutorials
- C Programming-Dynamic Memory Allocation
This tutorial is intended to tell beginner the power of pointers in C programming. This tutorial will try to explain how with the help of pointer we can solve the problem of memory management. If you dont... - Data Types in C Language
A programming language is proposed to help programmer to process certain kinds of data and to provide useful output. The task of data processing is accomplished by executing series of commands called... - Array in C programming Programmer's view
An array in C language is a collection of similar data-type, means an array can hold value of a particular data type for which it has been declared. Arrays can be created from any of the C data-types int,... - How to work with Two Dimensional Arrays in C
We know how to work with an array (1D array) having one dimension. In C language it is possible to have more than one dimension in an array. In this tutorial we are going to learn how we can use two... - File Copy Program in C Language
Today we are going to learn a simple file copy program in C language. As I said this is a simple file copy program so you should not expect its output like DOS copy command has. Ok lets start. The main... - What is Function in C Language?
A function in C language is a block of code that performs a specific task. It has a name and it is reusable i.e. it can be executed from as many different parts in a C Program as required. It also ... - How to work with Multidimensional Array in C Programming
C allows array of two or more dimensions and maximum numbers of dimension a C program can have is depend on the compiler we are using. Generally, an array having one dimension is called 1D array, array... - Pointers in C Programming
In this tutorial I am going to discuss what pointer is and how to use them in our C program. Many C programming learner thinks that pointer is one of the difficult topic in C language but its not... - C Programming Function Pointer
Like C variables, function too has address and we can use this address to invoke function. So this tutorial is entirely devoted to function-pointer. But before we can call a function using we need to find out... - C Programming - Recursive Function
We have learnt different types function C language and now I am going to explain recursive function in C. A function is called recursive if a statement within body of that function calls the same... - A Brief History of the C Language
Before we start any complex program in C, we must understand what really C is, how it came into existence and how it differs from other languages of that time. In this tutorial I will try to talk about these... - Types of Function in C Programming Languages:
In my previous c programming tutorial I tried to explain what the function, its advantages is and how to declare a C function. And I told you that there are five types of functions and they are: ...
Other Useful Tutorials
- Turn Your PC Keyboard to Musical Keyboard using C Program
To accomplish this task you dont need to buy any expensive software or hardware, only a little knowledge of C program will do and you can build your own musical keyboard software. Before we begin you... - How to enable Show Hidden Files and Folders option disabled after virus attack
If you ever faced such problem when your computer had a virus which ultimately disabled show hidden file and folders option, then you dont need to format or restore your setting. I have a trick... - How to Setup Laptop Mobile Internet using Bluetooth
In this tutorial I am not going to say same thing which I already mentioned in How to Setup Laptop Mobile Internet using Mobile Phone tutorial. In this workshop you will need a Bluetooth dongle and...
yes a structure can have more than one structure variable just like we create variable of int, char, float etc.
More advance topic on structure is here: http://hubpages.com/_98321/hub/Arrays-of-Structure
that was just great.. but still..i would love if you recommend a book for me... i have been reading let us c but seems it still not a able to understand it
i am very like you
nice tutorial.thnx for such tutorial.
Thnks for this tutorial,visit
greate ....i never seen before like this simple tutorial....
Nice tutorials, i bookmarked it for future references. I will also recommend this to my friends.
its really great nd gud to have dis helped me a lot......
NIC 1
i want variable explanation
great
fit
suprereb defination of c language
Wow ... very much simple and readable ... i have bookmarked it for the future reference also. Great job ... keep the good work up ....
thnks for this tutorial...................sir.thnks 1 again....................
arrays







madhura 24 months ago
can a structure have more than one structure variable