- 2071 Chaitra · 2 marks
Constants and variables
Short answer
Constants are fixed values that do not change (integer, real, character, string). They can be defined with #define or const. Variables are named memory locations holding values that can change; they must be declared with a specific data type before use.
Explain constants and variables.
Constants are fixed values that do not change during execution. They are of types integer (25), real (3.14), character ('A') and string ("Hello"). They can be named with #define PI 3.14 or const int DAYS = 7;.
Variables are named memory locations that store data which can change during execution. They must be declared with a type before use: int count = 0; float avg;. Variable names follow the rules of identifiers.
Example:
#include <stdio.h>
#define PI 3.14159
int main(void)
{
const int DAYS = 7; /* constant */
int count = 0; /* variable */
float radius = 2.0f;
count = count + DAYS;
printf("%d %.2f\n", count, PI * radius * radius);
return 0;
}
Output:
7 12.57
✓ Answer checked by Claude Opus 5.5 (high effort)
Related questions
- FORTRAN constants, variables, library functions · Computer Programming, chapter 11
- Penman equation and its variables · Engineering Hydrology, chapter 3