Introduction to Variables¶
āĻā§āϝāĻžāϰāĻŋā§ā§āĻŦāϞā§āϰ āĻŦā§āϏāĻŋāĻ āĻĢāĻžāύā§āĻĄāĻž (Concept of Variables)¶
āĻāĻāĻāĻž āĻā§āϝāĻžāϰāĻŋā§ā§āĻŦāϞ (Variable)-āĻā§ āĻāĻĒāύāĻŋ āĻā§āĻŦ āϏāĻšāĻā§āĻ āĻāĻ āĻā§āϞāĻžāϏ āĻĒāĻžāύāĻŋāϰ āĻ
ā§āϝāĻžāύāĻžāϞāĻāĻŋ (Analogy) āĻĻāĻŋā§ā§ āĻŦā§āĻāϤ⧠āĻĒāĻžāϰā§āĨ¤ āĻāĻŽāϰāĻž āϝā§āĻŽāύ āĻĒāĻžāύāĻŋ āĻāĻžāĻā§āĻžāϰ āĻāύā§āϝ āĻā§āϞāĻžāϏā§āϰ āĻā§āϤāϰ āĻĒāĻžāύāĻŋ āϰāĻžāĻāĻŋ, āĻ āĻŋāĻ āϤā§āĻŽāύāĻŋ āĻāĻāĻāĻž āĻā§āϝāĻžāϰāĻŋā§ā§āĻŦāϞ (Variable) āĻĒā§āϰā§āĻā§āϰāĻžāĻŽā§āϰ āĻā§āϤāϰ āĻā§āύ⧠āĻĄā§āĻāĻž āĻŦāĻž āĻŽāĻžāύ (Value) āϏā§āĻā§āϰ āĻāϰ⧠āϰāĻžāĻāĻžāϰ āĻāύā§āϝ āĻāĻžāĻ āĻāϰā§āĨ¤ āĻā§āϝāĻžāϰāĻŋā§ā§āĻŦāϞ⧠āĻāĻāĻŦāĻžāϰ āĻŽāĻžāύ āϏā§āĻā§āϰ āĻāϰāĻžāϰ āĻĒāϰ āĻāĻĒāύāĻŋ āĻāĻĒāύāĻžāϰ āĻĒā§āϰ⧠āĻĒā§āϰā§āĻā§āϰāĻžāĻŽā§ āĻāĻ āĻŽāĻžāύ āϝā§āĻāĻžāύ⧠āĻāĻā§āĻāĻž āĻĢā§āϰāĻŋāϞāĻŋ āĻāĻāĻ āĻāϰāϤ⧠āĻĒāĻžāϰāĻŦā§āĨ¤


āĻŦāĻžāϏā§āϤāĻŦā§ āĻā§āϝāĻžāϰāĻŋā§ā§āĻŦāϞ āĻšāϞ⧠āϏāĻŋāĻŽā§āĻĒāϞ āĻāĻŋāĻā§ āύāĻžāĻŽ (Names) āϝāĻž āĻŽā§āĻŽāϰāĻŋāϰ āĻā§āύ⧠āĻāĻāĻāĻž āύāĻŋāϰā§āĻĻāĻŋāώā§āĻ āϞā§āĻā§āĻļāύ āĻŦāĻž āĻŽā§āĻŽā§āϰāĻŋ āϞā§āĻā§āĻļāύ (Memory location)-āĻā§ āĻĒā§ā§āύā§āĻ āĻāϰā§āĨ¤ āϏāĻŋ āϞā§āϝāĻžāĻā§āĻā§ā§ā§āĻā§āϰ āĻāύāĻā§āύā§āĻāϰ āĻŦāĻž āĻāĻŦāĻŋāώā§āĻāĻžāϰāĻāĻā§ āĻāĻāĻāĻž āĻĨā§āϝāĻžāĻāĻāϏ āĻĻā§āĻā§āĻžāĻ āϞāĻžāĻāĻŦā§, āĻāĻžāϰāĻŖ āĻāύāĻŋ āĻāĻŽāĻžāĻĻā§āϰ āĻāύā§āϝ āĻŽā§āĻŽāϰāĻŋāϤ⧠āĻŽāĻžāύ āϏā§āĻ āĻāϰāĻžāϰ āĻāĻžāĻāĻāĻž āĻāĻžāϏā§āĻ āύāĻŋāĻā§āĻĻā§āϰ āĻĒāĻāύā§āĻĻā§āϰ āύāĻžāĻŽ āĻāĻāĻ āĻāϰā§āĻ āĻĒāĻžāύāĻŋāϰ āĻŽāϤ⧠āϏā§āĻāĻž āĻŦāĻžāύāĻŋā§ā§ āĻĻāĻŋā§ā§āĻā§āύāĨ¤ āĻāĻĒāύāĻžāϰ āĻā§āϝāĻžāϞ⧠āĻŽā§āĻŽāϰāĻŋāϰ āĻ āĻŋāĻ āĻā§āύ āĻ āĻŋāĻāĻžāύāĻžā§ āĻŦāĻž āĻāĻĄā§āϰā§āϏ⧠āϏā§āĻā§āϰ āĻšāĻā§āĻā§ āϤāĻž āύāĻŋā§ā§ āĻāĻĒāύāĻžāϰ āĻŦāĻŋāύā§āĻĻā§āĻŽāĻžāϤā§āϰ āĻĒā§āϝāĻžāϰāĻž āύā§āĻā§āĻžāϰ āĻĻāϰāĻāĻžāϰ āύāĻžāĻāĨ¤ āĻāĻĒāύāĻžāϰ āĻāĻžāĻ āĻšāϞ⧠āĻāĻžāϏā§āĻ āĻāĻāĻāĻž āϏā§āύā§āĻĻāϰ āύāĻžāĻŽ āĻĻā§āĻā§āĻž, āĻāϰ āĻŦā§āϝāĻžāĻāĻā§āϰāĻžāĻāύā§āĻĄā§ sistema āύāĻŋāĻā§ āĻĨā§āĻā§āĻ āĻāĻā§ āĻŽā§āĻŽā§āϰāĻŋ āϞā§āĻā§āĻļāύā§āϰ āϏāĻžāĻĨā§ āϞāĻŋāĻāĻ āĻāϰ⧠āύā§āĻŦā§āĨ¤
āĻā§āϝāĻžāϰāĻŋā§ā§āĻŦāϞ āĻĄāĻŋāĻā§āϞā§ā§āĻžāϰā§āĻļāύ āĻŦāύāĻžāĻŽ āĻĄā§āĻĢāĻŋāύāĻŋāĻļāύ (Variable Declaration versus Definition)¶
āĻĒāϰā§āĻā§āώāĻžāϰ āĻāύā§āϝ āĻā§āϝāĻžāϰāĻŋā§ā§āĻŦāϞā§āϰ āĻĻā§āĻāĻāĻž āĻā§āϰā§āĻļāĻŋā§āĻžāϞ āĻāĻžāϰā§āĻŽ āĻāĻāĻĻāĻŽ āĻā§āϰāĻŋāϏā§āĻāĻžāϞ āĻā§āϞāĻŋā§āĻžāϰ āϰāĻžāĻāĻž āϞāĻžāĻāĻŦā§:
- āĻā§āϝāĻžāϰāĻŋā§ā§āĻŦāϞ āĻĄāĻŋāĻā§āϞā§ā§āĻžāϰā§āĻļāύ (Declaration of a Variable): āĻĒā§āϰā§āĻā§āϰāĻžāĻŽā§ āĻā§āύ⧠āĻā§āϝāĻžāϰāĻŋā§ā§āĻŦāϞ āĻāĻāĻ āĻāϰāĻžāϰ āĻāĻā§ āϤāĻžāĻā§ āĻ āĻŦāĻļā§āϝāĻ āĻĄāĻŋāĻā§āϞā§ā§āĻžāϰ (Declare) āĻāϰāϤ⧠āĻšāĻŦā§āĨ¤ āĻĄāĻŋāĻā§āϞā§ā§āĻžāϰā§āĻļāύ āĻŽāĻžāύ⧠āĻšāϞ⧠āĻāĻŽā§āĻĒāĻžāĻāϞāĻžāϰ (Compiler)-āĻāϰ āĻāĻžāĻā§ āĻāĻ āĻā§āϝāĻžāϰāĻŋā§ā§āĻŦāϞā§āϰ āϏāĻŦ āĻĒā§āϰāĻĒāĻžāϰā§āĻāĻŋāĻ āĻŦāĻž āĻŦā§āĻļāĻŋāώā§āĻā§āϝ āĻ ā§āϝāĻžāύāĻžāĻāύā§āϏ (Announce) āĻāϰāĻž āĻŦāĻž āĻā§āώāĻŖāĻž āĻĻā§āĻā§āĻžāĨ¤ āĻĒā§āϰāĻĒāĻžāϰā§āĻāĻŋāĻ āĻŦāϞāϤ⧠āĻāĻāĻžāύ⧠āĻā§āϝāĻžāϰāĻŋā§ā§āĻŦāϞā§āϰ āύāĻžāĻŽ (Name) āĻā§ āĻšāĻŦā§ āĻāϰ āϏ⧠āĻŽā§āĻŽāϰāĻŋāϤ⧠āĻāϤāĻā§āĻā§ āϏāĻžāĻāĻ (Size) āύā§āĻŦā§ āϤāĻž āĻŦā§āĻāĻžā§āĨ¤
- āĻā§āϝāĻžāϰāĻŋā§ā§āĻŦāϞ āĻĄā§āĻĢāĻŋāύāĻŋāĻļāύ (Definition of a Variable): āĻĄā§āĻĢāĻŋāύāĻŋāĻļāύ āĻŽāĻžāύ⧠āĻšāϞ⧠āĻā§āϝāĻžāϰāĻŋā§ā§āĻŦāϞā§āϰ āĻāύā§āϝ āĻŽā§āĻŽāϰāĻŋāϤ⧠āĻāĻāĻā§ā§āĻžāϞ āĻāĻžā§āĻāĻž āĻŦāĻž āĻŽā§āĻŽā§āϰāĻŋ āϏā§āĻĒā§āϏ āĻ ā§āϝāĻžāϞā§āĻā§āĻ (Allocating memory) āĻāϰāĻžāĨ¤
āĻŦā§āĻļāĻŋāϰāĻāĻžāĻ āϏāĻŽā§ āϏāĻŋ āϞā§āϝāĻžāĻā§āĻā§ā§ā§āĻā§ āĻĄāĻŋāĻā§āϞā§ā§āĻžāϰā§āĻļāύ āĻāϰ āĻĄā§āĻĢāĻŋāύāĻŋāĻļāύ āĻāĻāĻ āϏāĻžāĻĨā§ āĻāϰāĻž āĻšā§ā§ āϝāĻžā§āĨ¤ āϤāĻŦā§ āϏāĻŦ āĻā§āώā§āϤā§āϰ⧠āĻāĻŋāύā§āϤ⧠āĻāĻŽāύāĻāĻž āĻšā§ āύāĻžāĨ¤ āĻā§āϝāĻžāϰāĻŋā§ā§āĻŦāϞā§āϰ āϏāĻžāĻĨā§ āĻāĻĒāύāĻŋ āĻā§ āϧāϰāύā§āϰ āĻŽāĻĄāĻŋāĻĢāĻžā§āĻžāϰ (Modifiers) āĻāĻāĻ āĻāϰāϤā§āĻ āϤāĻžāϰ āĻāĻĒāϰ āĻāĻāĻž āĻĄāĻŋāĻĒā§āύā§āĻĄ āĻāϰā§āĨ¤

āϏāĻŋāύāĻā§āϝāĻžāĻā§āϏ āĻāĻŦāĻ āĻŽā§āĻŽā§āϰāĻŋ āĻ ā§āϝāĻžāϞā§āĻā§āĻļāύ (Syntax and Memory Allocation)¶
āĻāĻāĻāĻž āĻā§āϝāĻžāϰāĻŋā§ā§āĻŦāϞ āĻĄāĻŋāĻā§āϞā§ā§āĻžāϰ āĻāϰāĻžāϰ āĻŦā§āϏāĻŋāĻ āϏāĻŋāύāĻā§āϝāĻžāĻā§āϏ (Syntax) āĻšāϞ⧠āĻĒā§āϰāĻĨāĻŽā§ āĻĄā§āĻāĻž āĻāĻžāĻāĻĒ (Data type) āϞāĻŋāĻāϤ⧠āĻšāĻŦā§ āĻāĻŦāĻ āϤāĻžāϰāĻĒāϰ āĻā§āϝāĻžāϰāĻŋā§ā§āĻŦāϞā§āϰ āύāĻžāĻŽ āĻĻāĻŋāϤ⧠āĻšāĻŦā§āĨ¤ āϝā§āĻŽāύ:
āĻā§āϝāĻžāϰāĻŋā§ā§āĻŦāϞā§āϰ āύāĻžāĻŽ āĻšāϞ⧠var āĻāϰ āĻāĻāĻžāϰ āĻĄā§āĻāĻž āĻāĻžāĻāĻĒ āĻšāϞ⧠int āĻŦāĻž āĻāύā§āĻāĻŋāĻāĻžāϰ (Integer)āĨ¤ āĻĄā§āĻāĻž āĻāĻžāĻāĻĒā§āϰ āĻŽā§āĻāύ āĻāĻžāĻāĻ āĻšāϞ⧠āĻāĻŽā§āĻĒāĻžāĻāϞāĻžāϰāĻā§ āĻŦāϞ⧠āĻĻā§āĻā§āĻž āϝ⧠āĻāĻ āĻā§āϝāĻžāϰāĻŋā§ā§āĻŦāϞāĻāĻž āĻŽā§āĻŽāϰāĻŋāϤ⧠āĻ āĻŋāĻ āĻāϤāĻāĻž āĻāĻžā§āĻāĻž āĻĻāĻāϞ āĻāϰāϤ⧠āϝāĻžāĻā§āĻā§āĨ¤
āĻā§āĻĄā§ int var; āϞā§āĻāĻžāϰ āĻŽāĻžāύā§āĻ āĻšāϞ⧠āĻāĻĒāύāĻŋ āĻā§āϝāĻžāϰāĻŋā§ā§āĻŦāϞāĻāĻž āĻĄāĻŋāĻā§āϞā§ā§āĻžāϰ āĻāϰāĻžāϰ āĻĒāĻžāĻļāĻžāĻĒāĻžāĻļāĻŋ āĻāĻŽā§āĻĒāĻžāĻāϞāĻžāϰāĻā§ āϰāĻŋāĻā§ā§ā§āϏā§āĻ āĻāϰāϤā§āĻ āĻāϰ āĻāύā§āϝ āĻŽā§āĻŽā§āϰāĻŋ āĻ
ā§āϝāĻžāϞā§āĻā§āĻ (Allocate memory) āĻāϰāϤā§āĨ¤ āĻāĻ āϏā§āĻā§āĻāĻŽā§āύā§āĻā§āϰ āĻļā§āώ⧠āĻāĻāĻāĻž āϏā§āĻŽāĻŋāĻā§āϞāύ ; āĻŦāϏāĻžāύ⧠āĻāĻāĻĻāĻŽ āĻŽā§āϝāĻžāύā§āĻĄā§āĻāϰāĻŋ āĻŦāĻž āĻŦāĻžāϧā§āϝāϤāĻžāĻŽā§āϞāĻāĨ¤ āĻāĻžāϰāĻŖ āĻāĻŽā§āĻĒāĻžāĻāϞāĻžāϰ āĻāĻ āϏā§āĻŽāĻŋāĻā§āϞāύ āĻĻā§āĻā§āĻ āĻāĻāĻāĻž āϏā§āĻā§āĻāĻŽā§āύā§āĻ āĻĨā§āĻā§ āĻāϰā§āĻāĻāĻž āϏā§āĻā§āĻāĻŽā§āύā§āĻ āĻāϞāĻžāĻĻāĻž āĻāϰā§āĨ¤ āϤāĻžāĻ āϏā§āĻŽāĻŋāĻā§āϞāύ āĻĻāĻŋāϤ⧠āĻā§āϞ⧠āĻā§āϞ⧠āĻā§āĻĄā§ āĻāϰāϰ (Compilation error) āĻāĻžāĻŦā§āĨ¤

āĻā§āϝāĻžāϰāĻŋā§ā§āĻŦāϞ āĻŽā§āĻŽāϰāĻŋāϤ⧠⧍ āĻŦāĻžāĻāĻ (2 bytes) āĻāĻžā§āĻāĻž āύā§āĻŦā§ āύāĻžāĻāĻŋ ā§Ē āĻŦāĻžāĻāĻ (4 bytes) āĻāĻžā§āĻāĻž āύā§āĻŦā§ āϤāĻž āĻĒā§āϰā§āĻĒā§āϰāĻŋ āĻĄāĻŋāĻĒā§āύā§āĻĄ āĻāϰ⧠āĻāĻĒāύāĻŋ āĻā§āύ āϏāĻŋāϏā§āĻā§āĻŽ āĻŦāĻž āĻāĻāϏ (System architecture)-āĻ āĻāĻžāĻ āĻāϰāϤā§āĻ āϤāĻžāϰ āĻāĻĒāϰāĨ¤ āĻā§āĻĄāĻžāϰ āĻšāĻŋāϏā§āĻŦā§ āĻāĻĒāύāĻžāϰ āĻāĻžāϏā§āĻ āĻĄāĻŋāĻā§āϞā§ā§āĻžāϰā§āĻļāύ āĻāϰ āĻĄā§āĻĢāĻŋāύāĻŋāĻļāύ⧠āĻĢā§āĻāĻžāϏ āĻāϰāϞā§āĻ āĻāϞāĻŦā§, āĻŽā§āĻŽāϰāĻŋāϰ āĻā§āϤāϰ⧠āĻāϤāĻā§āĻā§ āϏā§āĻĒā§āϏ āĻ
ā§āϝāĻžāϞā§āĻā§āĻļāύ āĻšāĻā§āĻā§ āϤāĻž āύāĻŋā§ā§ āĻŽāĻžāĻĨāĻž āĻāĻžāĻŽāĻžāύā§āϰ āĻā§āύ⧠āĻĻāϰāĻāĻžāϰ āύāĻžāĻāĨ¤
āĻā§āϝāĻžāϰāĻŋā§ā§āĻŦāϞ āĻāύāĻŋāĻļāĻŋā§āĻžāϞāĻžāĻāĻā§āĻļāύ āĻāĻŦāĻ āϰāĻŋ-āĻ ā§āϝāĻžāϏāĻžāĻāύāĻŽā§āύā§āĻ (Variable Initialization and Re-assignment)¶
āĻā§āϝāĻžāϰāĻŋā§ā§āĻŦāϞ āĻĄāĻŋāĻā§āϞā§ā§āĻžāϰ āĻāϰāĻžāϰ āϏāĻŽā§āĻ āϝāĻĻāĻŋ āĻāĻĒāύāĻŋ āϤāĻžāϤ⧠āĻā§āύ⧠āĻŽāĻžāύ āĻŦāĻž āĻā§āϝāĻžāϞ⧠āĻ ā§āϝāĻžāϏāĻžāĻāύ (Assign) āĻāϰ⧠āĻĻāĻžāĻ, āϤāĻŦā§ āϏā§āĻ āĻĒā§āϰāϏā§āϏāĻā§ āĻāύāĻŋāĻļāĻŋā§āĻžāϞāĻžāĻāĻā§āĻļāύ (Initialization) āĻŦāϞā§āĨ¤ āϝā§āĻŽāύ:
āĻāύāĻŋāĻļāĻŋā§āĻžāϞāĻžāĻāĻā§āĻļāύ āĻāϰāĻžāϰ āĻŽāĻžāύ⧠āĻāĻ āύāĻž āϝ⧠āĻāĻ āĻā§āϝāĻžāϞ⧠āϏāĻžāϰāĻž āĻā§āĻŦāύā§āϰ āĻāύā§āϝ āĻĢāĻŋāĻā§āϏāĻĄ āĻšā§ā§ āĻā§āϞāĨ¤ āĻāĻĒāύāĻŋ āĻāĻžāĻāϞ⧠āĻā§āĻĄā§āϰ āϝā§āĻā§āύ⧠āĻāĻžā§āĻāĻžā§ āĻāĻ āĻŽāĻžāύ āϝāĻāύ āĻā§āĻļāĻŋ āĻā§āĻā§āĻ āĻāϰāϤ⧠āĻĒāĻžāϰā§āĨ¤ āĻāĻ āĻāĻžāϰāĻŖā§āĻ āĻāĻ āĻāĻŋāύāĻŋāϏā§āϰ āύāĻžāĻŽ āĻĻā§āĻā§āĻž āĻšā§ā§āĻā§ āĻā§āϝāĻžāϰāĻŋā§ā§āĻŦāϞ, āϝāĻž āĻāϏā§āĻā§ "Vary" āĻļāĻŦā§āĻĻ āĻĨā§āĻā§, āϝāĻžāϰ āĻŽāĻžāύ⧠āĻšāϞ⧠āϝāĻž āϏāĻŽā§ā§āϰ āϏāĻžāĻĨā§ āϏāĻžāĻĨā§ āĻĒāϰāĻŋāĻŦāϰā§āϤāĻŋāϤ āĻŦāĻž āĻā§āĻāĻā§āĻ āĻšāϤ⧠āĻĒāĻžāϰā§āĨ¤ āĻāĻāĻž āĻšāϞ⧠āĻāύāϏā§āĻā§āϝāĻžāύā§āĻ (Constant)-āĻāϰ āĻāĻāĻĻāĻŽ āĻ āĻĒāĻāĻŋāĻ āĻŦāĻž āĻāϞā§āĻā§ āĻāĻŋāύāĻŋāϏ, āϝāĻž āĻāĻāĻŦāĻžāϰ āĻĄāĻŋāĻĢāĻžāĻāύ āĻāϰāϞ⧠āĻāϰ āϏāĻāϞā§āĻ āĻā§āĻāĻā§āĻ āĻāϰāĻž āϝāĻžā§ āύāĻžāĨ¤
āύāĻŋāĻā§āϰ āĻā§āĻĄā§āϰ āĻā§āĻžāϰā§āĻāĻĢā§āϞā§āĻāĻž āĻā§ā§āĻžāϞ āĻāϰā§:
#include <stdio.h>
int main() {
int var = 3; // āĻāύāĻŋāĻļāĻŋā§āĻžāϞāĻžāĻāĻā§āĻļāύ (Initialization)
var = 4; // āϰāĻŋ-āĻ
ā§āϝāĻžāϏāĻžāĻāύāĻŽā§āύā§āĻ (Re-assignment)
printf("%d", var);
return 0;
}

āĻāĻ āĻā§āĻĄāĻāĻž āϝāĻāύ āĻāĻŽāϰāĻž āĻŦāĻŋāϞā§āĻĄ āĻ ā§āϝāĻžāύā§āĻĄ āϰāĻžāύ (Build and Run) āĻāϰāĻŦ, āϤāĻāύ āĻāĻŽāĻžāĻĻā§āϰ āĻŦā§āϞā§āϝāĻžāĻ āĻāύāϏā§āϞ āĻāĻāύā§āĻĄā§ (Console window)-āϤ⧠āĻāĻāĻāĻĒā§āĻ āĻāϏāĻŦā§ 4āĨ¤ āĻ āϰā§āĻĨāĻžā§ āĻāĻā§āϰ ā§Š āĻŽāĻžāύāĻāĻž āϏāĻžāĻāϏā§āϏāĻĢā§āϞāĻŋ āĻāĻĒāĻĄā§āĻ āĻšā§ā§ ā§Ē āĻšā§ā§ āĻā§āĻā§āĨ¤
āĻāĻŋāĻā§ āĻā§āϰāĻŋāĻāĻŋāĻā§āϝāĻžāϞ āϰā§āϞāϏ āĻāĻŦāĻ āϰā§āϏā§āĻā§āϰāĻŋāĻāĻļāύ (Critical Rules and Constraints)¶
- āĻāϞāϞāĻŋāĻāĻžāϞ āϰāĻŋ-āĻĄā§āĻĢāĻŋāύāĻŋāĻļāύ (Illegal Redefinition): āĻā§āĻĄā§ āϝāĻāύ āĻŽāĻžāύ āĻāĻĒāĻĄā§āĻ āĻāϰāĻžāϰ āĻāύā§āϝ
var = 4;āϞā§āĻāĻž āĻšā§ā§āĻā§, āϤāĻāύ āĻāĻŋāύā§āϤ⧠āϏāĻžāĻŽāύ⧠āĻāĻŦāĻžāϰ āύāϤā§āύ āĻāϰā§intāĻĄā§āĻāĻž āĻāĻžāĻāĻĒ āϞā§āĻāĻž āĻšā§ āύāĻžāĻāĨ¤ āĻŽā§āĻŽā§āϰāĻŋ āĻ ā§āϝāĻžāϞā§āĻā§āĻļāύ āĻā§āϝāĻžāϰāĻŋā§ā§āĻŦāϞā§āϰ āĻāύā§āϝ āĻāĻžāϏā§āĻ āĻāĻāĻŦāĻžāϰāĻ āĻšā§āĨ¤ āĻāĻāĻ āĻŦā§āϞāĻā§āϰ āĻā§āϤāϰ āϝāĻĻāĻŋ āĻāĻĒāύāĻŋ āĻāĻŦāĻžāϰint var = 4;āϞā§āĻā§, āϤāĻžāϰ āĻŽāĻžāύ⧠āĻāĻĒāύāĻŋ āĻāĻāĻ āύāĻžāĻŽā§āϰ āĻāύā§āϝ āĻā§āĻžāύ āĻŽā§āϰ āĻāĻžāĻāĻŽ āĻŽā§āĻŽā§āϰāĻŋ āϏā§āĻĒā§āϏ āĻāĻžāĻā§āĻ, āϝāĻž āϏāĻŋ āϞā§āϝāĻžāĻā§āĻā§ā§ā§āĻā§ āĻāĻāĻĻāĻŽ āĻāϞāϞāĻŋāĻāĻžāϞ āĻŦāĻž āĻ āĻŦā§āϧ (Illegal)āĨ¤ - āĻāĻāĻ āĻĄā§āĻĢāĻŋāύāĻŋāĻļāύ āϰā§āϞ (Single Definition Rule): āĻĒā§āϰāϤāĻŋāĻāĻž āĻā§āϝāĻžāϰāĻŋā§ā§āĻŦāϞ āϤāĻžāϰ āύāĻŋāϰā§āĻĻāĻŋāώā§āĻ āϏā§āĻā§āĻĒ āĻŦāĻž āĻŦā§āϞāĻā§āϰ āĻā§āϤāϰ āĻāĻžāϏā§āĻ āĻāĻāĻŦāĻžāϰāĻ āĻĄāĻŋāĻĢāĻžāĻāύ āĻāϰāĻž āϝāĻžāĻŦā§, āϤāĻŦā§ āϤāĻžāĻā§ āĻĄāĻŋāĻĢāϰā§āύā§āĻ āĻ
ā§āϝāĻžāϏāĻžāĻāύāĻŽā§āύā§āĻ (Different assignment) āĻĻāĻŋā§ā§ āĻŽāĻžāϞā§āĻāĻŋāĻĒāϞ āĻāĻžāĻāĻŽāϏ āĻāĻāĻ āĻāϰāĻž āϝāĻžāĻŦā§āĨ¤ āĻāĻ
main()āĻĢāĻžāĻāĻļāύ āĻŦā§āϞāĻā§āϰ āĻā§āϤāϰ āĻāĻĒāύāĻŋ āĻāĻāύā§āĻ āϏā§āĻŽ āύāĻžāĻŽā§ āĻāĻāĻžāϧāĻŋāĻ āĻā§āϝāĻžāϰāĻŋā§ā§āĻŦāϞ āĻĄāĻŋāĻĢāĻžāĻāύ āĻāϰāϤ⧠āĻĒāĻžāϰāĻŦā§ āύāĻžāĨ¤ - āĻĢāϰāĻŽā§āϝāĻžāĻ āϏā§āĻĒā§āϏāĻŋāĻĢāĻžā§āĻžāϰ (Format Specifier): āĻā§āĻĄā§āϰ
printf("%d", var);āĻ āĻāĻļā§ āĻĨāĻžāĻāĻž%dāĻšāϞ⧠āĻāĻāĻāĻž āĻĢāϰāĻŽā§āϝāĻžāĻ āϏā§āĻĒā§āϏāĻŋāĻĢāĻžā§āĻžāϰ (Format specifier)āĨ¤ āĻāĻāĻž āĻāύā§āĻāĻŋāĻāĻžāϰā§āϰ āĻāύā§āϝ āĻāĻāĻ āĻāϰāĻž āĻšā§ āĻāĻŦāĻ āĻāĻŽā§āĻĒāĻžāĻāϞāĻžāϰāĻā§ āύāĻŋāϰā§āĻĻā§āĻļ āĻĻā§ā§ āĻā§āϝāĻžāϰāĻŋā§ā§āĻŦāϞā§āϰ āĻā§āϤāϰā§āϰ āĻāĻžāϰā§āύā§āĻ āĻāύā§āĻā§āύā§āĻ āĻŦāĻž āĻŽāĻžāύ āϏā§āĻā§āϰāĻŋāύ⧠āĻĒā§āϰāĻŋāύā§āĻ āĻāϰāϤā§āĨ¤
āĻ ā§āϝāĻžāĻĄāĻāĻžāύā§āϏāĻĄ āĻ ā§āϝāĻžāϏāĻžāĻāύāĻŽā§āύā§āĻ āĻ āĻĒāĻžāϰā§āĻļāύāϏ (Advanced Assignment Operations)¶
- āĻā§āϝāĻžāϰāĻŋā§ā§āĻŦāϞ āĻā§ āĻā§āϝāĻžāϰāĻŋā§ā§āĻŦāϞ āĻ ā§āϝāĻžāϏāĻžāĻāύāĻŽā§āύā§āĻ (Assigning a Variable to another Variable): āĻā§āύ⧠āĻĄāĻŋāϰā§āĻā§āĻ āĻāύāϏā§āĻā§āϝāĻžāύā§āĻ āĻā§āϝāĻžāϞ⧠āĻ ā§āϝāĻžāϏāĻžāĻāύ āύāĻž āĻāϰ⧠āĻāĻĒāύāĻŋ āĻāĻžāĻāϞ⧠āĻāĻāĻāĻž āĻā§āϝāĻžāϰāĻŋā§ā§āĻŦāϞā§āϰ āĻŽāĻžāύ āĻāϰā§āĻāĻāĻž āĻā§āϝāĻžāϰāĻŋā§ā§āĻŦāϞā§āϰ āĻā§āϤāϰ āĻ ā§āϝāĻžāϏāĻžāĻāύ āĻāϰāϤ⧠āĻĒāĻžāϰā§āĨ¤ āϝā§āĻŽāύ:
āĻāĻāĻžāύ⧠āĻĄāĻžāύ āĻĒāĻžāĻļā§ āĻā§āϝāĻžāϰāĻŋā§ā§āĻŦāϞā§āϰ āύāĻžāĻŽ var1 āϞā§āĻāĻžāϰ āĻŽāĻžāύā§āĻ āĻšāϞ⧠āĻāĻĒāύāĻŋ āϤāĻžāϰ āĻā§āϤāϰā§āϰ āĻāύāϏā§āĻā§āϝāĻžāύā§āĻ āĻŽāĻžāύ (āϝāĻž āĻšāĻ˛ā§ ā§Š) āĻā§āύ⧠āĻāύ⧠var2 āĻāϰ āĻā§āϤāϰ āĻ
ā§āϝāĻžāϏāĻžāĻāύ āĻāϰāϤā§āĻāĨ¤ āĻāĻāύ var2 āĻĒā§āϰāĻŋāύā§āĻ āĻāϰāϞ⧠āĻāĻāĻāĻĒā§āĻ ā§Š āĻ āĻāϏāĻŦā§āĨ¤
- āĻāĻ āϞāĻžāĻāύ⧠āĻŽāĻžāϞā§āĻāĻŋāĻĒāϞ āĻā§āϝāĻžāϰāĻŋā§ā§āĻŦāϞ āĻ āĻĒāĻžāϰā§āĻļāύ (Single Line Multi Variable Operations): āĻāϞāĻžāĻĻāĻž āĻāϞāĻžāĻĻāĻž āϞāĻžāĻāύ⧠āĻā§āϝāĻžāϰāĻŋā§ā§āĻŦāϞ āĻĄāĻŋāĻā§āϞā§ā§āĻžāϰ āĻāϰ āĻĄāĻŋāĻĢāĻžāĻāύ āύāĻž āĻāϰ⧠āĻā§āĻĄā§āϰ āĻā§āϞāĻŋāύāύā§āϏ āĻŦāĻž āϰāĻŋāĻĄāĻžāĻŦāĻŋāϞāĻŋāĻāĻŋ āĻŦāĻžā§āĻžāύā§āϰ āĻāύā§āϝ āϏā§āĻŽ āĻĄā§āĻāĻž āĻāĻžāĻāĻĒā§āϰ āĻāĻāĻžāϧāĻŋāĻ āĻā§āϝāĻžāϰāĻŋā§ā§āĻŦāϞ āĻāĻžāϏā§āĻ āĻāĻ āϞāĻžāĻāύā§āĻ āĻšā§āϝāĻžāύā§āĻĄā§āϞ āĻāϰāĻž āϝāĻžā§āĨ¤ āϝā§āĻŽāύ:
āĻāĻāĻžāύ⧠āĻāĻāĻ āϞāĻžāĻāύ⧠āϏāĻŦ āĻā§āϝāĻžāϰāĻŋā§ā§āĻŦāϞāĻā§ āϏā§āĻŽ āĻā§āϝāĻžāϞ⧠āĻĻāĻŋā§ā§ āĻ
ā§āϝāĻžāϏāĻžāĻāύ āĻāϰāĻž āĻšā§ā§āĻā§āĨ¤ āĻāĻāύ āĻāĻĻā§āϰāĻā§ āϝāĻĻāĻŋ printf āĻĻāĻŋā§ā§ āϤāĻŋāύāĻŦāĻžāϰ āĻĒā§āϰāĻŋāύā§āĻ āĻāϰāĻž āĻšā§, āϤāĻŦā§ āĻāĻāĻāĻĒā§āĻ āĻāĻāύā§āĻĄā§āϤ⧠4 4 4 āĻĻā§āĻāϤ⧠āĻĒāĻžāĻŦā§, āĻāĻžāϰāĻŖ āϤāĻŋāύāĻāĻž āĻāϞāĻžāĻĻāĻž āĻā§āϝāĻžāϰāĻŋā§ā§āĻŦāϞāĻ āĻāĻāύ āĻāĻāĻ āĻŽāĻžāύ āĻšā§āϞā§āĻĄ āĻāϰāϤā§āĻā§āĨ¤
Concept of Variables¶
A variable can be understood through the simple analogy (āϏāĻžāĻĻā§āĻļā§āϝ) of a glass of water. Just as a glass is used to store water for drinking, a variable is used to store some specific value inside a program. Once the value is stored inside the variable, you are fully free to use this value anywhere throughout your program execution.
In reality, variables are simply user defined names that point to some specific memory location. The inventor of the C language made it highly convenient (āϏā§āĻŦāĻŋāϧāĻžāĻāύāĻ) for programmers by allowing us to store values in the memory by using any name of our choice. You do not need to worry about the exact location or the memory address where your value is being stored. Your only task is to provide a valid name, and the system internally points it to a memory location.
Variable Declaration versus Definition¶
There are two distinct concepts regarding variables that you must understand carefully for your examinations:
- Declaration of a Variable: You must always declare a variable before using it in your program. Declaration simply means announcing the properties (āĻŦā§āĻļāĻŋāώā§āĻā§āϝāϏāĻŽā§āĻš) of the variable to the compiler. By properties, we mean specifying what will be the name of the variable and what will be the size of the variable.
- Definition of a Variable: Definition means the actual allocation (āĻŦāϰāĻžāĻĻā§āĻĻāĻāϰāĻŖ) of memory space to that specific variable.
Most of the time, declaration and definition are done at the exact same time in C. However, this is not always the case because it depends heavily on the modifiers (āĻĒāϰāĻŋāĻŦāϰā§āϤāύāĻāĻžāϰā§) you mention with the variables.
Syntax and Memory Allocation¶
To declare a variable, the syntax requires specifying the data type followed by the variable name. For example:
Here, the name of the variable is var and its data type is int (integer). Data type simply tells the compiler how much space a variable is going to occupy in the memory.
By writing int var;, you are declaring the variable and simultaneously requesting the compiler to allocate memory for it. It is absolutely mandatory (āĻŦāĻžāϧā§āϝāϤāĻžāĻŽā§āϞāĻ) to put a semicolon ; at the end of this statement. The compiler uses the semicolon to separate one statement from another, so missing it will cause a compilation error.
The exact memory size allocated depends entirely on the data type and the underlying system architecture (āϏāĻŋāϏā§āĻā§āĻŽā§āϰ āĻāĻ āύ) you are working on. For instance, an integer data type may take either 2 bytes of memory or 4 bytes of memory, depending purely on the system. As a programmer, you only need to concentrate on the declaration and definition rather than worrying about the exact space allocation details.
Variable Initialization and Re-assignment¶
When you assign a value to a variable at the very time of its declaration itself, the process is called initialization (āĻĒā§āϰāĻžāϰāĻŽā§āĻāĻŋāĻā§āĻāϰāĻŖ). For example:
Initialization does not mean that the value of the variable becomes permanent or fixed. You can easily change this value later anywhere in your code. This is where the word variable gets its actual meaning, as it comes from the word "vary", meaning something that can change or vary over time. This is completely opposite to a constant (āϧā§āϰā§āĻŦāĻ), which never changes once defined.
Consider the following program workflow:
#include <stdio.h>
int main() {
int var = 3; // Initialization
var = 4; // Re-assignment
printf("%d", var);
return 0;
}
When we execute this code using the Build and Run process, the output displayed on the black console window will be 4. The initial value 3 is successfully updated to 4.
Critical Rules and Constraints¶
- Illegal Redefinition: Notice that during the re-assignment statement
var = 4;, the data typeintis not written again. Memory is allocated to a variable only once. Writingint var = 4;again in the same block means you are trying to allocate memory again for an already existing name, which is completely illegal (āĻ āĻŦā§āϧ) in C. - Single Definition Rule: Each variable must be defined only once within its scope (āĻāĻāϤāĻž), but it can be used multiple times with different assignments throughout the program. Within the same
main()function block, you cannot define multiple variables having the exact same name. - Format Specifier: In the statement
printf("%d", var);, the%dis a format specifier (āĻĢāϰāĻŽā§āϝāĻžāĻ āϏā§āĻĒā§āϏāĻŋāĻĢāĻžā§āĻžāϰ) used specifically for integers. It tells the compiler to print the current textual or numerical content stored inside the integer variable onto the screen.
Advanced Assignment Operations¶
- Assigning a Variable to another Variable: Instead of assigning a direct constant value like 3 or 4, you can assign the value of one variable to another variable. For example:
Writing the name of a variable on the right side means you are fetching its current constant value (which is 3) and assigning it to var2. Printing var2 will correctly yield 3 as the output.
- Single Line Multi Variable Operations: Instead of defining and declaring variables in separate individual lines, you can declare and define multiple variables of the same data type in a single line to improve code cleanliness. For example:
If you assign them the same value and print them three times using printf, the console window will output 4 4 4 as expected, because all three separate variables hold the identical value.