Skip to content

Introduction to Variables

āĻ­ā§āϝāĻžāϰāĻŋā§Ÿā§‡āĻŦāϞ⧇āϰ āĻŦ⧇āϏāĻŋāĻ• āĻĢāĻžāĻ¨ā§āĻĄāĻž (Concept of Variables)

āĻāĻ•āϟāĻž āĻ­ā§āϝāĻžāϰāĻŋā§Ÿā§‡āĻŦāϞ (Variable)-āϕ⧇ āφāĻĒāύāĻŋ āϖ⧁āĻŦ āϏāĻšāĻœā§‡āχ āĻāĻ• āĻ—ā§āϞāĻžāϏ āĻĒāĻžāύāĻŋāϰ āĻ…ā§āϝāĻžāύāĻžāϞāϜāĻŋ (Analogy) āĻĻāĻŋā§Ÿā§‡ āĻŦ⧁āĻāϤ⧇ āĻĒāĻžāϰ⧋āĨ¤ āφāĻŽāϰāĻž āϝ⧇āĻŽāύ āĻĒāĻžāύāĻŋ āĻ–āĻžāĻ“ā§ŸāĻžāϰ āϜāĻ¨ā§āϝ āĻ—ā§āϞāĻžāϏ⧇āϰ āϭ⧇āϤāϰ āĻĒāĻžāύāĻŋ āϰāĻžāĻ–āĻŋ, āĻ āĻŋāĻ• āϤ⧇āĻŽāύāĻŋ āĻāĻ•āϟāĻž āĻ­ā§āϝāĻžāϰāĻŋā§Ÿā§‡āĻŦāϞ (Variable) āĻĒā§āϰ⧋āĻ—ā§āϰāĻžāĻŽā§‡āϰ āϭ⧇āϤāϰ āϕ⧋āύ⧋ āĻĄā§‡āϟāĻž āĻŦāĻž āĻŽāĻžāύ (Value) āĻ¸ā§āĻŸā§‹āϰ āĻ•āϰ⧇ āϰāĻžāĻ–āĻžāϰ āϜāĻ¨ā§āϝ āĻ•āĻžāϜ āĻ•āϰ⧇āĨ¤ āĻ­ā§āϝāĻžāϰāĻŋā§Ÿā§‡āĻŦāϞ⧇ āĻāĻ•āĻŦāĻžāϰ āĻŽāĻžāύ āĻ¸ā§āĻŸā§‹āϰ āĻ•āϰāĻžāϰ āĻĒāϰ āφāĻĒāύāĻŋ āφāĻĒāύāĻžāϰ āĻĒ⧁āϰ⧋ āĻĒā§āϰ⧋āĻ—ā§āϰāĻžāĻŽā§‡ āĻ“āχ āĻŽāĻžāύ āϝ⧇āĻ–āĻžāύ⧇ āχāĻšā§āĻ›āĻž āĻĢā§āϰāĻŋāϞāĻŋ āχāωāϜ āĻ•āϰāϤ⧇ āĻĒāĻžāϰāĻŦ⧇āĨ¤
alt text

alt text
āĻŦāĻžāĻ¸ā§āϤāĻŦ⧇ āĻ­ā§āϝāĻžāϰāĻŋā§Ÿā§‡āĻŦāϞ āĻšāϞ⧋ āϏāĻŋāĻŽā§āĻĒāϞ āĻ•āĻŋāϛ⧁ āύāĻžāĻŽ (Names) āϝāĻž āĻŽā§‡āĻŽāϰāĻŋāϰ āϕ⧋āύ⧋ āĻāĻ•āϟāĻž āύāĻŋāĻ°ā§āĻĻāĻŋāĻˇā§āϟ āϞ⧋āϕ⧇āĻļāύ āĻŦāĻž āĻŽā§‡āĻŽā§‹āϰāĻŋ āϞ⧋āϕ⧇āĻļāύ (Memory location)-āϕ⧇ āĻĒā§Ÿā§‡āĻ¨ā§āϟ āĻ•āϰ⧇āĨ¤ āϏāĻŋ āĻ˛ā§āϝāĻžāĻ™ā§āĻ—ā§ā§Ÿā§‡āĻœā§‡āϰ āχāύāϭ⧇āĻ¨ā§āϟāϰ āĻŦāĻž āφāĻŦāĻŋāĻˇā§āĻ•āĻžāϰāĻ•āϕ⧇ āĻāĻ•āϟāĻž āĻĨā§āϝāĻžāĻ‚āĻ•āϏ āĻĻ⧇āĻ“ā§ŸāĻžāχ āϞāĻžāĻ—āĻŦ⧇, āĻ•āĻžāϰāĻŖ āωāύāĻŋ āφāĻŽāĻžāĻĻ⧇āϰ āϜāĻ¨ā§āϝ āĻŽā§‡āĻŽāϰāĻŋāϤ⧇ āĻŽāĻžāύ āϏ⧇āĻ­ āĻ•āϰāĻžāϰ āĻ•āĻžāϜāϟāĻž āϜāĻžāĻ¸ā§āϟ āύāĻŋāĻœā§‡āĻĻ⧇āϰ āĻĒāĻ›āĻ¨ā§āĻĻ⧇āϰ āύāĻžāĻŽ āχāωāϜ āĻ•āϰ⧇āχ āĻĒāĻžāύāĻŋāϰ āĻŽāϤ⧋ āϏ⧋āϜāĻž āĻŦāĻžāύāĻŋā§Ÿā§‡ āĻĻāĻŋā§Ÿā§‡āϛ⧇āύāĨ¤ āφāĻĒāύāĻžāϰ āĻ­ā§āϝāĻžāϞ⧁ āĻŽā§‡āĻŽāϰāĻŋāϰ āĻ āĻŋāĻ• āϕ⧋āύ āĻ āĻŋāĻ•āĻžāύāĻžā§Ÿ āĻŦāĻž āĻāĻĄā§āϰ⧇āϏ⧇ āĻ¸ā§āĻŸā§‹āϰ āĻšāĻšā§āϛ⧇ āϤāĻž āύāĻŋā§Ÿā§‡ āφāĻĒāύāĻžāϰ āĻŦāĻŋāĻ¨ā§āĻĻ⧁āĻŽāĻžāĻ¤ā§āϰ āĻĒā§āϝāĻžāϰāĻž āύ⧇āĻ“ā§ŸāĻžāϰ āĻĻāϰāĻ•āĻžāϰ āύāĻžāχāĨ¤ āφāĻĒāύāĻžāϰ āĻ•āĻžāϜ āĻšāϞ⧋ āϜāĻžāĻ¸ā§āϟ āĻāĻ•āϟāĻž āϏ⧁āĻ¨ā§āĻĻāϰ āύāĻžāĻŽ āĻĻ⧇āĻ“ā§ŸāĻž, āφāϰ āĻŦā§āϝāĻžāĻ•āĻ—ā§āϰāĻžāωāĻ¨ā§āĻĄā§‡ sistema āύāĻŋāĻœā§‡ āĻĨ⧇āϕ⧇āχ āĻāϕ⧇ āĻŽā§‡āĻŽā§‹āϰāĻŋ āϞ⧋āϕ⧇āĻļāύ⧇āϰ āϏāĻžāĻĨ⧇ āϞāĻŋāĻ‚āĻ• āĻ•āϰ⧇ āύ⧇āĻŦ⧇āĨ¤

āĻ­ā§āϝāĻžāϰāĻŋā§Ÿā§‡āĻŦāϞ āĻĄāĻŋāĻ•ā§āĻ˛ā§‡ā§ŸāĻžāϰ⧇āĻļāύ āĻŦāύāĻžāĻŽ āĻĄā§‡āĻĢāĻŋāύāĻŋāĻļāύ (Variable Declaration versus Definition)

āĻĒāϰ⧀āĻ•ā§āώāĻžāϰ āϜāĻ¨ā§āϝ āĻ­ā§āϝāĻžāϰāĻŋā§Ÿā§‡āĻŦāϞ⧇āϰ āĻĻ⧁āχāϟāĻž āĻ•ā§āϰ⧁āĻļāĻŋ⧟āĻžāϞ āϟāĻžāĻ°ā§āĻŽ āĻāĻ•āĻĻāĻŽ āĻ•ā§āϰāĻŋāĻ¸ā§āϟāĻžāϞ āĻ•ā§āϞāĻŋ⧟āĻžāϰ āϰāĻžāĻ–āĻž āϞāĻžāĻ—āĻŦ⧇:

  1. āĻ­ā§āϝāĻžāϰāĻŋā§Ÿā§‡āĻŦāϞ āĻĄāĻŋāĻ•ā§āĻ˛ā§‡ā§ŸāĻžāϰ⧇āĻļāύ (Declaration of a Variable): āĻĒā§āϰ⧋āĻ—ā§āϰāĻžāĻŽā§‡ āϕ⧋āύ⧋ āĻ­ā§āϝāĻžāϰāĻŋā§Ÿā§‡āĻŦāϞ āχāωāϜ āĻ•āϰāĻžāϰ āφāϗ⧇ āϤāĻžāϕ⧇ āĻ…āĻŦāĻļā§āϝāχ āĻĄāĻŋāĻ•ā§āĻ˛ā§‡ā§ŸāĻžāϰ (Declare) āĻ•āϰāϤ⧇ āĻšāĻŦ⧇āĨ¤ āĻĄāĻŋāĻ•ā§āĻ˛ā§‡ā§ŸāĻžāϰ⧇āĻļāύ āĻŽāĻžāύ⧇ āĻšāϞ⧋ āĻ•āĻŽā§āĻĒāĻžāχāϞāĻžāϰ (Compiler)-āĻāϰ āĻ•āĻžāϛ⧇ āĻ“āχ āĻ­ā§āϝāĻžāϰāĻŋā§Ÿā§‡āĻŦāϞ⧇āϰ āϏāĻŦ āĻĒā§āϰāĻĒāĻžāĻ°ā§āϟāĻŋāϜ āĻŦāĻž āĻŦ⧈āĻļāĻŋāĻˇā§āĻŸā§āϝ āĻ…ā§āϝāĻžāύāĻžāωāĻ¨ā§āϏ (Announce) āĻ•āϰāĻž āĻŦāĻž āĻ˜ā§‹āώāĻŖāĻž āĻĻ⧇āĻ“ā§ŸāĻžāĨ¤ āĻĒā§āϰāĻĒāĻžāĻ°ā§āϟāĻŋāϜ āĻŦāϞāϤ⧇ āĻāĻ–āĻžāύ⧇ āĻ­ā§āϝāĻžāϰāĻŋā§Ÿā§‡āĻŦāϞ⧇āϰ āύāĻžāĻŽ (Name) āϕ⧀ āĻšāĻŦ⧇ āφāϰ āϏ⧇ āĻŽā§‡āĻŽāϰāĻŋāϤ⧇ āĻ•āϤāϟ⧁āϕ⧁ āϏāĻžāχāϜ (Size) āύ⧇āĻŦ⧇ āϤāĻž āĻŦā§‹āĻāĻžā§ŸāĨ¤
  2. āĻ­ā§āϝāĻžāϰāĻŋā§Ÿā§‡āĻŦāϞ āĻĄā§‡āĻĢāĻŋāύāĻŋāĻļāύ (Definition of a Variable): āĻĄā§‡āĻĢāĻŋāύāĻŋāĻļāύ āĻŽāĻžāύ⧇ āĻšāϞ⧋ āĻ­ā§āϝāĻžāϰāĻŋā§Ÿā§‡āĻŦāϞ⧇āϰ āϜāĻ¨ā§āϝ āĻŽā§‡āĻŽāϰāĻŋāϤ⧇ āĻāĻ•āϚ⧁⧟āĻžāϞ āϜāĻžā§ŸāĻ—āĻž āĻŦāĻž āĻŽā§‡āĻŽā§‹āϰāĻŋ āĻ¸ā§āĻĒ⧇āϏ āĻ…ā§āϝāĻžāϞ⧋āϕ⧇āϟ (Allocating memory) āĻ•āϰāĻžāĨ¤

āĻŦ⧇āĻļāĻŋāϰāĻ­āĻžāĻ— āϏāĻŽā§Ÿ āϏāĻŋ āĻ˛ā§āϝāĻžāĻ™ā§āĻ—ā§ā§Ÿā§‡āĻœā§‡ āĻĄāĻŋāĻ•ā§āĻ˛ā§‡ā§ŸāĻžāϰ⧇āĻļāύ āφāϰ āĻĄā§‡āĻĢāĻŋāύāĻŋāĻļāύ āĻāĻ•āχ āϏāĻžāĻĨ⧇ āĻ•āϰāĻž āĻšā§Ÿā§‡ āϝāĻžā§ŸāĨ¤ āϤāĻŦ⧇ āϏāĻŦ āĻ•ā§āώ⧇āĻ¤ā§āϰ⧇ āĻ•āĻŋāĻ¨ā§āϤ⧁ āĻāĻŽāύāϟāĻž āĻšā§Ÿ āύāĻžāĨ¤ āĻ­ā§āϝāĻžāϰāĻŋā§Ÿā§‡āĻŦāϞ⧇āϰ āϏāĻžāĻĨ⧇ āφāĻĒāύāĻŋ āϕ⧀ āϧāϰāύ⧇āϰ āĻŽāĻĄāĻŋāĻĢāĻžā§ŸāĻžāϰ (Modifiers) āχāωāϜ āĻ•āϰāϤ⧇āĻ› āϤāĻžāϰ āĻ“āĻĒāϰ āĻāϟāĻž āĻĄāĻŋāĻĒ⧇āĻ¨ā§āĻĄ āĻ•āϰ⧇āĨ¤
alt text

āϏāĻŋāύāĻŸā§āϝāĻžāĻ•ā§āϏ āĻāĻŦāĻ‚ āĻŽā§‡āĻŽā§‹āϰāĻŋ āĻ…ā§āϝāĻžāϞ⧋āϕ⧇āĻļāύ (Syntax and Memory Allocation)

āĻāĻ•āϟāĻž āĻ­ā§āϝāĻžāϰāĻŋā§Ÿā§‡āĻŦāϞ āĻĄāĻŋāĻ•ā§āĻ˛ā§‡ā§ŸāĻžāϰ āĻ•āϰāĻžāϰ āĻŦ⧇āϏāĻŋāĻ• āϏāĻŋāύāĻŸā§āϝāĻžāĻ•ā§āϏ (Syntax) āĻšāϞ⧋ āĻĒā§āϰāĻĨāĻŽā§‡ āĻĄā§‡āϟāĻž āϟāĻžāχāĻĒ (Data type) āϞāĻŋāĻ–āϤ⧇ āĻšāĻŦ⧇ āĻāĻŦāĻ‚ āϤāĻžāϰāĻĒāϰ āĻ­ā§āϝāĻžāϰāĻŋā§Ÿā§‡āĻŦāϞ⧇āϰ āύāĻžāĻŽ āĻĻāĻŋāϤ⧇ āĻšāĻŦ⧇āĨ¤ āϝ⧇āĻŽāύ:

int var;

āĻ­ā§āϝāĻžāϰāĻŋā§Ÿā§‡āĻŦāϞ⧇āϰ āύāĻžāĻŽ āĻšāϞ⧋ var āφāϰ āĻāϟāĻžāϰ āĻĄā§‡āϟāĻž āϟāĻžāχāĻĒ āĻšāϞ⧋ int āĻŦāĻž āχāĻ¨ā§āϟāĻŋāϜāĻžāϰ (Integer)āĨ¤ āĻĄā§‡āϟāĻž āϟāĻžāχāĻĒ⧇āϰ āĻŽā§‡āχāύ āĻ•āĻžāϜāχ āĻšāϞ⧋ āĻ•āĻŽā§āĻĒāĻžāχāϞāĻžāϰāϕ⧇ āĻŦāϞ⧇ āĻĻ⧇āĻ“ā§ŸāĻž āϝ⧇ āĻāχ āĻ­ā§āϝāĻžāϰāĻŋā§Ÿā§‡āĻŦāϞāϟāĻž āĻŽā§‡āĻŽāϰāĻŋāϤ⧇ āĻ āĻŋāĻ• āĻ•āϤāϟāĻž āϜāĻžā§ŸāĻ—āĻž āĻĻāĻ–āϞ āĻ•āϰāϤ⧇ āϝāĻžāĻšā§āϛ⧇āĨ¤

āϕ⧋āĻĄā§‡ int var; āϞ⧇āĻ–āĻžāϰ āĻŽāĻžāύ⧇āχ āĻšāϞ⧋ āφāĻĒāύāĻŋ āĻ­ā§āϝāĻžāϰāĻŋā§Ÿā§‡āĻŦāϞāϟāĻž āĻĄāĻŋāĻ•ā§āĻ˛ā§‡ā§ŸāĻžāϰ āĻ•āϰāĻžāϰ āĻĒāĻžāĻļāĻžāĻĒāĻžāĻļāĻŋ āĻ•āĻŽā§āĻĒāĻžāχāϞāĻžāϰāϕ⧇ āϰāĻŋāĻ•ā§‹ā§Ÿā§‡āĻ¸ā§āϟ āĻ•āϰāϤ⧇āĻ› āĻāϰ āϜāĻ¨ā§āϝ āĻŽā§‡āĻŽā§‹āϰāĻŋ āĻ…ā§āϝāĻžāϞ⧋āϕ⧇āϟ (Allocate memory) āĻ•āϰāϤ⧇āĨ¤ āĻāχ āĻ¸ā§āĻŸā§‡āϟāĻŽā§‡āĻ¨ā§āĻŸā§‡āϰ āĻļ⧇āώ⧇ āĻāĻ•āϟāĻž āϏ⧇āĻŽāĻŋāϕ⧋āϞāύ ; āĻŦāϏāĻžāύ⧋ āĻāĻ•āĻĻāĻŽ āĻŽā§āϝāĻžāĻ¨ā§āĻĄā§‡āϟāϰāĻŋ āĻŦāĻž āĻŦāĻžāĻ§ā§āϝāϤāĻžāĻŽā§āϞāĻ•āĨ¤ āĻ•āĻžāϰāĻŖ āĻ•āĻŽā§āĻĒāĻžāχāϞāĻžāϰ āĻāχ āϏ⧇āĻŽāĻŋāϕ⧋āϞāύ āĻĻ⧇āϖ⧇āχ āĻāĻ•āϟāĻž āĻ¸ā§āĻŸā§‡āϟāĻŽā§‡āĻ¨ā§āϟ āĻĨ⧇āϕ⧇ āφāϰ⧇āĻ•āϟāĻž āĻ¸ā§āĻŸā§‡āϟāĻŽā§‡āĻ¨ā§āϟ āφāϞāĻžāĻĻāĻž āĻ•āϰ⧇āĨ¤ āϤāĻžāχ āϏ⧇āĻŽāĻŋāϕ⧋āϞāύ āĻĻāĻŋāϤ⧇ āϭ⧁āϞ⧇ āϗ⧇āϞ⧇ āϕ⧋āĻĄā§‡ āĻāϰāϰ (Compilation error) āĻ–āĻžāĻŦ⧇āĨ¤

alt text
āĻ­ā§āϝāĻžāϰāĻŋā§Ÿā§‡āĻŦāϞ āĻŽā§‡āĻŽāϰāĻŋāϤ⧇ ⧍ āĻŦāĻžāχāϟ (2 bytes) āϜāĻžā§ŸāĻ—āĻž āύ⧇āĻŦ⧇ āύāĻžāĻ•āĻŋ ā§Ē āĻŦāĻžāχāϟ (4 bytes) āϜāĻžā§ŸāĻ—āĻž āύ⧇āĻŦ⧇ āϤāĻž āĻĒ⧁āϰ⧋āĻĒ⧁āϰāĻŋ āĻĄāĻŋāĻĒ⧇āĻ¨ā§āĻĄ āĻ•āϰ⧇ āφāĻĒāύāĻŋ āϕ⧋āύ āϏāĻŋāĻ¸ā§āĻŸā§‡āĻŽ āĻŦāĻž āĻ“āĻāϏ (System architecture)-āĻ āĻ•āĻžāϜ āĻ•āϰāϤ⧇āĻ› āϤāĻžāϰ āĻ“āĻĒāϰāĨ¤ āϕ⧋āĻĄāĻžāϰ āĻšāĻŋāϏ⧇āĻŦ⧇ āφāĻĒāύāĻžāϰ āϜāĻžāĻ¸ā§āϟ āĻĄāĻŋāĻ•ā§āĻ˛ā§‡ā§ŸāĻžāϰ⧇āĻļāύ āφāϰ āĻĄā§‡āĻĢāĻŋāύāĻŋāĻļāύ⧇ āĻĢā§‹āĻ•āĻžāϏ āĻ•āϰāϞ⧇āχ āϚāϞāĻŦ⧇, āĻŽā§‡āĻŽāϰāĻŋāϰ āϭ⧇āϤāϰ⧇ āĻ•āϤāϟ⧁āϕ⧁ āĻ¸ā§āĻĒ⧇āϏ āĻ…ā§āϝāĻžāϞ⧋āϕ⧇āĻļāύ āĻšāĻšā§āϛ⧇ āϤāĻž āύāĻŋā§Ÿā§‡ āĻŽāĻžāĻĨāĻž āϘāĻžāĻŽāĻžāύ⧋āϰ āϕ⧋āύ⧋ āĻĻāϰāĻ•āĻžāϰ āύāĻžāχāĨ¤

āĻ­ā§āϝāĻžāϰāĻŋā§Ÿā§‡āĻŦāϞ āχāύāĻŋāĻļāĻŋ⧟āĻžāϞāĻžāχāĻœā§‡āĻļāύ āĻāĻŦāĻ‚ āϰāĻŋ-āĻ…ā§āϝāĻžāϏāĻžāχāύāĻŽā§‡āĻ¨ā§āϟ (Variable Initialization and Re-assignment)

āĻ­ā§āϝāĻžāϰāĻŋā§Ÿā§‡āĻŦāϞ āĻĄāĻŋāĻ•ā§āĻ˛ā§‡ā§ŸāĻžāϰ āĻ•āϰāĻžāϰ āϏāĻŽā§Ÿāχ āϝāĻĻāĻŋ āφāĻĒāύāĻŋ āϤāĻžāϤ⧇ āϕ⧋āύ⧋ āĻŽāĻžāύ āĻŦāĻž āĻ­ā§āϝāĻžāϞ⧁ āĻ…ā§āϝāĻžāϏāĻžāχāύ (Assign) āĻ•āϰ⧇ āĻĻāĻžāĻ“, āϤāĻŦ⧇ āϏ⧇āχ āĻĒā§āϰāϏ⧇āϏāϕ⧇ āχāύāĻŋāĻļāĻŋ⧟āĻžāϞāĻžāχāĻœā§‡āĻļāύ (Initialization) āĻŦāϞ⧇āĨ¤ āϝ⧇āĻŽāύ:

int var = 3;

āχāύāĻŋāĻļāĻŋ⧟āĻžāϞāĻžāχāĻœā§‡āĻļāύ āĻ•āϰāĻžāϰ āĻŽāĻžāύ⧇ āĻāχ āύāĻž āϝ⧇ āĻāχ āĻ­ā§āϝāĻžāϞ⧁ āϏāĻžāϰāĻž āĻœā§€āĻŦāύ⧇āϰ āϜāĻ¨ā§āϝ āĻĢāĻŋāĻ•ā§āϏāĻĄ āĻšā§Ÿā§‡ āϗ⧇āϞāĨ¤ āφāĻĒāύāĻŋ āϚāĻžāχāϞ⧇ āϕ⧋āĻĄā§‡āϰ āϝ⧇āϕ⧋āύ⧋ āϜāĻžā§ŸāĻ—āĻžā§Ÿ āĻāχ āĻŽāĻžāύ āϝāĻ–āύ āϖ⧁āĻļāĻŋ āĻšā§‡āĻžā§āϜ āĻ•āϰāϤ⧇ āĻĒāĻžāϰ⧋āĨ¤ āĻāχ āĻ•āĻžāϰāϪ⧇āχ āĻāχ āϜāĻŋāύāĻŋāϏ⧇āϰ āύāĻžāĻŽ āĻĻ⧇āĻ“ā§ŸāĻž āĻšā§Ÿā§‡āϛ⧇ āĻ­ā§āϝāĻžāϰāĻŋā§Ÿā§‡āĻŦāϞ, āϝāĻž āĻāϏ⧇āϛ⧇ "Vary" āĻļāĻŦā§āĻĻ āĻĨ⧇āϕ⧇, āϝāĻžāϰ āĻŽāĻžāύ⧇ āĻšāϞ⧋ āϝāĻž āϏāĻŽā§Ÿā§‡āϰ āϏāĻžāĻĨ⧇ āϏāĻžāĻĨ⧇ āĻĒāϰāĻŋāĻŦāĻ°ā§āϤāĻŋāϤ āĻŦāĻž āĻšā§‡āχāĻžā§āϜ āĻšāϤ⧇ āĻĒāĻžāϰ⧇āĨ¤ āĻāϟāĻž āĻšāϞ⧋ āĻ•āύāĻ¸ā§āĻŸā§āϝāĻžāĻ¨ā§āϟ (Constant)-āĻāϰ āĻāĻ•āĻĻāĻŽ āĻ…āĻĒāϜāĻŋāϟ āĻŦāĻž āωāĻ˛ā§āĻŸā§‹ āϜāĻŋāύāĻŋāϏ, āϝāĻž āĻāĻ•āĻŦāĻžāϰ āĻĄāĻŋāĻĢāĻžāχāύ āĻ•āϰāϞ⧇ āφāϰ āϏāĻ•āϞ⧇āχ āĻšā§‡āχāĻžā§āϜ āĻ•āϰāĻž āϝāĻžā§Ÿ āύāĻžāĨ¤

āύāĻŋāĻšā§‡āϰ āϕ⧋āĻĄā§‡āϰ āĻ“ā§ŸāĻžāĻ°ā§āĻ•āĻĢā§āϞ⧋āϟāĻž āĻ–ā§‡ā§ŸāĻžāϞ āĻ•āϰ⧋:

#include <stdio.h>
int main() {
    int var = 3; // āχāύāĻŋāĻļāĻŋ⧟āĻžāϞāĻžāχāĻœā§‡āĻļāύ (Initialization)
    var = 4;     // āϰāĻŋ-āĻ…ā§āϝāĻžāϏāĻžāχāύāĻŽā§‡āĻ¨ā§āϟ (Re-assignment)
    printf("%d", var);
    return 0;
}

alt text
āĻāχ āϕ⧋āĻĄāϟāĻž āϝāĻ–āύ āφāĻŽāϰāĻž āĻŦāĻŋāĻ˛ā§āĻĄ āĻ…ā§āϝāĻžāĻ¨ā§āĻĄ āϰāĻžāύ (Build and Run) āĻ•āϰāĻŦ, āϤāĻ–āύ āφāĻŽāĻžāĻĻ⧇āϰ āĻŦā§āĻ˛ā§āϝāĻžāĻ• āĻ•āύāϏ⧋āϞ āωāχāĻ¨ā§āĻĄā§‹ (Console window)-āϤ⧇ āφāωāϟāĻĒ⧁āϟ āφāϏāĻŦ⧇ 4āĨ¤ āĻ…āĻ°ā§āĻĨāĻžā§Ž āφāϗ⧇āϰ ā§Š āĻŽāĻžāύāϟāĻž āϏāĻžāĻ•āϏ⧇āϏāĻĢ⧁āϞāĻŋ āφāĻĒāĻĄā§‡āϟ āĻšā§Ÿā§‡ ā§Ē āĻšā§Ÿā§‡ āϗ⧇āϛ⧇āĨ¤

āĻ•āĻŋāϛ⧁ āĻ•ā§āϰāĻŋāϟāĻŋāĻ•ā§āϝāĻžāϞ āϰ⧁āϞāϏ āĻāĻŦāĻ‚ āϰ⧇āĻ¸ā§āĻŸā§āϰāĻŋāĻ•āĻļāύ (Critical Rules and Constraints)

  1. āχāϞāϞāĻŋāĻ—āĻžāϞ āϰāĻŋ-āĻĄā§‡āĻĢāĻŋāύāĻŋāĻļāύ (Illegal Redefinition): āϕ⧋āĻĄā§‡ āϝāĻ–āύ āĻŽāĻžāύ āφāĻĒāĻĄā§‡āϟ āĻ•āϰāĻžāϰ āϜāĻ¨ā§āϝ var = 4; āϞ⧇āĻ–āĻž āĻšā§Ÿā§‡āϛ⧇, āϤāĻ–āύ āĻ•āĻŋāĻ¨ā§āϤ⧁ āϏāĻžāĻŽāύ⧇ āφāĻŦāĻžāϰ āύāϤ⧁āύ āĻ•āϰ⧇ int āĻĄā§‡āϟāĻž āϟāĻžāχāĻĒ āϞ⧇āĻ–āĻž āĻšā§Ÿ āύāĻžāχāĨ¤ āĻŽā§‡āĻŽā§‹āϰāĻŋ āĻ…ā§āϝāĻžāϞ⧋āϕ⧇āĻļāύ āĻ­ā§āϝāĻžāϰāĻŋā§Ÿā§‡āĻŦāϞ⧇āϰ āϜāĻ¨ā§āϝ āϜāĻžāĻ¸ā§āϟ āĻāĻ•āĻŦāĻžāϰāχ āĻšā§ŸāĨ¤ āĻāĻ•āχ āĻŦā§āϞāϕ⧇āϰ āϭ⧇āϤāϰ āϝāĻĻāĻŋ āφāĻĒāύāĻŋ āφāĻŦāĻžāϰ int var = 4; āϞ⧇āĻ–ā§‹, āϤāĻžāϰ āĻŽāĻžāύ⧇ āφāĻĒāύāĻŋ āĻāĻ•āχ āύāĻžāĻŽā§‡āϰ āϜāĻ¨ā§āϝ āĻ“ā§ŸāĻžāύ āĻŽā§‹āϰ āϟāĻžāχāĻŽ āĻŽā§‡āĻŽā§‹āϰāĻŋ āĻ¸ā§āĻĒ⧇āϏ āϚāĻžāĻšā§āĻ›, āϝāĻž āϏāĻŋ āĻ˛ā§āϝāĻžāĻ™ā§āĻ—ā§ā§Ÿā§‡āĻœā§‡ āĻāĻ•āĻĻāĻŽ āχāϞāϞāĻŋāĻ—āĻžāϞ āĻŦāĻž āĻ…āĻŦ⧈āϧ (Illegal)āĨ¤
  2. āĻāĻ•āĻ• āĻĄā§‡āĻĢāĻŋāύāĻŋāĻļāύ āϰ⧁āϞ (Single Definition Rule): āĻĒā§āϰāϤāĻŋāϟāĻž āĻ­ā§āϝāĻžāϰāĻŋā§Ÿā§‡āĻŦāϞ āϤāĻžāϰ āύāĻŋāĻ°ā§āĻĻāĻŋāĻˇā§āϟ āĻ¸ā§āϕ⧋āĻĒ āĻŦāĻž āĻŦā§āϞāϕ⧇āϰ āϭ⧇āϤāϰ āϜāĻžāĻ¸ā§āϟ āĻāĻ•āĻŦāĻžāϰāχ āĻĄāĻŋāĻĢāĻžāχāύ āĻ•āϰāĻž āϝāĻžāĻŦ⧇, āϤāĻŦ⧇ āϤāĻžāϕ⧇ āĻĄāĻŋāĻĢāϰ⧇āĻ¨ā§āϟ āĻ…ā§āϝāĻžāϏāĻžāχāύāĻŽā§‡āĻ¨ā§āϟ (Different assignment) āĻĻāĻŋā§Ÿā§‡ āĻŽāĻžāĻ˛ā§āϟāĻŋāĻĒāϞ āϟāĻžāχāĻŽāϏ āχāωāϜ āĻ•āϰāĻž āϝāĻžāĻŦ⧇āĨ¤ āĻāχ main() āĻĢāĻžāĻ‚āĻļāύ āĻŦā§āϞāϕ⧇āϰ āϭ⧇āϤāϰ āφāĻĒāύāĻŋ āĻ•āĻ–āύ⧋āχ āϏ⧇āĻŽ āύāĻžāĻŽā§‡ āĻāĻ•āĻžāϧāĻŋāĻ• āĻ­ā§āϝāĻžāϰāĻŋā§Ÿā§‡āĻŦāϞ āĻĄāĻŋāĻĢāĻžāχāύ āĻ•āϰāϤ⧇ āĻĒāĻžāϰāĻŦ⧇ āύāĻžāĨ¤
  3. āĻĢāϰāĻŽā§āϝāĻžāϟ āĻ¸ā§āĻĒ⧇āϏāĻŋāĻĢāĻžā§ŸāĻžāϰ (Format Specifier): āϕ⧋āĻĄā§‡āϰ printf("%d", var); āĻ…āĻ‚āĻļ⧇ āĻĨāĻžāĻ•āĻž %d āĻšāϞ⧋ āĻāĻ•āϟāĻž āĻĢāϰāĻŽā§āϝāĻžāϟ āĻ¸ā§āĻĒ⧇āϏāĻŋāĻĢāĻžā§ŸāĻžāϰ (Format specifier)āĨ¤ āĻāϟāĻž āχāĻ¨ā§āϟāĻŋāϜāĻžāϰ⧇āϰ āϜāĻ¨ā§āϝ āχāωāϜ āĻ•āϰāĻž āĻšā§Ÿ āĻāĻŦāĻ‚ āĻ•āĻŽā§āĻĒāĻžāχāϞāĻžāϰāϕ⧇ āύāĻŋāĻ°ā§āĻĻ⧇āĻļ āĻĻā§‡ā§Ÿ āĻ­ā§āϝāĻžāϰāĻŋā§Ÿā§‡āĻŦāϞ⧇āϰ āϭ⧇āϤāϰ⧇āϰ āĻ•āĻžāϰ⧇āĻ¨ā§āϟ āĻ•āĻ¨ā§āĻŸā§‡āĻ¨ā§āϟ āĻŦāĻž āĻŽāĻžāύ āĻ¸ā§āĻ•ā§āϰāĻŋāύ⧇ āĻĒā§āϰāĻŋāĻ¨ā§āϟ āĻ•āϰāϤ⧇āĨ¤

āĻ…ā§āϝāĻžāĻĄāĻ­āĻžāĻ¨ā§āϏāĻĄ āĻ…ā§āϝāĻžāϏāĻžāχāύāĻŽā§‡āĻ¨ā§āϟ āĻ…āĻĒāĻžāϰ⧇āĻļāύāϏ (Advanced Assignment Operations)

  1. āĻ­ā§āϝāĻžāϰāĻŋā§Ÿā§‡āĻŦāϞ āϟ⧁ āĻ­ā§āϝāĻžāϰāĻŋā§Ÿā§‡āĻŦāϞ āĻ…ā§āϝāĻžāϏāĻžāχāύāĻŽā§‡āĻ¨ā§āϟ (Assigning a Variable to another Variable): āϕ⧋āύ⧋ āĻĄāĻŋāϰ⧇āĻ•ā§āϟ āĻ•āύāĻ¸ā§āĻŸā§āϝāĻžāĻ¨ā§āϟ āĻ­ā§āϝāĻžāϞ⧁ āĻ…ā§āϝāĻžāϏāĻžāχāύ āύāĻž āĻ•āϰ⧇ āφāĻĒāύāĻŋ āϚāĻžāχāϞ⧇ āĻāĻ•āϟāĻž āĻ­ā§āϝāĻžāϰāĻŋā§Ÿā§‡āĻŦāϞ⧇āϰ āĻŽāĻžāύ āφāϰ⧇āĻ•āϟāĻž āĻ­ā§āϝāĻžāϰāĻŋā§Ÿā§‡āĻŦāϞ⧇āϰ āϭ⧇āϤāϰ āĻ…ā§āϝāĻžāϏāĻžāχāύ āĻ•āϰāϤ⧇ āĻĒāĻžāϰ⧋āĨ¤ āϝ⧇āĻŽāύ:

int var1 = 3;
int var2 = var1;

alt text

āĻāĻ–āĻžāύ⧇ āĻĄāĻžāύ āĻĒāĻžāĻļ⧇ āĻ­ā§āϝāĻžāϰāĻŋā§Ÿā§‡āĻŦāϞ⧇āϰ āύāĻžāĻŽ var1 āϞ⧇āĻ–āĻžāϰ āĻŽāĻžāύ⧇āχ āĻšāϞ⧋ āφāĻĒāύāĻŋ āϤāĻžāϰ āϭ⧇āϤāϰ⧇āϰ āĻ•āύāĻ¸ā§āĻŸā§āϝāĻžāĻ¨ā§āϟ āĻŽāĻžāύ (āϝāĻž āĻšāϞ⧋ ā§Š) āĻŸā§‡āύ⧇ āĻāύ⧇ var2 āĻāϰ āϭ⧇āϤāϰ āĻ…ā§āϝāĻžāϏāĻžāχāύ āĻ•āϰāϤ⧇āĻ›āĨ¤ āĻāĻ–āύ var2 āĻĒā§āϰāĻŋāĻ¨ā§āϟ āĻ•āϰāϞ⧇ āφāωāϟāĻĒ⧁āϟ ā§Š āχ āφāϏāĻŦ⧇āĨ¤

  1. āĻāĻ• āϞāĻžāχāύ⧇ āĻŽāĻžāĻ˛ā§āϟāĻŋāĻĒāϞ āĻ­ā§āϝāĻžāϰāĻŋā§Ÿā§‡āĻŦāϞ āĻ…āĻĒāĻžāϰ⧇āĻļāύ (Single Line Multi Variable Operations): āφāϞāĻžāĻĻāĻž āφāϞāĻžāĻĻāĻž āϞāĻžāχāύ⧇ āĻ­ā§āϝāĻžāϰāĻŋā§Ÿā§‡āĻŦāϞ āĻĄāĻŋāĻ•ā§āĻ˛ā§‡ā§ŸāĻžāϰ āφāϰ āĻĄāĻŋāĻĢāĻžāχāύ āύāĻž āĻ•āϰ⧇ āϕ⧋āĻĄā§‡āϰ āĻ•ā§āϞāĻŋāύāύ⧇āϏ āĻŦāĻž āϰāĻŋāĻĄāĻžāĻŦāĻŋāϞāĻŋāϟāĻŋ āĻŦāĻžā§œāĻžāύ⧋āϰ āϜāĻ¨ā§āϝ āϏ⧇āĻŽ āĻĄā§‡āϟāĻž āϟāĻžāχāĻĒ⧇āϰ āĻāĻ•āĻžāϧāĻŋāĻ• āĻ­ā§āϝāĻžāϰāĻŋā§Ÿā§‡āĻŦāϞ āϜāĻžāĻ¸ā§āϟ āĻāĻ• āϞāĻžāχāύ⧇āχ āĻšā§āϝāĻžāĻ¨ā§āĻĄā§‡āϞ āĻ•āϰāĻž āϝāĻžā§ŸāĨ¤ āϝ⧇āĻŽāύ:

int var1 = 4, var2 = 4, var3 = 4;

alt text

āĻāĻ–āĻžāύ⧇ āĻāĻ•āχ āϞāĻžāχāύ⧇ āϏāĻŦ āĻ­ā§āϝāĻžāϰāĻŋā§Ÿā§‡āĻŦāϞāϕ⧇ āϏ⧇āĻŽ āĻ­ā§āϝāĻžāϞ⧁ āĻĻāĻŋā§Ÿā§‡ āĻ…ā§āϝāĻžāϏāĻžāχāύ āĻ•āϰāĻž āĻšā§Ÿā§‡āϛ⧇āĨ¤ āĻāĻ–āύ āĻāĻĻ⧇āϰāϕ⧇ āϝāĻĻāĻŋ 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:

  1. 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.
  2. 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:

int var;

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:

int var = 3;

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

  1. Illegal Redefinition: Notice that during the re-assignment statement var = 4;, the data type int is not written again. Memory is allocated to a variable only once. Writing int 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.
  2. 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.
  3. Format Specifier: In the statement printf("%d", var);, the %d is 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

  1. 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:
int var1 = 3;
int var2 = var1;

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.

  1. 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:
int var1 = 4, var2 = 4, var3 = 4;

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.