2.4 VARIABLE

                                    

     Programming in C    



CONCEPT OF A VARIABLE

Programs operate on data. The instructions that form a program and the data on which they operate must be stored somewhere while the program is being executed. A programming language must therefore provide a mechanism to store and manipulate data. In this context, a computer provides Random Access Memory (RAM) for storing both the executable program code and the data that the program manipulates.


Computer Memory and Addressing

Computer memory consists of registers and memory cells that store information in the form of binary digits (bits), i.e., 0 and 1. Memory accesses data as a collection of bits, commonly in groups of 8 bits (1 byte), 16 bits, 32 bits, or 64 bits.

Each byte in memory is stored at a unique physical location, called a memory address. Hence, every byte can be uniquely identified using its address. This addressing mechanism allows the computer to store and retrieve data efficiently.


Word Length of a Computer

The word length of a computer refers to the number of bits the processor can operate on simultaneously. A word is the natural unit of memory for a computer system.

  • 8-bit systems → 1 byte word

  • 16-bit systems (e.g., 80286 processors) → 2 bytes

  • 32-bit systems (e.g., Pentium processors) → 4 bytes

  • 64-bit systems → 8 bytes or more

   

For example, when we say that a Pentium 4 is a 32-bit machine, it means that it can process 32 bits of data at a time.


Definition of a Variable

A variable is an identifier for a memory location where data can be stored and later retrieved. Variables are used to hold data values so that they can be used in various computations within a program.

Instead of remembering memory addresses like 46735, programmers use meaningful names (variables) to refer to memory locations. Every variable is mapped to a unique memory address by the compiler.


Variable and Memory Allocation

The C compiler generates executable code that maps data entities to memory locations.

Example:

int salary = 65000;


This statement causes the compiler to:

  • Allocate memory to store the variable salary

  • Store the value 65000 in binary form

  • Use the address of the first byte allocated to refer to the variable

The exact number of bytes and binary representation depend on the C implementation, but the programmer need not worry about these details.


Importance of Variables

Although the exact binary representation of data is rarely important to a programmer, understanding:

  • Memory organization

  • Addressing

  • Use of variables

is crucial for writing correct and efficient programs.


Attributes of a Variable

Every variable in C has three important attributes:

  1. Data Type
    The type of data the variable can store (e.g., int, float, char).
    Once defined, the data type cannot be changed.

  2. Variable Name
    The identifier used to refer to the memory location.

  3. Value
    The data stored in the variable. The kind of value depends on its type.
    For example, an integer variable can store values like 10, –5, 100.


Rules for Naming Variables

  • A minimum of 31 characters must be supported by any standard C compiler.

  • Variable names should not be too long, as they reduce readability.

  • Variable names are case sensitive (NUM and num are different).

  • Some compilers may truncate very long variable names.


Declaration of Variables in C

In C, a variable must be declared before it is used. Variables are usually declared:

  • At the beginning of a function

  • Or at the start of a block

Declaration serves two purposes:

  1. It informs the compiler about the memory requirement and type of arithmetic.

  2. It provides a convenient list of variables for error checking and compilation.


Types of Variables in C

Based on scope and storage, variables in C are classified as:

  1. Local Variables

    • Declared inside a function or block

    • Accessible only within that function or block

  2. Global Variables

    • Declared outside all functions

    • Accessible throughout the program

  3. Static Variables

    • Retain their value between function calls

    • Lifetime exists throughout program execution

  4. Automatic Variables

    • Default local variables

    • Created and destroyed automatically

  5. External Variables

    • Used to share variables across multiple source files


Conclusion

Thus, a variable is a fundamental concept in C programming that allows programmers to store, access, and manipulate data efficiently. Variables simplify memory usage, improve readability, and enable complex computations while hiding the low-level details of memory addressing.







📖 Reference

The content for this subject is prepared by referring to the standard textbook
“Computer Fundamentals and Programming in C”
by Pradip Dey and Manas Ghosh,
Second Edition, Oxford University Press (2018).

The explanation is exam-oriented and strictly aligned with the textbook content.


💙 Footer

Aivette-COI is created with the intention of helping college students learn smartly, revise quickly, and approach exams with confidence.

With love and care,
by Aivette 💙

Comments

Popular posts from this blog

22IMC7Z2 - CONSTITUTION OF INDIA

1.1 Historical Background of the Indian Constitution: The Company Rule & The Crown Rule

1.3 Preamble and Salient Features of the Indian Constitution