PROJECTENGRAM

VARIABLE, OPERATORS, EXPRESSIONS, AND THE ASSIGNMENT STATEMENT

Tags: #◇Computer Science #◇CCPROG1 #◇Code Snippets #◇Variables #◇Operators #◇C #◇M3


◇Overview

At the heart of imperative programming lies the ability to store, manipulate, and retrieve data. Variables serve as named memory locations for data storage, while operators are the tools used to manipulate this data. By combining variables, constants, and operators, programmers form expressions that evaluate to specific values. These computed values are then typically saved back into memory via assignment statements.

◇Key Definitions

  • Variable: A designated storage location in memory, identified by a symbolic name, that holds a value capable of changing during program execution.
  • Data Type: A classification that specifies the size and type of values a variable can hold (e.g., int for integers, float for floating-point numbers, char for single characters).
  • Operator: A special symbol that directs the compiler to perform specific mathematical, relational, or logical operations.
  • Expression: A syntactically valid combination of operands (variables and constants) and operators that reduces to a single value.

◇Theoretical Framework / Core Concepts

Operator Precedence in C

When an expression involves multiple operators, the compiler evaluates them according to strict precedence rules, ensuring predictable outcomes.

  1. Highest Precedence: ( ) Parentheses always override standard precedence.
  2. Multiplicative: * (Multiplication), / (Division), % (Modulo) are evaluated next, from left to right.
  3. Additive: + (Addition), - (Subtraction) are evaluated last, from left to right.

The Modulo Operator

The % operator is unique; it performs division but returns only the remainder. It is strictly for integer operands. For example, 10 % 3 evaluates to 1.

Integer Division Caveat

In C, if division is performed between two integer types, the result is an integer division. The fractional part is truncated entirely. For instance, 5 / 2 results in 2, not 2.5.

◇Worked Examples / Protocol Walkthroughs / Calculations

『』
C
#include <stdio.h>

int main() {
    // 1. Variable Declaration and Initialization
    int a = 15;
    int b = 4;
    
    // 2. Expressions and Assignment
    int sum = a + b;           // Evaluates to 19
    int division = a / b;      // Evaluates to 3 (Integer division truncates)
    int remainder = a % b;     // Evaluates to 3 (Since 15 = 4*3 + 3)
    
    // 3. Precedence Example
    int complex_expr = a + b * 2; // Evaluates to 15 + 8 = 23 (Multiplication first)
    
    printf("Division: %d\n", division);
    printf("Remainder: %d\n", remainder);
    
    return 0;
}

◇Summary & Key Takeaways

  • Variables require a data type that dictates what kind of information they can store.
  • Expressions evaluate to a single value based on operator precedence rules.
  • The assignment operator = evaluates the right side and stores it in the left side's variable location.
  • Integer division truncates decimals, which is a common source of logic errors.

◇References & Navigation

CONFIRMATION // 理解度の確認

ACTIVE RECALL VERIFICATION

Select an answer to verify conceptual comprehension and reveal the technical rationale.

Q1

What is a variable in programming?

Q2

Which data type is used to store whole numbers in C?

Q3

What does the modulo operator (%) return?

Q4

According to operator precedence in C, which of the following is evaluated first?

Q5

What is the result of the expression 11 / 4 in C (assuming integer operands)?

Q6

What will be the value of x after the following code executes: int x = 5 + 3 * 2;?

Q7

What is an assignment statement?

Q8

What happens if you use a variable before initializing it in C?

Q9

In the statement total = a + b;, what does the = symbol do?

Q10

Which of the following is a common pitfall regarding data types?