Thursday, June 7, 2012

Constants and Variables

Constants

        The C language is composed of seven basic types of code: Constants, Variables, Operators, Punctuations,Keywords, Functions and Labels – collectively known as Tokens.

A constant is a named storage location in the memory where the compiler keeps unchanged data during
program execution. Constants are of two types: Literal Constants and Symbolic Constants.
A literal constant is a value that is directly used in the source code wherever it is needed.
        E.g. int max = 100; integer constant
               float tax = 0.05; floating point constant.
               float multiplier = 2.03E-2 floating point constant (in scientific notation)

Integer constants can be written in three different notations.

  • An integer constant starting with any digit other than 0 is treated as a decimal integer. It can   contain a leading + or -.
  • A constant starting with a 0 is treated as an octal integer. It can contain a leading + or -.
  • A constant starting with a 0x or 0X is treated as a Hexadecimal integer. It can contain digits from    0to 9 the letters A to F and a leading + or -.

Symbolic Constants

       A symbolic constant is a constant that is represented by a symbol (name) in a program.
A non numeric constant may consist of letters, digits, and other characters.
The literals enclosed in single quotes are referred to as character values. Others those enclosed in 
double quotes are called character strings. In C you can use a name to represent a constant. The value
associated with a constant cannot be changed or reassigned.
      Eg: ‘a’, ‘b’, ‘,’, ‘;’                                - character constants.
           “Hello”, “Information”, “31/12/1999” - string constants.
Numeric constants in C are made up of digits and can include decimal points and contain letters and do 
not have single or double quotes.
Constants can be defined by using by using the # define construct.
      Eg: # define a 10
           # define ch ‘a’
           # define msg “End of Program”

Variable 

      A variable is a named memory location in the computer’s memory where different values of a certain type of data can be stored.

Variable Names

In C, a name given to a variable must satisfy the following rules.
  • The name can contain letters, digits and the underscore character.
  • The first character of the name must be an alphabet or an underscore. However the use of an underscore is not recommended.
  • C is case sensitive. So C treats X and x as two different variables.
  • Keywords cannot be used as variable names
C compilers allow long variable names. However most of the C compilers recognize the first 32 characters.
In C / C++ it is necessary to declare variables before they are used in a program. When a variable is
declared it is designated as belonging to one of the data types used in C. The compiler allocates memory for the variable according to its data type.

Variable Declaration

Eg. /* Adding two integers */
           #include <stdio.h>
           #define msg “End of Program”
           int main()
           {
                  int a,b,c;
                  a=2;
                  b=3;
                  c=a+b;
                  printf(“%d%s%d%s%d\n”,a,”+”,b,”=”,c);
                  puts(msg);
            }

        /* Adding Two Floating Point Numbers */
           #include <stdio.h>
           #define msg "End of Program"
           #include <conio.h>
           int main()
          { 
                    int a ;
                    float b;
                    a=123;
                    b=123;
                      printf("%5d\n",a);
                      printf("%+05d\n",a);
                      printf("%.5f\n",b);
                      puts(msg);
           }

Initializing Variables 

Initializing is the process of assigning a variable its first value.


No comments:

Post a Comment