Tuesday, August 23, 2011

Electricity Billing System using Structures in C

This example illustrates the concept of structures discussed in previous post. Here structure variable bill is declared which has structure template Bill and members firstName, lastName, Address, previousUnit and presentUnit. The structure represents the information of a customer and the program calculates the cost by checking previous and present unit. After you understand the code you will have the knowledge of following things:

  1. Defining Structure
  2. Declaring Structure variable
  3. Accessing members of structure
  4. Passing structure in function
  5. Returning structure from function

The source code and output is given here…

Source Code

//generating electricity bill using structure in C
/** This block defines the structure Bill having
    fields for first name, last name, address,
    previous unit and present unit. The structure
    must be defined before main function */
struct Bill{
    char firstName[10];
    char lastName[10];
    char Address[20];
    float previousUnit;
    float presentUnit;
/** This is the function definition that
    calculates the cost. Here structure is passed
    as an argument */
float generateBill(struct Bill temp){
    float diff;
    diff = temp.presentUnit - temp.previousUnit;
    if(diff > 20){
        return diff*4.75;
        return 20*4.75+(diff-20)*7.75;
int main(){
    struct Bill bill; /*Declaration of structure variable*/
    printf("Fill up the following: \n");
    printf("First Name: ");
    gets(bill.firstName); //accessing member
    printf("Last Name: ");
    printf("Address: ");
    printf("Previous Unit: ");
    printf("Present Unit: ");
    printf("\a\n\n***********Electricity Bill***************\n\n\a");
    printf("Name: %s %s",bill.firstName,bill.lastName);
    printf("\nAddress: %s",bill.Address);
    printf("\nPrevious Unit: %.3f       Current Unit: %.3f",bill.previousUnit,bill.presentUnit);
    printf("\nCost: %.3f\n\n", generateBill(bill));
    return 0;




  1. i've take this code but id'nt know if it is 've a logic error iam feel so angryyyyyyyyyyyyyyyyyyyyyyy

  2. I want to continue this billing system for 100 houses how can i do this

    1. use while loop and save it to doc or text file.. to learn this search in google