Structure Assignment 21 — Give a Salary Hike to an Employee
  • Twitter
  • Facebook
  • Snapchat
  • Instagram

C Program: Give a Salary Hike to an Employee Using a Structure

📑 On this page:
  • One line, one raise
  • The percentage formula
  • The three steps
  • Complete C Program
  • Program Output
  • Step-by-Step Explanation
  • Frequently Asked Questions

One line, one raise

Here's a fun fact: the entire salary hike in this assignment is one line of arithmetic. e.salary = e.salary * (1 + percent / 100.0); — that's it. Everything else is just filling the employee record and printing the results. So if you thought this was going to be a big program with lots of moving parts, you're in for a pleasant surprise.

But the simplicity is exactly why this assignment is worth doing. It shows you how little code is needed once the data is in the right shape. The struct holds the employee's salary, so a raise is just a multiply. The print code shows the before and after, so the user can see the effect. And the only real decision is how to read the percentage and what to do if the user enters something weird.

The percentage formula

A percentage raise of p percent means the salary goes up by salary × p / 100. So the new salary is salary + salary × p / 100 = salary × (1 + p / 100). For a 10% raise on 55,000, that's 55,000 × 1.10 = 60,500. For a 7.5% raise, it's 55,000 × 1.075 = 59,125.

The trick in C is to make sure the division p / 100 happens as a floating-point division, not integer division. If you write 1 + p / 100 and both p and 100 are integers, C computes the integer result — so for p = 10, 10 / 100 = 0, and the multiplier becomes 1.0, meaning no raise at all. Writing 100.0 instead of 100 forces floating-point division, which is what you want. This is the same integer-division trap we saw with the reciprocal series earlier.

You can also compute the raise amount separately and add it: raise = e.salary * percent / 100.0f;, then e.salary = e.salary + raise;. This is arguably clearer because "raise" is a meaningful name, and it lets you print the raise amount separately from the new salary. Both approaches give the same final number; the choice is about readability.

💡 In one line: new_salary = old_salary * (1 + percent / 100.0). Use 100.0, not 100 — otherwise the division truncates to zero and no raise happens.

An employee record with salary 55000.00, an arrow labelled +10% leading to a new record with salary 60500.00, with the raise amount 5500.00 highlighted between them.

The three steps

Three steps, and the third one is the interesting one:

  1. Fill the employee record — name, ID, and the current salary.
  2. Read the hike percentage from the user. Validate that it's zero or positive.
  3. Compute and print the new salary: e.salary * (1 + percent / 100.0).

That's the whole algorithm. The 100.0 in the third step is the only subtle thing. Get that wrong and the entire program silently does nothing useful — the "new salary" equals the old salary, and you might spend ten minutes wondering why.

Old salary Hike % Raise amount New salary
55000.00105500.0060500.00
55000.007.54125.0059125.00
47500.7552375.0449875.79
60000.0000.0060000.00
45000.002511250.0056250.00

The third row shows a subtle point: 47500.75 × 5% = 2375.0375, which displays as 2375.04 after rounding to two decimals. The new salary displays as 49875.79 — off by a cent from the "true" 49875.7875. That's because float arithmetic doesn't track exact pennies. For display purposes, it's fine; for actual payroll, this would be a real bug (you can't pay someone half a cent, so real systems handle rounding explicitly).

Complete C Program

Program: apply a salary hike and print before and after

#include <stdio.h>

struct employee {
    char  name[50];
    int   id;
    float salary;
};

int main(void) {
    struct employee e = {"Alice Johnson", 1001, 55000.0f};
    float percent;

    printf("Enter hike percentage: ");
    if (scanf("%f", &percent) != 1 || percent < 0) {
        printf("Invalid percentage.\n");
        return 1;
    }

    float old_salary = e.salary;
    float raise      = old_salary * percent / 100.0f;
    e.salary         = old_salary + raise;

    printf("\nEmployee   : %s (ID %d)\n", e.name, e.id);
    printf("Old salary : %.2f\n", old_salary);
    printf("Hike       : %.2f%% (%.2f)\n", percent, raise);
    printf("New salary : %.2f\n", e.salary);

    return 0;
}
                                    

And here's a version that reads the entire employee record from the user, so you can try different employees without recompiling:

Variant: read the employee and hike from the user

#include <stdio.h>

struct employee {
    char  name[50];
    int   id;
    float salary;
};

int main(void) {
    struct employee e;

    printf("Enter name: ");
    scanf(" %49[^\n]", e.name);
    printf("Enter ID: ");
    scanf("%d", &e.id);
    printf("Enter current salary: ");
    scanf("%f", &e.salary);

    float percent;
    printf("Enter hike percentage: ");
    if (scanf("%f", &percent) != 1 || percent < 0) {
        printf("Invalid percentage.\n");
        return 1;
    }

    float old_salary = e.salary;
    float raise      = old_salary * percent / 100.0f;
    e.salary         = old_salary + raise;

    printf("\n%s (ID %d)\n", e.name, e.id);
    printf("Old salary : %.2f\n", old_salary);
    printf("Hike       : %.2f%% (%.2f)\n", percent, raise);
    printf("New salary : %.2f\n", e.salary);
    return 0;
}
                                    

Both versions do the same math. The only difference is whether the employee record is hardcoded or read from the user. The output for the first version is the one you'll see below.

Program Output

Enter hike percentage: 10

Employee   : Alice Johnson (ID 1001)
Old salary : 55000.00
Hike       : 10.00% (5500.00)
New salary : 60500.00
                                

And for the user-input version, try a fractional hike:

Enter name: Bob Smith
Enter ID: 2042
Enter current salary: 47500.75
Enter hike percentage: 7.5

Bob Smith (ID 2042)
Old salary : 47500.75
Hike       : 7.50% (3562.56)
New salary : 51063.31
                                

Try a 0% raise — the new salary should equal the old one, and the raise should be 0.00. Try a large hike like 100% — the salary should double. Try entering a negative percentage — the code should reject it with an error message (thanks to the percent < 0 check). These are the cases a real payroll system would have to handle.

Step-by-Step Explanation

Walk-through of the main version with a 10% raise.

1. The struct

Same struct employee as the last two assignments. The struct doesn't change for this assignment — the salary field is already there, ready to be updated.

2. The hardcoded employee

struct employee e = {"Alice Johnson", 1001, 55000.0f}; fills the record at declaration. If this is a first run and you want to see the hike happen, hardcoded values are simpler — you know exactly what to expect.

3. Reading the percentage

scanf("%f", &percent) reads a float into the percent variable. The != 1 check verifies that scanf actually read something. The percent < 0 check rejects negative raises, which don't make sense as a "hike". Both checks together ensure the percentage is a valid non-negative number.

4. The raise calculation

Three lines. First, save the old salary in old_salary so we can print it later. Then compute raise = old_salary * percent / 100.0f — note the 100.0f, which forces floating-point division. Finally, update the salary: e.salary = old_salary + raise. The three lines could be collapsed into one, but this way the raise is visible as a separate value, which is useful for the output.

5. Why 100.0f and not 100

If you write old_salary * percent / 100 with an int 100, C does the division in floating point because old_salary and percent are floats — the 100 gets promoted to float. So actually the code works either way. But it's a good habit to write 100.0f or 100.0 explicitly, because if you ever write the expression the other way around (percent / 100 * old_salary), the percent / 100 part would be evaluated first, and with an int 100 that would be integer division — 10 / 100 = 0 — losing the raise entirely. Explicit float literals prevent this whole class of bugs.

6. Printing before and after

The output shows the old salary, the raise amount, and the new salary — all in a small table. The %.2f format makes all three values look like real money (two decimal places). The label "Hike : 10.00% (5500.00)" shows both the percentage and the actual amount added, which makes the effect visible even if the new salary is the only number the user cares about.

7. The float rounding problem

Here's where money and floats disagree. If you raise 47500.75 by 7.5%, the "true" raise is 3562.55625. The printed raise shows 3562.56 (rounded), and the printed new salary shows 51063.31 — but 47500.75 + 3562.55625 is exactly 51063.30625, which rounded is 51063.31. The rounding looks right here, but in other cases it can be off by a cent. That's why real payroll systems use integer cents (4750075 cents) and do all the math in cents, only converting back to dollars for display. For a teaching example, float is fine; for real money, use integers.

🎯 Remember this: new_salary = old_salary * (1 + percent / 100.0). Save the old salary before updating so you can show the before and after. Validate the percentage (reject negatives). Use %.2f for money. For real financial code, use integer cents, not floats.

Frequently Asked Questions

1. Why does my raise come out as zero when I enter 10?

You're probably computing percent / 100 as an integer division. If percent is an int (or a float but the expression has an int 100 in it that gets evaluated first), then 10 / 100 = 0, and the multiplier becomes 1.0 — no raise. Fix it by using a float literal: percent / 100.0f. That forces floating-point division regardless of the type of percent.

2. Can I use double instead of float for the salary?

Yes. double has more precision (about 15 significant digits vs 7 for float), which reduces rounding errors. The struct field and the local variables would both become double, and the printf format stays %.2f (which works for both). For teaching, float is fine; for real code, double is a bit safer. If you're worried about rounding, use integer cents instead of either.

3. What if the user enters a fractional hike percentage?

It works fine — scanf("%f", &percent) accepts decimals, so 7.5 reads correctly as 7.5%. A 7.5% raise on 55000 is 4125, giving 59125. This is why the percentage should be a float, not an int. If you used int, 7.5 would read as just 7 (and leave the .5 in the input buffer, confusing the next scanf).

4. What if the user enters a negative percentage?

With the percent < 0 check, the program prints an error message and exits. That's the safe behavior. You could also interpret negative percentages as "demotions" and apply them, but for a "salary hike" assignment, rejecting them makes sense. Real payroll systems would handle negative percentages differently (they'd be a separate operation).

5. How would I apply a flat raise (like $5000) instead of a percentage?

Same pattern, different formula: e.salary = old_salary + 5000.0f;. Then the "raise" is a fixed amount. You could also let the user choose between percentage and flat raise — a char choice input ('p' for percent, 'f' for flat), then an if-else to apply the right formula. The struct doesn't care; it just stores whatever the salary becomes.

6. Can I apply a hike to multiple employees at once?

Yes — store an array of employees and loop over it. for (int i = 0; i < n; i++) { emp[i].salary *= (1 + percent / 100.0f); }. That gives everyone the same percentage raise. For different raises per employee, you'd read a percentage for each one, or apply rules based on their current salary, department, performance, etc. The struct-per-employee setup makes all of this straightforward.

💡 Tip: After this, try applying a hike in two stages — 5% now, then 5% again — and compare the result to a single 10% hike. Spoiler: they're not the same. 5% twice compounds to 10.25%, not 10%. That's a useful insight for understanding percentages and why "two 5% raises" is better than "one 10% raise" from the employee's perspective. It's also a good example of why the order of operations matters in real code.

📖 Related Tutorials

  • 19. Create a Structure to Represent an Employee
  • 20. Initialize and Display the Details of an Employee Using a Structure
  • 22. Create a Structure to Represent a Complex Number
  • Structures and Functions
  • Accessing Structure Members
  • Defining a Structure
  • Array of Structures
  • All 25 Structure Assignments

Previous Topic: -->> 20. Initialize and Display the Details of an Employee Using a Structure   ||   Next Topic: -->> 22. Create a Structure to Represent a Complex Number


C Language

  • Home
  • Why C Language
  • History of C Language
  • Applications of C Language
  • Introduction To C
    • What is Program?
    • Structure of C Program
    • Working Of C Program
    • CHARACTER SET
    • VARIABLES AND IDENTIFIERS
    • BUILT-IN DATA TYPES
    • OPERATORS AND EXPRESSIONS
    • CONSTANTS AND LITERALS
    • SIMPLE ASSIGNMENT STATEMENT
    • BASIC INPUT/OUTPUT STATEMENT
    • SIMPLE 'C' PROGRAMS
    • Assignments
  • Operators in C Programming
    • Arithmetic Operators
    • Assignment Operators
    • Increment and Decrement Operators
    • Relational Operators
    • Logical Operators
    • Bitwise Operators
    • Other Operators
    • Assignments
  • CONDITIONAL STATEMENTS
    • DECISION MAKING WITHIN A PROGRAM
    • CONDITIONS
    • IF STATEMENT
    • IF-ELSE STATEMENT
    • IF-ELSE LADDER
    • NESTED IF-ELSE
    • SWITCH CASE
    • Assignments
  • LOOPS STATEMENTS
    • Introduction to Loops
    • GO TO Statement
    • Do while Loop
    • While Loop
    • Nested While Loop
    • Difference Between While and Do while
    • Difference Between Goto and loop
    • while loop assignments
    • C FOR Loop
    • C For loop examples
    • Nested for loop
    • Nested for loop examples
    • Infinite while Loops
    • Infinite for Loops
    • Continue in Loops
    • break in Loops
    • difference while do..while & for
    • Assignments
  • Arrays
    • One Dimensional Array
    • Declaring 1D Arrays
    • Initilization of 1D arrays
    • Accessing element of one 1D Array
    • Read and Display 1D Arrays
    • Two Dimensional Arrays
    • Declare 2D Arrays
    • Read and Display 2D Arrays
    • Assignments/Examples
  • Functions
    • Introduction
    • Need For User-Defined Function
    • Multiple Function Program
    • Modular Programming
    • Elements Of User Defined Function
    • Function Definition
    • Function Declaration
    • Types of functions
    • Nesting of Function
    • Recursion
    • Passing Array To Functions
    • Scope,Visibility and Lifetime of Variables
    • Assignments
  • Structure
    • Introduction
    • Array vs Structure
    • Defining Structure
    • Declaring Structure Variables
    • Type Defined Structure
    • Accessing Structure Members
    • Structure Initilization
    • Copying & Comparing Structure Variables
    • Array of Structure
    • Arrays Within Structure
    • Structures Within Structures
    • Structures and Functions
    • Structure Examples/Assignments
  • Union
    • Define Union
    • Create and use Union
    • Difference Between Structure and Union
    • Union Examples
    • Union FAQ
  • Pointers
    • What Are Pointers In C?
    • How Do We Use Pointers In C?
    • Declaration Of A Pointer
    • The Initialization Of A Pointer
    • Syntax Of Pointer Initialization
    • Use Of Pointers In C
    • The Pointer To An Array
    • The Pointer To A Function
    • The Pointer To A Structure
    • Types Of Pointers
    • The Null Pointer
    • The Void Pointer
    • The Wild Pointer
    • The Near Pointer
    • The Huge Pointer
    • The far Pointer
    • dangling pointer
    • Accessing Pointers- Indirectly And Directly
    • Pros Of Using Pointers In C
    • Cons Of Pointers In C
    • Applications Of Pointers In C
    • The & Address Of Operator In C
    • How To Read The Complex Pointers In C?
    • Practice Problems On Pointers
  • File Processiong
    • File Handling In C
    • Types Of Files In C
    • Operations Done In File Handling
    • File Examples
    • Binary Files
    • count words,lines in a file
    • Copy files
    • Update File
    • count vowels in a file
  • Preprocessor
    • Macro substitution division
    • File Inclusion
    • Conditional Compilation
    • Other directives
    • Examples
  • Dynamic Memory Allocation
    • malloc
    • calloc
    • free
    • realloc
    • Examples
  • Storage Classes
  • Graphics
  • Frequently Asked Interview Questions (FAQ)
    • Introduction To C FAQ
    • Operators FAQ
    • Conditional Statements FAQ
    • Loops FAQ
    • Arrays FAQ
    • Function FAQ
    • Structure FAQ
    • Pointers FAQ
    • Files FAQ
    • Storage classes FAQ
    • Dynamic Memory FAQ
  • Programs/Assignments
    • Introduction To C
    • Operators
    • Conditional Statements
    • Loops
    • Arrays
    • Function
    • Structure
    • Pointers
    • Files
    • Storage classes
    • Dynamic Memory
  • Case Studies
  • Multiple Choice Questions
    • Introduction To C MCQ
    • Operators MCQ
    • Conditional Statements MCQ
    • Loops MCQ
    • Arrays MCQ
    • Function MCQ
    • Structure MCQ
    • Pointers MCQ
    • Files MCQ
    • Storage classes MCQ
    • Dynamic Memory MCQ
    • More MCQ

Get in touch

  • tech2dsm@gmail.com

© Sankalan Data Tech. All rights reserved.