Table of Contents
KEY TAKEAWAYS
- Every variable in C is stored at a specific memory address that can be retrieved using the
&operator - The data type determines how many bytes a variable occupies and how the stored bits are interpreted
- Pointers store memory addresses and allow indirect access to variables
- Understanding memory layout is essential for debugging, optimization, and embedded programming
What are Variables in C?
Variables in C are a symbolic name given to a memory location where a value can be stored for later use. It represents a specific piece of data that can change as the program runs. Variables have three key attributes:- Name: A unique identifier for the variable, typically following the rules of C’s naming conventions.
- Type: The kind of data that the variable can hold, such as int (integer), float (floating-point number), or char (character).
- Value: The actual data stored in the variable.
Memory Locations and Variables in C:
To understand variables, you need to first know a bit about computer memory. Imagine it to be like a bank with a huge number of lockers. Each locker is a memory location and has an address just like a locker has a locker number.When you declare variables in C or C++, you are giving a locker a name too. So now you call your locker by its name. Supposeint num=20;So your locker is named ‘num’ and it has a location in the memory with a locker number or ‘address’ say 5 and you store 20 rupees in it.
How Variables in C Relate to Memory Locations:
Each variable in C is associated with a specific memory location. The following points outline this relationship:- Declaration: When you declare a variable, the compiler reserves a memory location with an address and size based on the variable’s type.
- Assignment: When you assign a value to a variable, the value is stored in the associated memory location.
- Access: When you use a variable in an expression, the compiler retrieves the value from the memory location.
- Modification: When you update a variable’s value, the new value is written to the same memory location, replacing the old value.
Variable Naming Rules:
Variable naming rules in C are an essential aspect of writing clear, maintainable, and error-free code. Following these rules ensures that your variable names are valid and easily understandable by others. Here are the key rules for naming variables in C:- Begin with a letter or underscore: Variable names must start with a letter (either uppercase or lowercase) or an underscore (_). They cannot begin with a number or any other special characters.
- Use alphanumeric characters and underscores: After the initial letter or underscore, variable names can contain any combination of uppercase letters (A-Z), lowercase letters (a-z), digits (0-9), and underscores (_).
- Case sensitivity: C is a case-sensitive language, meaning that variable names with different letter cases are considered distinct. For example, ‘myVariable’, ‘MyVariable’, and ‘MYVARIABLE’ are all treated as separate variables.
- No reserved words: Avoid using C language keywords (such as ‘int’, ‘float’, ‘if’, ‘else’, ‘for’, etc.) as variable names, as these words are reserved for specific purposes within the language.
- Meaningful and descriptive names: Choose variable names that clearly indicate their purpose or the type of data they store. This makes your code more readable and easier to understand. For example, use ‘age’ or ‘salary’ instead of ambiguous names like ‘a’ or ‘s’.
- Keep names concise: While it’s important to choose descriptive variable names, try to keep them concise to avoid clutter and improve readability. Strike a balance between clarity and brevity.
- Consistent naming conventions: Adopt a consistent naming convention throughout your codebase. Common conventions include camelCase (e.g., ‘myVariableName’), snake_case (e.g., ‘my_variable_name’), and PascalCase (e.g., ‘MyVariableName’). Pick a style that suits your preferences and stick to it.

Vivek Bhageria — Lead Firmware R&D Engineer, 12+ years. Ex-Bosch (automotive powertrain), MusicTribe (real-time audio), medical devices. M.Tech BITS Pilani. I write at NerdyElectronics — practical, register-level embedded systems for engineers who want to understand what’s actually happening under the hood.






![Memory Layout of a C Structure: Padding, Alignment, and sizeof Memory layout diagram of a C struct room array with 3 elements r[0] r[1] r[2], showing how members temperature luminosity soundLevel and humidity are stored at consecutive memory addresses 700 through 744, with the struct definition containing int and float members](https://nerdyelectronics.com/wp-content/uploads/2020/06/structure_memory_layout_nerdyelectronics-1-e1591528678954.png)
