C - Variables and Constant

In C programming, variables are used to store and manipulate data. A variable is a named memory location that holds a value of a specific data type. Here are the key points to understand about variables in C:

Variable Declaration:

Before using a variable in C, you need to declare it. A variable declaration specifies the data type and name of the variable. For example:

int age;      // Declaration of an integer variable named "age"
float salary; // Declaration of a floating-point variable named "salary"

Variable Initialization:

Variables can be initialized at the time of declaration by assigning an initial value. For example:

int count = 0;      // Initialization of an integer variable "count" with 0
float pi = 3.14;   // Initialization of a floating-point variable "pi" with 3.14

Variable Assignment:

Once a variable is declared, you can assign a value to it using the assignment operator (=). For example:

age = 25;     // Assigning a value of 25 to the variable "age"
salary = 5000.50; // Assigning a value of 5000.50 to the variable "salary"

Variable Names:

  • Variable names in C must follow certain rules:
  • Variable names can contain letters, digits, and underscores.
  • The first character of a variable name must be a letter or an underscore.
  • C is case-sensitive, so "age" and "Age" are considered different variables.

Scope of Variables:

The scope of a variable determines its visibility within a program. Variables can be declared at different scopes, such as:

  • Global scope: Variables declared outside any function and can be accessed throughout the program.
  • Local scope: Variables declared within a function and can only be accessed within that function.

Constants:

In addition to variables, C also supports constants, which are values that cannot be changed during program execution. Constants are typically declared using the "const" keyword. For example:

const float PI = 3.14159;   // Declaration of a constant named "PI" with a value of 3.14159