Skip to content
Home · Sonraki hedefe 100 XP
Son hareketler
Henüz bir öğrenme hareketi yok. İlk derse başlayın.

Programming Fundamentals

C++ variables: Initialization and immutability

A variable is identified by its type, name, and lifetime. Initializing the value when it is created is the key habit to prevent accidentally reading an ambiguous value later.

Bu derste neler var?

SIBERSALDIRI.COM · FOCUSED LESSON

What you will learn

  • Explain the relationship between Type and Initialization.
  • “Is const just speed optimization?” To give a reasoned answer to the question.
  • Changing the example and comparing expected and observed behavior.

How it works and where the trust boundary lies

Initializing with curly braces catches some of the narrowing transformations at compile stage. Make the value const that will not change later. Declare the variable close to where it will first be used; Accumulating disconnected names at the beginning of a large function obscures which value is valid and when.

C++ variables: Initialization and immutability flow: Type → Initialization → Value usage
Type → Initialization → Value usage. The diagram shows the relationship; It does not replace the entire application.

Worked example

The following passage makes the concept visible. Evaluate samples containing commands or code in your own separate testing environment, not in the production system. Architectural diagrams and policy examples are not executable programs.

#include <iostream>
int main() {
 const int unitPrice{25};
 int quantity{3};
 std::cout << unitPrice * quantity;
}

Expected result and interpretation

75 is calculated; unitPrice cannot be changed later.

Compare the scenarios

StatusRecommended approachWhy?
Unchanging priceconstUnwanted updates are blocked
Contractor startReview compiler warningThere may be loss of value
First use awayZoom in on definitionThe validity range becomes clear

Practice task

Change the quantity. Observe the const check by trying to change the price.

  1. Write down the expected outcome and acceptance condition before the action.
  2. Change a single variable; Record the observation and the medium you used.
  3. Evaluate the result with the comparison table above. Explain which assumption might be wrong if it does not fit.

Common mistake and how to correct it

When using auto, do not leave arithmetic or API boundaries unclear where type matters.

Test your understanding

Is const just speed optimization?

Answer explained: No. First of all, it clarifies the value that should not change in the program contract.

Continue with related lessons

Technical reference

The explanation and examples of this course have been re-prepared for the academy. The link below is a technical reference to verify details of the technology or security approach used.

Lessons in this section

Bir adım daha ilerlediniz.

Dersi bitirdiyseniz tamamlandı olarak işaretleyin. İlerlemeniz bu cihazda saklanır.

Academy menu

Akademide ara

En az 2 karakter yazın.