Variables and types
لم يُترجم بعد؛ يُعرض باللغة الإنجليزية
A C++ variable is a named box in memory, and its type is fixed when you declare it. Follow six short lines, then one bad one, as each declaration makes a box exactly as big as its type (int 4 bytes, double 8, char and bool 1), values move in and change, 7 / 2 turns out to be 3, and the compiler refuses to put text into an int before the program can run.
6 أسئلة سريعة
اختر إجابة لتعرف السبب. تُحفَظ نتيجتك مع تقدّمك.
01What does int age = 30; do?
A declaration names the type first (int, a whole number), then the variable's name (age), then its starting value (30). The = here stores a value; it does not compare.
02After int half = 7 / 2; what value is in half?
7 and 2 are both ints, so / does integer division and the answer is an int: 3. The fractional part is dropped, not rounded. 7.0 / 2 would give the double 3.5.
03On a typical 64-bit desktop, how many bytes do an int and a double take?
sizeof(int) is 4 (32 bits) and sizeof(double) is 8 on today's common desktops and phones. The standard only guarantees that sizeof(char) is 1; the other sizes can differ between systems.
04age is an int. What happens when you build a program containing age = "hi";?
C++ is statically typed: a variable keeps its type for life, and the compiler checks every line against the types before anything runs. Text does not fit in an int box, so the build stops with an error.
05After double p = 2.5; int n = p; what value is in n?
An int holds only whole numbers, so storing 2.5 in it cuts off the .5. The fraction is dropped, not rounded, so n is 2.
06Which type would you use to store whether a door is open?
A bool holds only true or false, the answer to a yes-or-no question. It usually takes 1 byte.
المسرد
- Variable
- A named box in memory that holds one value at a time. In C++ every variable has a fixed type.
- Type
- What kind of value a variable holds, such as int or double. It decides the variable's size, which values fit and what operators do.
- Declaration
- The line that creates a variable: the type, then the name, often with a starting value, as in int age = 30;.
- Assignment
- Storing a new value in an existing variable with =, as in age = age + 1;. The old value is replaced.
- Byte
- The unit of memory size: 8 bits, each a 0 or a 1. sizeof tells you how many bytes a type uses.
- int / double
- int holds whole numbers (usually 4 bytes); double holds numbers with a decimal point (usually 8 bytes, about 15 significant digits).
- char / bool
- char holds one character in single quotes, such as 'A' (always 1 byte); bool holds true or false (usually 1 byte).
- Statically typed
- Every variable's type is known before the program runs, and the compiler checks every line against those types.
الملاحظات والمصادر
- The card shows the body of main. The full program adds #include <iostream>, int main() { ... } and a line that prints the values; it builds with g++ -std=c++20 -Wall -Wextra and prints 31 2.5 A 1 3 (std::cout prints the bool true as 1).
- Sizes are those of today's common desktops and phones (64-bit Windows, Linux and macOS on x86-64 or ARM). The standard fixes only sizeof(char) at 1 and sets minimums: int has at least 16 bits. Check your own system with sizeof.
- The drawing lines the boxes up in a row. Where each variable really lives in memory is up to the compiler, which may reorder them or keep some only in CPU registers.
- A double is usually an IEEE-754 64-bit number: about 15 to 17 significant decimal digits, so some decimals, such as 0.1, are stored as a very close approximation.
- Always give a variable a starting value. A local variable declared without one, such as int x;, holds an unpredictable value, and reading it is a bug.
- Exact error messages differ between compilers, but every C++ compiler rejects storing a string literal in an int.
- cppreference.com: Declarations
- cppreference.com: Initialization
- cppreference.com: Fundamental types
- cppreference.com: sizeof operator
- cppreference.com: Arithmetic operators (integer division)
- cppreference.com: Implicit conversions (floating-integral)
- cppreference.com: Numeric limits of integer types (climits)
- Standard C++ Foundation (isocpp.org): Get Started!
- Standard C++ Foundation (isocpp.org): Newbie Questions & Answers FAQ
اختبار يتاح بعد الخطوة الأخيرة.