C - Introduction Variable

Introduction

A variable represents a specific piece of memory.

Every variable in a program has a name, which will correspond to the memory address for the variable.

You use the variable name to store a data value in memory or retrieve the data from the memory.

Naming Variables

The name you give to a variable is called a variable name.

A variable name is a sequence of one or more uppercase or lowercase letters, digits, and underscore characters _ that begin with a letter or _.

Examples of legal variable names are as follows:

abc def Circle rect Radius  diameter  A_M  K_Wool  D678 T001

A variable name must not begin with a digit, so 8_Ball and 6_pack aren't legal names.

A variable name must not include characters other than letters, underscores, and digits.

The following code uses a variable of type int, which is an integer type:

Demo

//  Using a variable
#include <stdio.h>

int main(void)
{
      int salary;      // Declare a variable called salary
      salary = 10000;  // Store 10000 in salary
      printf("My salary is %d.\n", salary);
      return 0;/*from   w w  w .  ja v a2 s .  c o m*/
}

Result

The following statement that identifies the memory that you're using to store your salary is the following:

int salary;// Declare a variable called salary

This statement is called a variable declaration because it declares the name of the variable.

Related Topics