This blog is under construction

Sunday, 4 March 2012

What is an identifier?

What is an identifier?
An identifier is a set of characters used to name variables, functions, macros and enums.

Valid characters in C language identifiers:
Alpha characters[a-z][A-Z]
Decimal numbers[0-9]
Underscore (_)
Dollar symbol($)

Valid and invalid identifiers in C language:
Numeric character should not be used as the first character of identifer.  Space is not allowed in the name of the identifier.  Below are some of the invalid identifier.

Example:
9num, 1val, #val

Identifiers are case sensitive.  Consider the below example,
num
NUM
nUM
Num
All the above identifiers are not same, but unique.

First letter of an identifier can be $ or _.  Below are few valid identifiers

Example:
$, _, $num, _num, num

Note:
$ cannot be used as the first character of function name.

Examples for valid identifiers in c:
Variable names: $, _, $val, _val, v1, v1aa
Function names: add, sub, $add, _add, sub9
Macros        : PI, PI9, pi9, $pi, _pi

 
  #include <stdio.h>
  #define $pi 3.14  // pi is an identifer
  void _calc_area(int radius) { // calc_area and radius are identifiers
        printf("Area of circle: %f\n", $pi * radius * radius);
        return;
  }

  int main() {
        int $, _, radius; // $, _, radius are identifiers
        _ = 5;
        $ = 10;
        radius = 10;
        printf("Value of _: %d\n", _);
        printf("Value of $: %d\n", $);
        _calc_area(radius);  // calc_area & radius are identifiers
        return 0;
  }

  Output:
  jp@jp-VirtualBox:~/$ ./a.out
  Value of _: 5
  Value of $: 10
  Area of circle: 314.000000


$, _, radius, $pi, _calc_area are the identifiers used in the above sample program.


No comments:

Post a Comment