egsoftweb@gmail.com
 facebook.com/egsoftweb

Identifier Names & Variables

 07-Apr-2019 |  Admin

Identifier Names:

The names of variables, functions, labels, and various other user-defined items are called identifiers.

The length of these identifiers can vary from one to several characters.

The first character must be a letter or an underscore, and subsequent characters must be either letters, digits or underscores.

Correct

Incorrect

i1

1i

a23     

23a

low_count

low..count

helloWorld

hello!World

correct and incorrect identifier names

 

In C, identifiers may be of any length. However, not all characters will necessarily be significant.

C defines two kinds of identifiers: external and internal.

An external identifier will be involved in an external link process. These identifiers, called external names, include function names and global variable names that are shared between source files. If the identifier is not used in an external link process, then it is internal. This type of identifier is called an internal name and includes the names of local variables. For example, in C89, at least the first 6 characters of an external identifier and at least the first 31 characters of an internal identifier will be significant. C99 has increased these values.

In C99, an external identifier has at least 31 significant characters, and an internal identifier has at least 63 significant characters. As a point of interest, in C++, at least the first 1,024 characters of an identifier are significant. These differences may be important if you are converting a program from C89 to C99, or from C to C++.

In an identifier, upper- and lowercase are treated as distinct. Hence, count, Count, and COUNT are three separate identifiers.

An identifier cannot be the same as a C keyword and should not have the same name as functions that are in the C library.


Variables:

A variable is a named location in memory that is used to hold a value that can be modified by the program.

All variables must be declared before they can be used.

The general form of a declaration is

type variable_list;

Here, type must be a valid data type plus any modifiers, and variable_list may consist of one or more identifier

names separated by commas. Here are some declarations:

int i, j, l;

short int si;

unsigned int ui;

double balance, profit, loss;

In C the name of a variable has nothing to do with its type

 

Types of variables:

Local variables:  Variables that are declared inside a function.

Variables as formal parameters: A function`s parameters are known as variables that will accept the values of the arguments.

Global variables: Global variables are known throughout the program and may be used by any piece of code. Also, they will hold their value throughout the program`s execution. You create global variables by declaring them outside of any function. Any expression may access them, regardless of what block of code that expression is in.

#include <stdio.h>

int count; /* count is global */

void func1(void);

void func2(void);

int main(void)

{

            count = 100;

            func1();

            return 0;

}

void func1(void)

{

            int temp;

            temp = count;

            func2();

            printf("count is %d", count);

            /* will print 100 */

}

void func2(void)

{

            int count;

            for (count = 1; count<10; count++)

                        putchar(`.`);

}

In the above code, the variable count has been declared outside of all functions. Although its declaration occurs before the main( ) function, you could have placed it anywhere before its first use as long as it was not in a function. However, it is usually best to declare global variables at the top of the program.

Although neither main( ) nor func1( ) has declared the variable count, both may use it. func2( ), however, has declared a local variable called count . When func2( ) refers to count, it refers to only its local variable, not the global one. If a global variable and a local variable have the same name, all references to that variable name inside the code block in which the local variable is declared will refer to that local variable and have no effect on the global variable.


References:

[1] C The Complete Reference 4th Ed, Herbert Schildt.