C / CHAPTER 01

Values and explicit computation

Beginner

Read a complete C11 program and trace its arithmetic.

By the end of this chapter

  • Initialize scalar objects
  • Match output formats to types
  • Distinguish division and remainder

The idea

Begin with arithmetic and read each statement in order. A C11 console program can define main with no arguments and use stdio.h for formatted output. A declaration such as int count = 14 creates an integer object and initializes it. C does not automatically initialize ordinary local variables, so always give a value before reading one. A semicolon ends a statement rather than making its expression safe.

Integer division computes a whole-number quotient, and remainder records the leftover part. Use small positive values while learning so overflow and negative-operand rules do not obscure the main idea. printf interprets its format string, so each conversion must match the corresponding argument’s type. The compiler and its warnings are useful partners, but you must still check the intended calculation and its input assumptions.

C example
#include <stdio.h>
int main(void){int seats=23, row=5; printf("%d %d\n",seats/row,seats%row); return 0;}
Result
4 3

Four whole groups consume twenty seats, leaving three. Both results are int values, matching the two %d conversions.

Try it yourself

Trace quotient and remainder for 31 divided into groups of 6.

Use your own editor for this exercise. Runnable challenges on CodeQuest are available for JavaScript, Python, and SQL.

CHECK YOUR UNDERSTANDING

Chapter checkpoint

What does int n = 11 / 4; store?