ProgrammerGuide.Net | Step by Step Programmer Guide

C-Constants, Variables & Data Types

Constants, Variables & Data Types,

Constants in C refer to fixed values that do not change during the execution of a program .C supports several types of constants as illustrated in fig 2.2

1. Constants

         A. Numeric Constants

                   a. Integer Constants               b. Real constants

         B. Character Constants

                    a. Single character constants             b. String constants

Integer Constants
An integer constants refers to a sequence of digits. There are three types of integers, namely, decimal integer ,octal integer and hexadecimal integer.

Decimal integer consist of set of digit ,0 through 9,preceded by an optional – or + sign. Valid examples of decimal integer contents are:
123     -321   0   654321   +78
Embedded spaces, commas and non-digit characters are not permitted between digits. For example.
15   750   20,000    $1000
are illegal numbers. note that ANSI C supports unary plus which was not defined earlier.

      An octal integer constants consist of any combination of digit from the set 0 through 7,with a leading 0.some example of octal integer are:
037   0   0435   0551
A  sequence of digits preceded by 0x or 0X is considered as hexadecimal integer .They may also include alphabets A through F. The letter A through F represent the number s 10 through15.Following are the examples of  valid hex integers.
0X2    0x9F    0Xbcd      0x
We rarely use octal and hexadecimal numbers in programming.

The largest integer value that can be  stored in machine dependent .It is 32767 on 16-bit  machines and 2,147,483,647 on 32-bit machine. It is also possible to store larger integer constants on these machines by appending qualifiers such as U, L and UL to the constants. For example:

56789Uor 56789u(unsigned integer)
987612347ULor 98761234ul(unsigned long integer)
9876543Lor 98765431l(long integer)

Example for this constants
                    printf (“Integer values\n\n”);
                    printf (“%d  %d  %d\n”,32767,32767+1,32767+10);
                    printf (“\n”);
                    printf (“long integer values\n\n”);
                    printf (“%ld  %ld  %ld\n”,32767L,32767L+1L,32767L+10L);
                Integer values
                32767  -32768   -32759
                Long integer values
                32767  32768  32777
The normal integral constants are obvious: things like 1, 1034 and so on. You can put l or L at the end of an integer constant to force it to be long. To make the constant unsigned, one of u or U can be used to do the job.

Integer constants can be written in hexadecimal by preceding the constant with 0x or 0X and using the upper or lower case letters a, b, c, d, e, f in the usual way.

Be careful about octal constants. They are indicated by starting the number with 0 and only using the digits 0, 1, 2, 3, 4, 5, 6, 7. It is easy to write 015 by accident, or out of habit, and not to realize that it is not in decimal. The mistake is most common with beginners, because experienced C programmers already carry the scars.

Please click here for related products on Amazon!


Add comment

Want to Check Celebrities News?