Variables

In programming, a variable is a value that can change during the program's execution. It can be either a numeric or non-numeric value. Variables are named memory locations assigned a data type, such as integer or character. Its data type determines the size of a variable.

What you can name your variables depends on your compiler. Here we show you some rules, plus some names you cannot use for variables. When you break the rules, the compiler lets you know by flinging an error at you. To avoid that, try to keep the following guidelines in the back of your head when you create new variables:

Identifier Rules

  • Use character or Underscore ( _ ) as first character of identifier.
  • Only Character, Underscore and Digits (0-9) can be used in Identifier. Other special character (&,$,#,@,!, Etc) are not allowed in identifier.
  • First 31 character of an identifier are significant.
  • No reserve word can be use as identifier.
  • Cannot duplicate any Identifier in the same scope.
  • Space is not allowed in identifier.
  • First 8 characters must be unique i.e compiler treat forgiveme and forgivemenot identifiers as same identifier because of first 8 characters are same.

Variable Declaration

Each variable in your program must be declared and define. In C, declaration is used to assign name an object, such as a variable. Definitions are creating the object. With the few exceptions, a variable is declared and define at the same time.

The way of variable declaration is:



Variable Initialization

We can initialize a variable at the same time when we declare it by including an initializer. When present, the initializer establishes the first value that the variable will contain. To initialize a variable, the assignment operator (=) is used.
Ex.
      int count = 0;

ADVERTISEMENT