In Part 1, we separated saving source code, building a program, and running it. Now let's give the program values to store and calculations to perform.
Our goal is to understand variables, initialization, assignment, and the types inside a small arithmetic expression. By the end, you will be able to explain why storing 5 / 2 in a double produces 2.0, and calculate a useful progress percentage.
Series: C, One Experiment at a Time — Part 2.
Before you start: You should be able to save, build, and run a C program. Part 1 walks through those steps.
Test environment: Windows, MSVC, C17 mode, x64. Commands use an x64 Visual Studio developer command prompt. The arithmetic examples use small values and nonzero divisors.
1. Start with named values
Save this complete program as values.c:
#include <stdio.h>
int main(void) {
int attempts = 3;
double seconds = 2.5;
int snapshot = attempts;
printf("initial: attempts=%d seconds=%.1f\n", attempts, seconds);
attempts = attempts + 1;
printf("updated: attempts=%d snapshot=%d\n", attempts, snapshot);
return 0;
}
Build it with cl /nologo /std:c17 /W4 values.c, then run values.exe after the build succeeds.
The tested output is:
initial: attempts=3 seconds=2.5
updated: attempts=4 snapshot=3
int attempts = 3; defines an integer variable named attempts and initializes it to 3. double seconds = 2.5; defines a floating-point variable and initializes it to 2.5.
The name identifies the variable; the type affects which values it can represent and how expressions using it behave. We use int for these small whole-number counts and double for a value with a fractional part. Floating-point types do not represent every decimal fraction exactly; these particular small examples avoid that extra issue.
In printf, %d formats an int, while %.1f formats a double with one digit after the decimal point. Formatting controls the displayed text. It does not change how the earlier arithmetic was performed.
2. Initialization and later assignment do different jobs
This line supplies an initial value when the variable is defined:
int snapshot = attempts;
At this point, attempts is 3, so snapshot receives that value.
Later, this assignment updates attempts:
attempts = attempts + 1;
The right side computes 3 + 1; the result becomes the new value of attempts. The assignment operator = is not a mathematical claim that the two sides must remain equal forever.
snapshot stays 3. Initializing this separate int variable copied a value; it did not establish a live connection between the variables. A snapshot is a photo, not a livestream.
| Point in the program | attempts | snapshot |
|---|---|---|
| After snapshot is initialized | 3 | 3 |
| After attempts is assigned attempts + 1 | 4 | 3 |
Initialize these local variables before reading them. This tutorial does not use an uninitialized value as a way to generate an interesting result.
3. Follow the expression before its destination
Consider:
double assigned = 5 / 2;
The two constants are of type int. Their division therefore produces an integer result: 2. That result is then converted to double for initialization, giving 2.0.
The destination type does not retroactively turn the division into floating-point division. Here is a useful way to trace it:
int 5 / int 2 -> int 2 -> convert to double -> 2.0
Compare that with:
double fractional = 5.0 / 2;
5.0 is a double constant. For this division, the int operand is converted to double, and the calculation produces 2.5.
double 5.0 / int 2 -> floating-point division -> double 2.5
These are explanations of the expression's types and value, not promises about the exact machine instructions a compiler will emit.
4. Put the conversion where it can help
Save this as division.c:
#include <stdio.h>
int main(void) {
int a = 5;
int b = 2;
double assigned = a / b;
double cast_after = (double)(a / b);
double cast_before = (double)a / b;
double right_operand = a / (double)b;
printf("assigned=%.1f\n", assigned);
printf("cast_after=%.1f\n", cast_after);
printf("cast_before=%.1f\n", cast_before);
printf("right_operand=%.1f\n", right_operand);
return 0;
}
Build with cl /nologo /std:c17 /W4 division.c, then run division.exe.
assigned=2.0
cast_after=2.0
cast_before=2.5
right_operand=2.5
(double)(a / b) converts the result of integer division. The fractional part was already discarded by that division, so converting 2 produces 2.0.
(double)a / b converts an operand before division. In this example, that makes the calculation floating-point division. Converting the other operand, as in a / (double)b, also works here.
A cast is not a general correctness repair. We are using it for a specific purpose: changing the type of an operand in this expression.
5. Use the rule to calculate progress
Suppose you have completed 5 of 8 planned exercises. You expect 62.5%.
Save this as progress.c:
#include <stdio.h>
int main(void) {
int completed = 5;
int planned = 8;
int lost_fraction = completed / planned * 100;
double percent = 100.0 * completed / planned;
printf("integer_first=%d\n", lost_fraction);
printf("floating_first=%.1f\n", percent);
return 0;
}
Build with cl /nologo /std:c17 /W4 progress.c, then run progress.exe.
integer_first=0
floating_first=62.5
In completed / planned * 100, the operators group left to right: (5 / 8) * 100. Integer division produces 0, and multiplying that by 100 still gives 0.
In 100.0 * completed / planned, the calculation starts with a double operand. With our values, 500.0 / 8 produces 62.5. A nonzero planned value is a condition of this example; we will handle input checks when we reach conditions and branches.
All the examples use small values. They are not a complete guide to integer overflow, division by zero, signed/unsigned conversions, or floating-point rounding.
Try it yourself
- In
values.c, initializeattemptsto7. Predictattemptsandsnapshotafter the assignment. - In
division.c, setato7andbto2. Predict all four results before running it. - In
progress.c, setcompletedto3and keepplannedat8. Predict both printed results.
Check your reasoning
-
attemptsbecomes8;snapshotstays7. - The results are
3.0,3.0,3.5, and3.5. - Integer-first gives
0; floating-first gives37.5.
These exercise predictions follow the same rules. The three main programs above were compiled and run locally; see the testing note below for the tested scope.
Next, we will use conditions and loops to let a program choose and repeat work. For now, the habit to keep is: identify the operands, follow the calculation, and only then look at the destination variable.
Have you encountered a calculation where changing the destination type did not change the answer? What expression caused it?
References and testing
The three complete programs above were built and run with MSVC in C17 x64 mode, with /W4 /WX during verification. Output was checked against expected results. The test record also contains a separate size/limits experiment, which this tutorial does not use to claim fixed type sizes across implementations.
- MSVC: initializing scalar types
- MSVC: assignment conversions
- MSVC: usual arithmetic conversions
- MSVC: printf format specifications
Writing note: Prepared with AI assistance; the main code examples were tested locally.

Top comments (0)