Programming

C – Constant and Types of Constant

Constants, also called literals, are the actual representation of fixed values used in the program. All the numeric representations without fractional (such as 0, 20, 81563) are considered as the integer constant and all the numeric representation that has fractional part or exponential part (such as 5.6, 3.3, 3.55e55) are known as floating point constants.

The alphabetic, numeric or other symbolic characters are characters constant.    Character constants are represented by enclosing character inside single quote (such as ‘a’, ‘%’, ‘+’).

The collection of multiple character constants is called string constants which are enclosed in double-quotes (i.e “Ram”, “Sita”, etc).    Different constants used in C are as follows; 

  1. Integer Constant
    1. Decimal Constant
    2. Hexadecimal Constant
    3. Octal Constant
  2. Floating Point Constant
  3. Character Constant
  4. String Constant

#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<conio.h> //above header files //below symbolic constant
#define PI 3.1412
#define TRUE 1
#define FALSE 0  

Integer Constant

The integer constants are the fixed numeric representation that does not have a fractional part.

Integer constants may be positive or negative numbers without a fractional part. There are three types of integer literals.

I) Decimal Literals:

Decimal literals are the integer numbers represented with base 10 number system. No point or comma is allowed in the decimal literal. Some examples of decimal literals are 124, +54, -54, 84884 etc.

II) Hexadecimal Literals

Hexadecimal numbers are the integer numbers that have base sixteen. These numbers use digits from 0 to 9 and alphabets from A to F (or a to f).

Hexadecimal numbers are written using 0x before actual digits. Some examples of hexadecimal literals are 0xBe, 0x3, 0xCA3, 0xBBA etc. 

III) Octal Literals

They are the integer numbers with base eight. Octal numbers use digital from 0 to 7. Octal numbers are written using 0 before the octal digits. Some examples of octal integers are 043, 01743, 0203457 etc. 

Floating Point Constant

The fixed numeric representation that has fractional part or exponential part is called floating point constant or real constant.

The floating-point literals can be represented in fractional form or exponential form.

No commas are allowed in a floating-point number. In fractional form, the number has integer part and fractional part separated by a point and in exponential form, the number has an integer part, a fractional part, and exponential part.

If the fractional part of the integer part has the value 0, then we may omit it, but we must use the point to indicate that it is a floating-point constant.

The integer part and fractional part are separated by a decimal point and the exponential part is separated by letter ‘e’. No space is allowed in any numbers. Some floating-point literals are 4. 71, 43, 4., 4.5e-23 etc.

Character Constant

A character represented within single quotes denotes a character constant. Following are some examples.

‘a’, ‘z’, ‘x’ etc. Actually, character literals are internally represented as integer numbers. Most of the computer use ASCII character set in which each character is assigned 7-bit code but character set may be implementation-defined.

Internally each character is stored as an 8-bit integer. There are some special characters written with (backslash) as the escape character.

For example n for a new line, t for horizontal tab, b as backspace, r carriage return, f form fee, an alert, \ backslash, ‘ single quote,  ” double quote.

String Constant

A string literal is a character sequence enclosed withing double quote. If double quote (“) or single quote (‘) are to be part of the string they should be preceded  by backslash. Following are the example of the string literal. 

"Republic New Nepal" To mean just republic new Nepal. 
"Ravi' S wife" to mean Ravi's wife.
"He said "Health is Wealth"" to mean He said "health is Wealth". 

A string literal contains one more character, that is null character ” at the end that it actually appears to have. Another special escape character can be used within string as; 

printf("stringt for test an");

String can be broken into two lines by white space character as

printf("........................................"
"...................................................");

The two-line string will be concatenated by the compiler to make a single string. Even if we write two string in a single line as “Donald” Duck” it will mean “DonaldDuck” to the compiler.

Tuts

About Author

Tutsmaster.org provides tutorials related to tech and programmings. We are also setting up a community for the users and students.