C Tutorial

C Project: Inventory

Track item names, stock, and price. Print a table and the total value of the stock.

What you will build

An inventory is a list of items. Each item has a name, a quantity on the shelf, and a unit price. Line value is quantity times price. The total value of the stock is the sum of those line values. Print a table, then print that total.

Store a few records in an array of structs. Hardcode the demo so gcc can run with no typed input. ClickTry it in C for /c/try. That is the C editor.

Name, quantity, price

Three fields, three types: a char array, an int, and a double. Print one row to prove the layout before you add a loop.

Example

#include <stdio.h>

struct Item {
  char name[24];
  int qty;
  double price;
};

int main(void) {
  struct Item nail = {"nails", 100, 0.05};
  double line = nail.qty * nail.price;
  printf("%s: %d x %.2f = %.2f\n", nail.name, nail.qty, nail.price, line);
  return 0;
}

100 nails at 0.05 should print 5.00. If you see 0.00, the price field never received 0.05, or you multiplied with integers only.

A table of stock

Put several items in an array. Print a header, then one row per item. Align columns with a width in the format string so names, counts, and money line up.

Example

#include <stdio.h>

struct Item {
  char name[24];
  int qty;
  double price;
};

int main(void) {
  struct Item stock[] = {
      {"nails", 100, 0.05},
      {"screws", 40, 0.12},
      {"hinges", 8, 2.50},
      {"paint", 3, 14.99},
  };
  int n = (int)(sizeof stock / sizeof stock[0]);

  printf("%-10s %6s %8s %10s\n", "item", "qty", "price", "value");
  for (int i = 0; i < n; i++) {
    double value = stock[i].qty * stock[i].price;
    printf("%-10s %6d %8.2f %10.2f\n", stock[i].name, stock[i].qty,
           stock[i].price, value);
  }
  return 0;
}

Compile the table at /c/try. Check that hinges show 20.00 in the value column. Use the C gcc editor, not the Python, HTML, or C++ playground.

Total value of the stock

Add each line value into a running total. Print the total after the table. That number is the point of the project: how much money sits on the shelf if you value every unit at its listed price.

Example

#include <stdio.h>

struct Item {
  char name[24];
  int qty;
  double price;
};

double line_value(const struct Item *it) {
  return it->qty * it->price;
}

int main(void) {
  struct Item stock[] = {
      {"nails", 100, 0.05},
      {"screws", 40, 0.12},
      {"hinges", 8, 2.50},
      {"paint", 3, 14.99},
  };
  int n = (int)(sizeof stock / sizeof stock[0]);
  double total = 0.0;

  printf("%-10s %6s %8s %10s\n", "item", "qty", "price", "value");
  for (int i = 0; i < n; i++) {
    double value = line_value(&stock[i]);
    total += value;
    printf("%-10s %6d %8.2f %10.2f\n", stock[i].name, stock[i].qty,
           stock[i].price, value);
  }
  printf("%-10s %6s %8s %10.2f\n", "total", "", "", total);
  return 0;
}

5.00 + 4.80 + 20.00 + 44.97 is 74.77. If your total differs, print each line value on its own first. Do not round in the middle of the sum; let printf round only for display.

Find the dearest line

After the total works, walk the array once more to find the item whose line value is largest. That is the shelf line you would miss most if it vanished. Reuse line_value so the multiply lives in one place.

Example

#include <stdio.h>

struct Item {
  char name[24];
  int qty;
  double price;
};

double line_value(const struct Item *it) {
  return it->qty * it->price;
}

int main(void) {
  struct Item stock[] = {
      {"nails", 100, 0.05},
      {"screws", 40, 0.12},
      {"hinges", 8, 2.50},
      {"paint", 3, 14.99},
  };
  int n = (int)(sizeof stock / sizeof stock[0]);
  int richest = 0;
  double total = 0.0;

  for (int i = 0; i < n; i++) {
    double value = line_value(&stock[i]);
    total += value;
    if (value > line_value(&stock[richest])) {
      richest = i;
    }
  }

  printf("stock value: %.2f\n", total);
  printf("largest line: %s (%.2f)\n", stock[richest].name,
         line_value(&stock[richest]));
  return 0;
}

Paint should win: 44.97 against 20.00 for hinges. Quantity 3 is small, but the unit price is not.

Common mistakes

  • Storing price as int. Then 0.05 becomes 0 and the nails line is worthless.
  • Printing money with %d. Use %.2f for two decimal places.
  • Multiplying in the total and again in the table with two different expressions that drift apart. One helper keeps them equal.
  • Forgetting that qty is a count. A restock should add to qty, not replace the name.

Practice

  1. Print a second total that counts units: the sum of every qty.
  2. Mark any item with qty below 5 as low stock in an extra column.
  3. Add a restock of 10 nails in main and print the table before and after so the nails value changes from 5.00 to 5.50.

FAQ: C Project: Inventory

Common questions about this page.

What is the StudyGrid C tutorial?

The StudyGrid C tutorial is a full beginner-to-advanced track: syntax, types, input, loops, functions, pointers, structs, files, and the standard library. Each chapter has copy-and-run examples.

Should I run c inventory project examples locally for better learning?

Yes. Use the browser editor on StudyGrid for a quick check, then Download the example and run it on your computer. Local runs show real errors and the real toolchain, which is one of the fastest ways to learn c inventory project in this C C lesson (C Project: Inventory).

Is the C editor the same as Try Python, Try HTML, or Try C++?

No. Try C compiles with gcc at /c/try and shows stdout plus compiler messages. Try Python stays at /try. Try HTML stays at /html/try. Try C++ stays at /cpp/try. C lessons never open those editors.

Do I need to install a compiler to learn C?

No. Open a chapter, click Try it in C, and compile in the browser. You can also download a .c file and compile locally with gcc.

Where should I start the C tutorial?

Start at C Intro, then Get Started and Syntax. After the first program, continue to output, variables, and if-else. After pointers, open C Examples, then files and bitwise. Use Next at the bottom of each chapter.

Is the C tutorial free?

Yes. The C workshop on StudyGrid (studygrid.in) is free: dashboard, chapters, and the compile-and-run editor.