Technical Training
C TutorialsTable of Contents
C Programming - Constants and Identifiers
C Special Characters
C String Constants
C Integer and Floating Point Constants
C IdentifiersC Identifiers
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:
- double a = 93.2;
- typedef enum { Line, Triangle, Square } ShapeKind;
- struct shape {
- int num_verticies;
- double *vx, *vy;
- ShapeKind kind;
- };
- int draw_shape( struct shape * );
- float b3x;
- int \u015cocket;
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.
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.
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:
- #include <stdio.h>
- double sum( double a, double b )
- {
- printf( "%s called with %g and %g\n", __func__, a, b );
- return a + b;
- }
- double mult( double a, double b )
- {
- printf( "%s called with %g and %g\n", __func__, a, b );
- return a * b;
- }
- void main()
- {
- printf("(1+2)*(3+4) = %g\n", mult( sum(1,2), sum(3,4) ));
- }
This program outputs:
- sum called with 3 and 4
- sum called with 1 and 2
- mult called with 3 and 7
- (1+2)*(3+4) = 21
You can see that the __func__ variable is set to each function's name on lines 5 and 11.
C Tutorials
- C Programming - An Overview
- C Programming - Data Types : Part 1
- C Programming - Data Types : Part 2
- C Programming - Constants and Identifiers
- C Programming - Operators
- C Programming - Expressions
- C Programming - Managing Input and Output Operations
- C Programming - Decision Making - Branching
- C Programming - Decision Making - Looping
- C Programming - Arrays
- C Programming - Handling of Character String
- C Programming - Functions (Part-I)
- C Programming - Functions (Part-II)
- C Programming - Structures and Unions
- C Programming - Pointers
- C Programming - Dynamic Memory allocation
- C Programming - Linked Lists
- C Doubly Linked Lists
- C Circular Linked Lists
- C Programming - File management in C
- C Language - The Preprocessor
- Call by Value and Call by Reference
- Concept of Pixel in C Graphics
- TSR in C - An Introduction







