My Blog
Articles related to programming, computer science, technology and research.

22/04/2016 Categories: C Programming. No Comments on Variables in C

In the programs, generally we need to store values in the memory and perform operations on those values. This can be achieved in C, by the concept of variables. A variable is a placeholder for holding a value in the main memory (RAM). As the name implies, the value in the variable can change at any point of execution of the program. For using variables in our programs, there are essentially two steps:

1) Declare the variable

2) Initialize the variable

 

Declaring a Variable

Before using a variable in the program, we have to declare the variable. The syntax for declaring a variable in a program is as shown below:

variable-decl

The “type” in the above syntax represents the data type. The “variable-name” is the identifier. There are certain rules that must be followed while writing the variable name. They are as follows:

  1. A variable name must always start with an alphabet (letter) or an underscore ( _ ).
  2. The variable name must not be more than 31 characters. The suggested length of a variable name is 8 characters.
  3. C is case sensitive. So, the variable name “average” is different from “AVERAGE”.
  4. Keywords must not be used for declaring variables.
  5. White spaces are not allowed within the variable name.

 

Initializing a Variable

After declaring the variable, we can assign a value to the variable. This process of assigning a value to the variable is known as initialization. Syntax for initializing a variable is as shown below:

variable-init

The value we assign to the variable depends on the data type of the variable.

Example:

variable-init-ex

The declaration and initialization can be combined into a single line as shown below:

var-init-decl

 

Related Links:

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll Up