# C Structure and User Define Function Exercise with Item Details

By: IncludeHelp, On 17 NOV 2016

Write a function that passes the structure of an Item Structure along with Item’s Name, Quantity, and Price. And function should calculate the total amount (Quantity* Price).

Input

Enter Item name: Dove
Enter price: 38.50
Enter quantity: 10

Output

Item name: Dove, Price: 38.50, Quantity: 10
Total Amount: 385.00

In this example we will declare a structure named Item with members itemName, quantity, price and amount.

There is a user define function calAmount() which is taking address of structure variable along with other temporary parameters, function will assign temporary variable’s values to the structure members and also calculate the total amount.

## C Example with Item Structure passing in User Defined Function

```#include <stdio.h>
#include <string.h>

//structure declaration
struct Item{
int quantity;
float price,amount;
char itemName[50];
};

//function to calculate total amount of item
void calAmount(struct Item *item,char *name, int qty, float price){
strcpy(item->itemName,name);
item->quantity=qty;
item->price=price;
item->amount=(float)qty*item->price;
}

int main(){
//declaration temporary variables
char name[50];
int qty;
float price;

//declare structure variable
struct Item ITEM;

printf("Enter Item name: ");
scanf("%s",name);
printf("Enter price: ");
scanf("%f",&price);
printf("Enter quantity: ");
scanf("%d",&qty);

//fill structure and calculate total amount
calAmount(&ITEM,name,qty,price);

//print structure values
printf("Item name: %s, Price: %.02f, Quantity: %d\n",ITEM.itemName,ITEM.price,ITEM.quantity);
printf("Total Amount: %.02f\n",ITEM.amount);

return 0;
}
```

### Output

```Enter Item name: Dove
Enter price: 38.50
Enter quantity: 10
Item name: Dove, Price: 38.50, Quantity: 10
Total Amount: 385.00
```