Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
C Program to calculate the salesmen salary with macro functions.
In C programming, macro functions provide a way to define constants and simple calculations that are processed during compilation. This program demonstrates how to calculate a salesman's salary using predefined macro constants for base salary, bonus rates, and commission percentages.
Problem Statement
A laptop manufacturing company has the following monthly compensation policy for their salespersons −
- Minimum base salary: 3000.00
- Bonus for every laptop sold: 200.00
- Commission on total monthly sales: 5 percent
Since laptop prices change monthly, the sales price of each laptop is fixed at the beginning of every month.
Syntax
#define MACRO_NAME value #define BASIC_SALARY 3000.00 #define BONUS_RATE 200.00 #define COMMISSION 0.05
Solution Logic
The salary calculation follows this approach −
-
Bonus calculation:
bonus = BONUS_RATE * quantity -
Commission calculation:
commission = COMMISSION * quantity * price -
Gross salary:
basic_salary + bonus + commission
Example
The following C program calculates the salesman's salary using macro functions −
#include <stdio.h>
#define BASIC_SALARY 3000.00
#define BONUS_RATE 200.00
#define COMMISSION 0.05
int main() {
int quantity;
float gross_salary, price;
float bonus, commission;
printf("Enter number of items sold and their price: ");
scanf("%d %f", &quantity, &price);
bonus = BONUS_RATE * quantity;
commission = COMMISSION * quantity * price;
gross_salary = BASIC_SALARY + bonus + commission;
printf("<br>--- Salary Breakdown ---<br>");
printf("Basic Salary = %.2f<br>", BASIC_SALARY);
printf("Bonus = %.2f<br>", bonus);
printf("Commission = %.2f<br>", commission);
printf("Gross Salary = %.2f<br>", gross_salary);
return 0;
}
Enter number of items sold and their price: 20 15000 --- Salary Breakdown --- Basic Salary = 3000.00 Bonus = 4000.00 Commission = 15000.00 Gross Salary = 22000.00
Key Points
-
Macro constants are defined using
#definedirective and processed during compilation - Macros improve code readability and make it easy to modify values
- The commission is calculated as 5% of total sales (quantity × price)
- Bonus is a fixed amount per laptop sold
Conclusion
Using macro functions in C provides an efficient way to define constants for salary calculations. This approach makes the code more maintainable and allows easy modification of compensation rates when company policies change.
