Variable in C language

Variable in C language

A variable in C language As we have seen before, an entity that can vary during program execution is called a variable. Variable names are names given to locations in memory. These locations can contain integers, real, or character constants. In any language, the types of variables it can support depend on the types of constants it can handle.

This is because a certain type of variable can hold only the same type of constant. For example, an integer variable can hold only an integer constant, a real variable can only hold a real constant, and a character variable can hold only a character constant.

The rules for constructing different types of constants are different. However, the same set of rules applies for constructing variable names of all types. These rules are set out below.

Rules for constructing Variable in C language names

  • A variable name is any combination of 1 to 31 alphabets, numbers or subgraphs. Some compilers allow variable names whose length could be up to 247 characters long. However, it would be safer to follow the 31 character rule. Do not create unnecessary long variable names as they add to your typing effort.
  • The first character in the variable name must be an alphabet or underscore.
  • Commas or semi-fabricated in a variable name are not allowed.
  • A special symbol cannot be used, except for an underline (as in gross_sal) in a variable name.
Ex.:      si_int

These rules remain the same for all types of primary and secondary variables. Naturally, the question arises … how is Variable in C able to differentiate these variables? This is a simple symptom. Compiler C is able to distinguish between variable names by making it mandatory to declare the type of any variable name that you want to use in a program.

This type declaration is made at the beginning of the program. Here are the examples of type declarations:

Ex.:      int   si, m_hra ;
          float   bassal ;
          char   code ; 

Because the maximum allowable length of a variable name is 31 characters, an enormous number of variable names can be constructed using the rules mentioned above. It is good practice to exploit this huge choice in variable naming using significant variable names.

Thus, if we want to calculate the simple interest, it is always advisable to construct the names of significant variables such prin, roi, noy to represent the Principle, the Interest Rate and the Number of years, rather than using the variables a, b, c.

Keywords in C language

Keywords are words whose meaning has already been explained to compiler C (or in a broad sense to the computer). Keywords cannot be used as variable names because if we do, we try to assign a new meaning to the keyword, which is not allowed by the computer. Some C compilers allow you to build variable names that look exactly like keywords.

However, it would be safer not to mix variable names and keywords. Keywords are also called “Reserved words”. There are only 32 keywords available in Variable in C. Figure 1.5 provides a list of these keywords for your ready reference. A detailed discussion of each of these keywords will be taken up in subsequent chapters, where their use is relevant.

Note that compiler providers (such as Microsoft, Borland, etc.) provide their own keywords, in addition to the ones mentioned above. These include extended keywords such as near, lighthouse, asm, etc. Although the ANSI committee suggested that each such compiler-specific keyword be preceded by two underlines (as in __asm), not every seller complies with this rule.

You can find more C language related topics here

Leave a Comment