Understanding Different Types of Constants in Programming
Constants in programming are fixed values that do not change during program execution. They include character, numerical, and string constants. Integer constants can be decimal, octal, or hexadecimal. This article explains each type of constant with examples of decimal, octal, and hexadecimal integers, as well as real constants represented in decimal notation and exponential notation.
Download Presentation
Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
E N D
Presentation Transcript
CONSTANTS Mr.M.Sulthan Ibrahim Associate Professor of Computer Science Hajee Karutha Rowther Howdia College(Autonomous)
CONSTANTS Constants in C refer to fixed values that do not change during the execution of a program
TYPES OF CONSTANTS CONSTANTS Character constant Numerical constant Single character constants Integer constant Real constant string constant
Integer constants An integer constant refers to a sequence of digits. There are three types of integers 1. decimal integer. 2. octal integer. 3. hexadecimal integer
Decimal integers Decimal integers consist of a set of digits, 0 through 9, preceded by an optional or + sign Examples. 123 -321 0 654321 +78
Octal integers Octal integers consist of any combination of digits from the set 0 through 7, with a leading 0. Examples 037 0 0435 0551
Decimal to octal convertion 291 8 8 36 3 4 4 Decimal octal 291 = 0443
Hexadecimal integer A sequence of digits preceded by 0x or 0X is considered as hexadecimal integer. They may also include alphabets A through F or a through f. the letter A through F represent the numbers 10 through 15 Examples 0X2 0x9F 0Xbcd 0x
Decimal to Hexadecimal convertion 16 16 498 31 2 15 1 Decimal hexadecimal 498 = 0X1f2 or 0x1f2
Real constants Numbers containing fractional parts are called real constant or floating point constant (17.543) Real constants are represented in two ways 1. Decimal notation. 2. Exponential or Scientific notation
Decimal notation A whole number followed by decimal point and fractional part is called real constant in decimal notation. Examples 0.00083 -0.75 435.36 +247.0 215. .95
Exponential or Scientific notation A real number may also expressed in exponential notation Example the value 215.65 may be written as 2.1565e2 in exponential notation. Here e2 means multiply by 102
= 2.1565 X 102 215.65 215.65 = 2.1565e2 = 4.51X 10-5= 4.51E-5 0.0000451 6510000000 = 6.51 X 109= 6.51E9
The general form is: mantissa e exponent 2.156 e 2 The mantissa is a real number expressed in decimal notation. The exponent is an integer number with an optional plus or minus sign. The letter e separate the mantissa and the exponent can be written in either lowercase or uppercase.
Single character constants A single character constant contains a single character enclosed within a pair of single quote mark Example x 5 ; Note: the character constant 5 is not the same as the number 5. Character constant have integer values known as ASCII values
String Constants A string constant is a sequence of character enclosed in double quotes. The characters may be letters numbers, special characters and blank space Example Hello 2002 Well Done ?...! 5+3 x Note: A character constant(e.g., x ) is not equivalent to the single character string constant(e.g., x )
Backslash Character Constant C support some special backslash character constants that are used in output function. For example, the symbol \n stands for newline character. Note: each one of them represents one character, although they consist of two characters. These characters combinations are known as escape sequences.
constant meaning \a \b \f \n \r \t \v \ \ \? \\ \0 Audible alert(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 data name that may be used to store a data value. A variable may take different value at different times during execution.
A variable name can be chosen in a meaning full way so as to reflect its function or nature in the program. Examples average height total counter_1 class_strenth
Variable names may consist of lettes,digits, and underscore character, subject to the following conditions. 1. They must begin with a letter. 2. ANSI standard recognizes a length of 31 characters. However, length should not be normally more than eight characters.
3. Uppercase and lowercase are significant. This is, the variable Total is not the same as total or TOTAL 4. It should not be a keyword. 5. White space is not allowed.