Exforsys

Home arrow Technical Training arrow C Tutorials

C Identifiers

Page 5 of 5
Author: Exforsys Inc.     Published on: 2nd Mar 2006    |   Last Updated on: 6th Jul 2011

C Programming - Constants and Identifiers

Identifiers

An identifier is a name. It can be the name of a variable, function, a structure or union, a member of a struct, union or enum, a typedef name, a macro name or a macro variable. Some examples:

Sample Code
  1. double a = 93.2;
  2. typedef enum { Line, Triangle, Square } ShapeKind;
  3. struct shape {
  4.      int num_verticies;
  5.      double *vx, *vy;
  6.      ShapeKind kind;
  7. };
  8. int draw_shape( struct shape * );
  9. float b3x;
  10. int \u015cocket;
Copyright exforsys.com


In the code above, a, Line, Triangle, Square, ShapeKind, num_verticies, vx, vy, kind, draw_shape, b3x and \u015cocket are all identifiers. That last one is an example of using universal character codes in identifiers.

Ads

There are some rules you must follow when creating an identifier. An identifier is a sequence of one or more characters. It must start with a non-digit character; it can use upper and lower case a-z characters or universal character codes (discussed in the character constants section) or an underscore character (_). After the first character, digits 0-9 may also be used.

Upper and lower case alphabetic characters are distinct – an identifier Ac is different from ac. Even though it is technically possible to start an identifier with an underscore (_) character, you should avoid doing that. Identifiers starting with underscores are reserved for future use.

Different identifiers within the same scope must be unique. For each identifier, the compiler must determine if it is the same as one seen before or if it is a new identifier. This is usually done by looking at a certain number of characters at the beginning of the name. Exactly how many characters are compared is up to the implementation; the C standard only specifies the minimum number to compare. In addition, the number of characters to compare may be different for internal and external identifiers.

Recall that an external identifier is one that is (maybe implicitly) declared with extern storage class. The C standard says that at least 31 initial characters of the identifier must be compared. In addition, if you use universal character codes in these identifiers, each of those characters may count as either 6 or 10 characters (depending on the code).

For non-external identifiers the C standard says at least 63 initial characters must be compared. For these identifiers a universal character code counts as 1 character.

An identifier also cannot be one of the C keywords. These are words like if, then, while, for, etc. that are part of the C language. Here is a list of C keywords:

auto enum restrict unsigned break extern return void case float short volatile char for signed while const goto sizeof _Bool continue if static _Complex default inline struct _Imaginary do int switch double long typedef else register union

Choosing an identifier name should be done carefully. In these examples we have been using names like a1, b, and so on – short and not very descriptive. In actual code, identifiers should strike a balance between being easy to type and being descriptive.
There are various conventions programmers follow when creating identifiers.

For example, if an identifier represents two (or more) words such as the draw_shape() function above, some people will prefer to call it drawshape(), while others will use drawShape() or DrawShape(), or Draw_Shape(). Also, some programmers will use all uppercase identifiers for constants. These are just conventions some people follow, the C language itself does not force any rules on identifiers except for the ones discussed above.

Ads

There is one predefined identifier that the compiler defines in functions: __func__. This is a character array containing the name of the function. It is useful to know the name of the function sometimes, especially when printing diagnostic messages:

Sample Code
  1.    #include <stdio.h>
  2.    
  3.    double sum( double a, double b )
  4.    {
  5.            printf( "%s called with %g and %g\n", __func__, a, b );
  6.            return a + b;
  7.    }
  8.    
  9.    double mult( double a, double b )
  10.   {
  11.           printf( "%s called with %g and %g\n", __func__, a, b );
  12.           return a * b;
  13.   }
  14.  
  15.   void main()
  16.   {
  17.           printf("(1+2)*(3+4) = %g\n", mult( sum(1,2), sum(3,4) ));
  18.   }
Copyright exforsys.com


This program outputs:

Sample Code
  1. sum called with 3 and 4
  2. sum called with 1 and 2
  3. mult called with 3 and 7
  4. (1+2)*(3+4) = 21
Copyright exforsys.com


You can see that the __func__ variable is set to each function's name on lines 5 and 11.



 
This tutorial is part of a C Tutorials tutorial series. Read it from the beginning and learn yourself.

C Tutorials

 

Comments