Sunday, September 26, 2010

Constants and Variables in 'C'

CONSTANTS
 
INTRODUCTION

A programming language is designed to help process certain kinds of data consiting of numbers, characters and strings to provide useful output known as information. The task of processing of data is done by executing a sequence of precise instructions called a program. These instructions are formed using certain symbols and words according to some rigid rules known as syntax rules( or grammer).

CHARACTER SET SUPPORTED BY 'C'


Letters:                                                           Digits:

Uppercase A to Z                                             All decimal digits 0 to 9
Lowercase a to z

Special Characters:

,
.
:
;
?
'
"
!
|
/
\
~
_
$
%

comma
period
colon
semicolon
question mark
apostrophe
quotation mark
exclamation mark
vertical bar
slash
backslash
tilde
underscore
dollar sign
percent sign
&
^
*
-
+
<
>
(
)
[
]
{
}
#
ampersand
caret
asterisk
minus sign
plus sign
opening angle bracket
closing angle bracket
left parenthesis
right parenthesis
left bracket
right bracket
left brace
right brace
number sign


White Spaces:

Blank Space
Horizontal tab
Carriage return
New line
Form Feed

CONSTANTS

In 'C' it refers to fixed values that do not change during the excection of a program. 'C' supports several types of constants.

  • Numeric Constants
    • Integer Constants
      • Decimal
      • Octal
      • Hexadecimal
    • Real Constants
  • Character Contants
    • Single Character Constants
    • String Constants

INTEGER CONSTANTS

An integer constant refers to a sequence of digits.
  • Decimal integers constant consists of a set of digits i.e 0 through 9, preceded by an optional - or + sign.
       Valid Example
       123
      -321
        4
        0
        99991
        86

       Invalid Example (Not Permitted in 'C')
       45 750
       15,000
       Rs 200

  • Octal Integer constant consist of any combination of digits from the set 0 through 7, with a leading 0.
       Valid Example
         043
         0
         0347
         0551
  • Hexadecimal Integer constant is preceded by OX or Ox, they may contain alphabets from A to F or a to f. The alphabets A to F refers to 10 to 15 in decimal digits. Example of valid hexadecimal integers are

    Valid Example
    OX2
    OX8C
    OXbcd
    Ox
REAL CONSTANTS

Real Constants consists of a fractional part in their representation. Integer constants are inadequate to represent quantities that vary continuously. These quantities are represented by numbers containingfractional parts like 26.082. Example of real constants are

          0.0626
          0.127
        -495.29
        +487.0

Real Numbers can also be represented by exponential notation. The general form for exponential notation is mantissa exponent. The mantissa is either a real number expressed in decimal notation or an integer. The exponent is an integer number with an optional plus or minus sign.


 CHARACTER CONSTANTS


A Single Character constant represent a single character which is enclosed in a pair of quotation symbols.
Example for character constants are

           '5'
           'x'
           ';'
           ' '

All character constants have an equivalent integer value which are called ASCII Values.

A string constant is a set of characters enclosed in double quotation marks. The characters in a string constant sequence may be a alphabet, number, special character and blank space. Example of string constants are

           "Ravi"
           "1234"
           "God Bless"
           "!.....?"
           "expertinc"

BLACKSLASH CHARACTER CONSTANTS(Escape Sequences)

Backslash character constants are special characters used in output functions. Although they contain two characters they represent only one character.For example a symbol '\n' stands for newline character. A list is shown below.


    ConstantsMeaning

    '\a'
    '\b'
    '\f'
    '\n'
    '\r'
    '\t'
    '\v'
    '\''
    '\"'
    '\?'
    '\\'
    '\0'

    audible alet(bell)
    back space
    form feed
    new line
    carriage return
    horizontal tab
    vertical tab
    single quote
    double quote
    question mark
    backslash
    null


    VARIABLES

    A variable is a value that can change any time. It is a memory location used to store a data value. A variable name should be carefully chosen by the programmer so that its use is reflected in a useful way in the entire program. Variable names are case sensitive. Example of variable names are

              Rollno
              number
              Salary
             Emp_name
             average1

    Rules of declaring a variable in 'C'
    • They must always begin with a letter, although some systems permit underscore as the first character.
    • The length of a variable must not be more than 8 characters.
    • White space is not allowed and
    • A variable should not be a Keyword
    • It should not contain any special characters.

    Examples of Invalid Variable names are

            123
            (area)
            6th
           %abc
        
    Variables and Constants Topic Ends Here

    0 comments:

    Post a Comment