ProgramGuru

C Variables, Initialization and Constants

C Variables, Initialization and Constants

A class has 28 students, then one more arrives. Store the changing count separately from the fixed classroom capacity.

A variable is a named object that holds a value of a declared type. Initialization gives it a starting value; assignment replaces the stored value. const prevents changing an object through that qualified name.

  1. Explain

    Declare, initialize and update a count without confusing assignment with comparison.

  2. Trace

    Start with 27 present students and calculate the result after two arrivals.

  3. Apply

    Choose names for changing marks and values that should stay fixed.

Variables and Constants: follow the data

  1. Start

    A class has 28 students, then one more arrives. Store the changing count separately from the fixed classroom capacity.

  2. Operation

    A variable is a named object that holds a value of a declared type. Initialization gives it a starting value; assignment replaces the stored value. const prevents changing an object through that qualified name.

  3. Result

    Present: 29; places: 1

A guided model of this example. The illustration explains the process; run the complete C program to check its output.

Understand the operation

int present = 28 creates an integer object and initializes it. present = present + 1 reads its current value, adds one and stores the result. const int capacity = 30 expresses that this program does not change that value.

An automatic local variable without an initializer does not provide a useful starting value. Do not read it before storing a valid value. A variable name is a tool for explaining meaning, not a substitute for choosing the correct type.

Read the complete C program

This complete C17 program uses a small school example. Read declarations before the statements that use their values. Each printf format describes how the corresponding value is written to standard output.

C17 · complete program

#include <stdio.h>
int main(void) {
  const int capacity = 30;
  int present = 28;
  present = present + 1;
  printf("Present: %d; places: %d\n", present, capacity - present);
  return 0;
}

Expected output

Present: 29; places: 1
Run in C compiler →

Follow the values through the code

C17 · code used in this explanation

#include <stdio.h>
int main(void) {
  const int capacity = 30;
  int present = 28;
  present = present + 1;
  printf("Present: %d; places: %d\n", present, capacity - present);
  return 0;
}

present begins at 28 and capacity at 30.

The assignment stores 29. The remaining-place expression computes 30 - 29 = 1 without a second stored counter.

Follow the worked calculation

  1. Read the starting values

    present begins at 28 and capacity at 30.

  2. Perform the operation

    The assignment stores 29. The remaining-place expression computes 30 - 29 = 1 without a second stored counter.

  3. Check the observable result

    Present: 29; places: 1

    Present: 29; places: 1
A guided model of this example. The illustration explains the process; run the complete C program to check its output.

Try a small variation

C17 · code used in this explanation

#include <stdio.h>
int main(void) {
  const int capacity = 30;
  int present = 28;
  present = present + 1;
  printf("Present: %d; places: %d\n", present, capacity - present);
  return 0;
}

Start with 27 present students and calculate the result after two arrivals.

Worked answer

The += operator adds two to the stored count. Deriving remaining places from capacity and present keeps the two displayed values consistent.

C17 · complete program

#include <stdio.h>
int main(void) {
  const int capacity = 30;
  int present = 27;
  present += 2;
  printf("Present: %d; places: %d\n", present, capacity - present);
  return 0;
}

Expected output

Present: 29; places: 1
Run in C compiler →

Recognize the important boundaries

A const object is not necessarily a compile-time constant expression in C. Use an integer constant expression where the language requires one.

Reading an uninitialized automatic integer can cause undefined behavior; do not invent a predictable default such as zero.

Check your understanding

Choose the answer, then compare the reasoning for every option.

What does present = present + 1 do?
Choose one answer. You can try again.

Answer and explanation

Reads the previous count and stores a count one greater.

Correct. The right side is evaluated and assigned to the object on the left.

Use the idea independently

A variable is a named object that holds a value of a declared type. Initialization gives it a starting value; assignment replaces the stored value. const prevents changing an object through that qualified name.

Continue with choosing numeric types.

Language reference: WG14 C11 committee draft. These examples use the C17-compatible subset of the explained language features.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: C Data Types, Ranges and sizeof →