Monday, 25 October 2010

Constants

Constants

Constant means fixed data that can't be changed during the execution of a program. 

Integer constants

Integer constants are the numeric constants(constant associated with number) without any fractional part or exponential part. There are three types of integer constants in C language: decimal constant(base 10), octal constant(base 8) and hexadecimal constant(base 16) .

Decimal digits: 0 1 2 3 4 5 6 7 8 9

Octal digits: 0 1 2 3 4 5 6 7

Hexadecimal digits: 0 1 2 3 4 5 6 7 8 9 A B C D E F.

For example:

Decimal constants: 0, -9, 22 etc

Octal constants: 021, 077, 033 etc

Hexadecimal constants: 0x7f, 0x2a, 0x521 etc

Notes:

1. You can use small caps a, b, c, d, e, f instead of uppercase letters while writing a hexadecimal constant.

2. Every octal constant starts with 0 and hexadecimal constant starts with 0x in C programming.

Floating-point constants

Floating point constants are the numeric constants that has either fractional form or exponent form. For example:

-2.0

0.0000234

-0.22E-5

Note:Here, E-5 represents 10-5. Thus, -0.22E-5 = -0.0000022.

Character constants

Character constants are the constant which use single quotation around characters. For example: 'a', 'l', 'm', 'F' etc.


Escape Sequences or Character Constants

Suppose we want to print out Hello, on one line, followed by world! on the next line. One could attempt to represent the string to be printed as a single literal as follows:

#include <stdio.h>
void main() {
    printf("Hyd,
Chennai!");
}

This is not valid in C, since a string literal may not span multiple logical source lines
So solution is escape sequences

#include <stdio.h>
void main() {
    printf("Hyd \n Chennai!");
}


Escape sequence   Hex value in ASCII Character represented
\v                                0B                         Vertical Tab
\t                                09                          Horizontal Tab
\r                                0D                          Carriage Return
\nnn                               any                          The byte whose numerical value is given by nnn                                                                                       interpreted as an octal number
\n                               0A                           Newline (Line Feed); see notes below
\f                               0C                          Formfeed
\b                               08                          Backspace
\a                               07                          Alert (Beep, Bell) (added in C89)[1]
\\                               5C                          Backslash
\?                               3F                          Question mark
\'                               27                          Single quotation mark
\"                               22                          Double quotation mark
\0                                                                      Null character

It is also treated as  Constants
Sometimes, it is necessary to use newline(enter), tab, quotation mark etc. in the program which either cannot be typed or has special meaning in C programming. In such cases, escape sequence are used.

For example: \n is used for newline. The backslash( \ ) causes "escape" from the normal way the characters are interpreted by the compiler.
Escape Sequences

You may like the following posts:


No comments:

Post a Comment