The alphabet, numbers, and special symbols when properly combined from constant,
variable, and keywords. Let us see what are ‘constant’ and ‘variable’ in C. A constant is an entity that does not change, whereas, a variable is an entity that may change.
We normally perform numerous computations throughout every programme. The computer’s memory is where the calculations’ outcomes are kept. The computer’s memory is made up of millions of cells, just like human memory. These memory cells hold the determined values. These memory cells, also known as m memory locations, have names to make it simple to retrieve and use these values. The names supplied to these locations are referred to as variable names since the value recorded in each location is subject to change. Take the illustration in the figure as an example.
Here, 3 is kept in a memory location and given the name x. Since a memory region may only retain one value at a time, we then allocated a new value of 3, which is 3.
Since the location whose name is x can hold different values at different times x is known as a variable. As against this,3 or 5 do not change, hence known as constants.
Types of C constant
C constant can be divided into two major categories:
PrimaryConstants
SecondaryConstants
These constants are further categorized as shown in the figure.
At this stage, we would restrict our discussion to only Primary constants, namely, Integer, Real, and Character constants. Let us see the details of each of these constants. For constructing these different types of constants, certain rules have been laid down. These rules are as under:
Integer Constant Construction Rules
(a)A minimum of one digit is required for an integer constant.
(b) There can be no decimal point.
Both positive and bad outcomes are possible.
(d) If an integer constant has no sign before it, it is taken to be positive.
(e)An integer constant cannot contain commas or blank spaces.
For integer constants, the acceptable range is 32768 to 32767.
In actuality, the compiler determines the integer constant’s range. The range for a 16-bit compiler is -32768 to 32767 for Turbo C or Turbo C++. In fact, the range would be considerably bigger with a 32-bit compiler. How exactly do you define a 16-bit or 32-bit compiler, and how does the range of an Integer constant relate to the type of compiler, etc. Till that time it would be assumed that we are working with a 16-bit compiler.
Ex.: 426 , +782, -8000 , -7605
Regulations for Building Real Constants
Floating Point constants are a common name for real constants. There are two ways to express the real constant: in fractional form and in exponential form.
When creating real constants in fractional form, the following guidelines must be followed:
A real constant must have at least one digit, according to (a).
(b) A decimal point is required.
(c) It can be either favourable or unfavourable.
(d) Positive by default.
No commas or blank spaces are permitted inside of a real constant.
Ex.: +325.34 , 426.0 , -32.76 , -48.5792
Guidelines for Building Character Constants
(a). A single alphabet, one digit, or one unique special symbol wrapped in a single pair of inverted commas is referred to as a character constant. Both of these commas are inverted and ought to point to the left. For instance, whereas ‘A’ is not a valid character constant, ‘A’ is.
(b). A character constant can have a maximum length of one character.
Ex.: ‘A’ , ‘I’ , ‘5’
Types of C variable
As we previously observed, a variable name is a name provided to a location in memory that may change during the execution of a programme. Constants in these areas may be character, real, or integer. Any language’s ability to support a certain set of constants determines the kinds of variables that can be supported by that language. This is so that a specific type of variable can only include other variables of the same type. For Example, A character variable can only hold a character constant, while a real variable can only hold a real constant. An integer variable can only hold an integer constant.
Different sorts of constants are constructed according to different rules. However, for constructing variable names of all the types, the same set of rules applies. The rule is given below.
Guidelines for Writing Variable Names
(a).A variable name is made up of 1 to 31 letters, numbers, or underscores. Some compilers permit variable names with a maximum length of 247 characters. However, it would be safer to follow the 31-character rule. Avoid making variable names too long, as this increases the amount of typing required.
(b).The variable name must begin with an alphabetic character or an underscore. (c). A variable name cannot contain any commas or spaces.
(d). A variable name can only contain an underscore (such as in gross_sal) and no other special characters.
Ex.: si_int , m_hra , pop_e_89
For all varieties of primary and secondary variables, this rule is constant. Of course, the next query is, “How is C able to distinguish between the variable?” This is a rather easy situation. By requiring you to define the types of any variable names you want to use in a programme, the C compiler is able to distinguish between the variable name and the variables names. These examples of declaration statements are made at the beginning of the programme as this form of declaration.
Ex.: int si,m_hra; float bassal; char code;
Since, the maximum allowable length of a variable name is 31 characters, an enormous
number of variable names can be constructed using the above-mentioned rules. It is a good practice to exploit this enormous choice in naming variables by using meaningful variables names.
thus, if we want to calculate simple interest, it is always advisable to construct meaningful variable names like prin,roi,noy to represent Principle, Rate of interest, and Number of years rather than using the variables a,b,c.
Keywords in C
Keywords are words whose meaning has already been communicated to the C compiler (or, in general, to the computer). These words cannot be used as variable names since doing so would violate the computer’s prohibition against assigning new meanings to keywords. You can create variable names for C programmes that mimic keywords using some compilers. It would be safer to keep the names of the variables and the keywords separate, though.
‘Reserved words’ is another name for the keywords.
Types of C Keywords
There are only 32 keywords available in C.
auto | double | int | struct |
break | else | long | switch |
case | enum | register | typedef |
char | extern | return | union |
continue | for | signed | void |
do | if | static | while |
default | goto | sizeof | volatile |
const | float | short | unsigned |