This blog consists of C and C++ programs. C and C++ programming is the basics to learn any programming language. Most of the programs are provided with their respective outputs. This blog also contains Data Structures programs using Object-Oriented Programming (C++). Some of the best books for learning C and C++ programming are also mentioned.

Friday 27 September 2013

CONSTANTS IN C

CONSTANTS IN C:

A constant is of numeric or non-numeric type. It can be a number, a character or a character string that can be used as a value in a program. As the name implies, the value of a constant cannot be modified. A constant is immutable. Numeric data is primarily made up of numbers and can include decimal points. Non-numeric data may be composed of numbers, letters, blanks and any special characters supported by the system. In other words, non-numeric data consists of alphanumeric characters. A non-numeric data can be called as a literal. Constants are characterized by having a value and type. Numeric constants are of three types:
  • integer constant
  • floating-point constant
  • character constant

Integer Constant
An integer constant is a decimal number (base 10) that represents an integral value (the whole number). It comprises of the digits 0 to 9.

If an integer constant begins with the letters 0x or 0X, it is a hexadecimal (base 16) constant. If it begins with 0 then it is an octal (base 8) constant. Otherwise it is assumed to be decimal.
·         23, 36 and 948 are decimal constants
·         0x1C, 0XAB, and 0x23 are hexadecimal constants
·         071, 023, and 035 are octal constants

Integer constants are positive unless they are preceded by a minus sign and hence –18 is also a valid integer constant. Special characters are not allowed in an integer constant. The constant 2,345 is an invalid integer constant because it contains the special character.

Real Constant
Real constants are also known as floating-point constants. A floating-point constant is a signed real number. It includes integer portion, a decimal point, fractional portion and an exponent. While representing a floating point constant, either the digits before the decimal point (the integer portion) or the digits after the decimal point (the fractional portion) can be omitted but not both. The decimal point can be omitted if an exponent is included. An exponent is represented in powers of 10 in decimal system.

Example:
·         2.5, 5.521, 3.14
·         58.64 is a valid floating-point (real) constant. It can be represented in exponent form as follows:
·         5.864E1 => 5.864 X 101 => 58.64
·         5864E-2 => 5864 X 10-2 => 58.64
·         0.5864e2 => 0.5864 X 102 => 58.64

The letter E or e is used to represent the floating-point constant in exponent form.

Character Constant
A character is a letter, numeral or special symbol, which can be handled by the computer system. These available symbols define the system’s character set. Enclosing a single character from the system’s character set within single quotation marks forms a character constant. The characters used in C language are grouped into three classes.
  • Alphabetic characters: a, b, c, …., z, A, B, C, ….., Z
  • Numeric characters: 0 through 9
  • Special characters: + - * / % # = , . ‘ “ ( ) [ ] :

(i)                 Single Character Constants:
A character constant is a single character.  Characters are also represented with a single digit or a single special symbol or white space enclosed within a pair of single quote marks.
Example: ‘1’, ‘a’, ‘+’, ‘-‘


(ii)               String Constants:
A string constant or a string literal is a sequence of characters from the system’s character set, enclosed in double quotes. By default, the null character ‘\0’ is assumed as the last character in a string literal. The string may be a combination of all kinds of symbols.

Example: “hello”, “rose”, “23454”, “r”

“hello” is a valid string literal. The actual number of characters in this string literal is 6 including the null character at the last. The null character is invisible here. Six bytes are required to store this string in memory. However, the physical length of this string is 5 characters.

Note:
Since two single quotes are used to represent the character constant, how do we represent the single quote itself as a character constant? It is not possible to represent a single quote character enclosed between two single quotes, that is, the representation ‘’’ is invalid.

An escape sequence may be used to represent a single quote as a character constant. Character combinations consisting of a backslash \ followed by a letter are called escape sequences.

‘\’’ is a valid single quote character constant.

Similarly some nonprintable characters are represented using escape sequence characters.

Examples:
‘\a’ Bell (beep)
‘\b’ Backspace
‘\f’ Form feed
‘\r’ Carriage return
‘\n’ New line
‘\0’ null character

To represent the backslash itself as a character, two backslashes are used (‘\\’).

To have a double quote itself as a character in the string constant, an escape sequence ‘\”’ is used.

 Rules for Constructing Integer Constants
1.      An integer constant must have at least one digit.
2.      It must not have a decimal point.
3.      It can be either positive or negative.
4.      If no sign precedes an integer constant it is assumed to be positive.
5.      No commas or blanks are allowed within an integer constant.
6.      The allowable range for integer constants is -32768 to 32767.
Ex.: 426, +782 , -8000 , -7605

Rules for Constructing Real Constants
1.      A real constant must have at least one digit.
2.      It must have a decimal point.
3.      It could be either positive or negative.
4.      Default sign is positive.
5.      No commas or blanks are allowed within a real constant.
Ex.: +325.34 , 426.0 , -32.76 , -48.5792

Rules for Constructing Character Constants
1.      A character constant is a single alphabet, a single digit or a single special symbol enclosed within single inverted commas. Both the inverted commas should point to the left. For example, ’A’ is a valid character constant whereas ‘A’ is not.
2.      The maximum length of a character constant can be 1 character.
Ex.: 'A', 'I', '5', '='

No comments:

Post a Comment