Programs Need Somewhere to Put Things

Printing fixed text is a party trick. A real program takes values in, works on them, and reports results, which means it needs named places to keep those values while it works. Those named places are variables, and with them plus one input function you can already write the program that opens nearly every C exam: the simple interest calculator.

Declaring Variables

A declaration introduces a variable: its type, then its name.

int years;
double principal;

int holds whole numbers. double holds numbers with a fractional part, like an interest rate of 7.5. These two will carry you through this chapter; the full tour of types comes in the next one.

ANSI C has a placement rule that later dialects relaxed: all declarations come first in a block, before the first statement. Inside main that means every variable is declared at the top, then the work begins. Under this platform's strict flags, a declaration after a statement is a compile error, and on an exam paper it is a lost mark.

You can give a variable its starting value in the declaration, and this course always does:

int years = 0;
double principal = 0.0;

Why insist? Because a variable without an initializer holds an indeterminate value: whatever bits happened to be in that memory. Reading it before assigning is undefined behaviour, the first of many times this course will use that phrase. The habit that avoids the entire problem costs a few characters: initialize at declaration, every time.

Several Variables in One Declaration

One declaration can introduce several variables of the same type, with commas between the names. Textbooks and exam papers use this form constantly:

double amount = 0.0, value = 0.0, inrate = 0.11;

That is one statement: one type name, three variables, one semicolon. Written out one per line it means precisely the same thing.

double amount = 0.0;
double value = 0.0;
double inrate = 0.11;

The comma form has a trap, and it is the rule above waiting to bite. An initializer attaches to one name, not to the whole list:

double amount = 0.0, value, inrate;

Only amount is initialized there. value and inrate are declared and hold indeterminate values, which is exactly the situation to avoid, and it is easy to miss because at a glance the line looks like it initialized everything.

That is why this course declares one variable per line: each name sits beside its own initializer where you cannot overlook it. Read the comma form fluently, because exam papers are full of it, and write the one-per-line form.

Assignment

The = sign stores a value into a variable:

years = 3;
principal = 5000.0;

Read = as "gets", not "equals": the right side is computed, then stored into the left side. The old value is gone. Arithmetic uses the operators you expect, + - * /, and a variable can appear on both sides of its own assignment: total = total + interest; computes with the current value and stores the result back.

Reading Input with scanf

scanf is printf's opposite number: it reads values typed into the program. Here is the shape:

scanf("%d", &years);
scanf("%lf", &principal);

Two things demand attention. First, the conversion specifier must match the variable's type, and the pairs differ between input and output: %d reads an int, but a double is read with %lf and printed with %f. Mixing those up is a classic paper-marking trap and a real bug.

Second, the &. It is the address-of operator: &years means "the location of years in memory" rather than its value. scanf needs the location because its job is to put something there. Forget the & and the program compiles with a warning and does something undefined at run time. For now, treat "scanf arguments start with &" as a rule; the chapter on pointers turns the rule into understanding.

One honest caveat: scanf reports how many values it successfully read, and robust programs check that report. Doing something useful with it needs if, which arrives in chapter 5, so the programs in these early chapters trust their input, the way exam-paper programs do. The habit gets fixed the moment the language to fix it exists.

The Exam Classic, Complete

Every piece so far, in one program:

Run it and type three numbers separated by spaces, such as 5000 7.5 3. A few details worth noticing:

  • The declarations sit together at the top of main, each initialized: the ANSI C shape.
  • One scanf can read several values; whitespace in the format string skips the spaces or newlines between them.
  • %.2f in printf prints a double with exactly two digits after the decimal point, which is how money should look.
  • The prompt printf ends without \n deliberately, so your typing appears on the same line as the prompt.

What the Machine Is Doing

A declaration reserves a small patch of memory and attaches your chosen name to it for as long as main runs. Assignment writes bytes into that patch; using the variable reads them back. &years is the patch's address, which is why handing it to scanf lets scanf deliver the typed value to the right place. The next chapter starts measuring those patches with sizeof, and by the pointers chapter you will be passing addresses around on purpose. The picture stays this simple: named patches of memory, values moving in and out.

Key Takeaways

  • A declaration is type then name; in ANSI C all declarations in a block come before the first statement.
  • Initialize every variable at declaration; reading an uninitialized variable is undefined behaviour.
  • Several variables of one type can share a declaration, separated by commas, but each initializer binds to a single name; this course writes one variable per line.
  • = stores the right side into the left side; the old value is overwritten.
  • scanf("%d", &x) reads an int; %lf reads a double (which printf prints with %f). The & hands scanf the variable's address so it can store what it reads.
  • %.2f prints with two decimal places; simple interest is principal times rate times years over 100.