Understanding Constants and Variables in Programming
Classified in Computers
Written on in
English with a size of 1.94 KB
Constants and Variables
Constants
Constants are quantities whose values do not change during program execution. They may be numeric, character, or string.
Numeric Constants
Numeric constants are of two types: integer and floating-point numbers.
- Integer Constants: Represent values that are counted, such as the number of students in a class. Examples include 7145, -234, and 26.
- Floating-point Constants: Used to represent values that are measured, such as height (e.g., 166.75 cm) or weight (e.g., 82.6 kg).
Character and String Constants
- Character Constant: A symbol from the C character set (digits 0-9, letters A-Z, a-z, punctuation, and special symbols). These are enclosed in single quotes, such as 'a' or 's'.
- String Constant: A sequence of characters enclosed within double quotes, such as "Hello Ahmed".
Variables
A variable is a symbolic name that represents a value that can change during the execution of a program. A variable has a name and holds data of specific types. The data held in a variable is called its value.
Types of Variables
- Numeric Variables: Used to represent numeric values (integers or floating-point). Examples include sum, avg, length, salary, and marks.
- Character Variables: Represent character values or strings of characters. Examples include name, city, and gender.
Memory and Variables
When a variable is used in a computer program, the computer associates it with a particular memory location. The value of a variable at any time is the value stored in that associated memory location. Variables allow the same space in memory to hold different values at different times.