Variables in C Programming Language

Notes:

Variables in C Programming Language

Variable:
Variable is a named memory location, whose value can change during the execution of a program.

Constant:
Constant is a named memory location, whose value never changes during the execution of a program.

i.e. constants stay constant whereas variables vary.

Declaring and initializing variables in C:

W.K.T. In order to store and process different types of data & data values computer uses its memory (RAM). To allocate a chunk of memory and access it within a program, we need to declare a variable or a constant. Declaring a variable or a constant: means allocating a memory location for some data

In order to change the value in a variable or constant, we need to initialize or assign a value to it.
Initializing a variable or a constant: means assigning the initial value to that allocated memory location

Syntax for declaring and initializing variables:
// declaration of a single variable
datatype nameOfVariable;
Ex:
int playerScore;

Note: By default the value of a variable is garbage value

// initialization of a single variable
nameOfVariable = value;
Ex:
playerScore = 0;

// declaration and initialization of a single variable
datatype nameOfVariable = value;
Ex:
int playerScore = 0;

// declaration of multiple variables
datatype nameOfVariable1, nameOfVariable2,...;
Ex:
int playerScore, playerHealth;

// initialization of a single variable
nameOfVariable1 = value;
nameOfVariable2 = value;
Ex:
playerScore = 0;
playerHealth = 100;

// declaration and initialization of multiple variables
datatype nameOfVariable1 = value, nameOfVariable2 = value,…;
Ex:
int playerScore=0, playerHealth=100;

Example code:
#include <stdio.h>
int main()
{
/*
int playerScore;
printf("player score= %d\n",playerScore); // 2

playerScore = 0;
printf("player score= %d\n",playerScore); // 0

int playerScore = 0;
printf("player score= %d\n",playerScore); // 0

int playerScore, playerHealth;
printf("player score= %d\n",playerScore); // 2
printf("player health= %d\n",playerHealth); // 80

playerScore =0;
playerHealth=100;
printf("player score= %d\n",playerScore); // 0
printf("player health= %d\n",playerHealth); // 100

int playerScore=0, playerHealth=100;
printf("player score= %d\n",playerScore); // 0
printf("player health= %d\n",playerHealth); // 100

*/ int playerScore = 0;
printf("player score= %d\n",playerScore); // 0

playerScore = 10;
printf("player score= %d\n",playerScore); // 10

return 0;
}