WEL COME BILASPUR UNIVRSITY BILASPUR Department Of Computer
WEL COME
BILASPUR UNIVRSITY, BILASPUR Department Of Computer Science & Application Assignment- Constants In C Guided By: - Presented By: - Mr. Jeetendra sir Ajay Thakur
1. 2. 3. 4. 5. 6. 7. 8. What is constant. Declaring constants Basic types of constant. Integer constant. Real constant. Single character constant. String constant. Backslash character constant.
What is constant : Constants are data values that cannot be changed during the execution of a program. Like variables have a type. In this section, we discuss Boolean , character , integer , real , complex , and string constants. 4
Declaring constants : Declaring a constants allows you to quickly and easily change a value that is used throughout your code simply by changing the declaration. Unlike variables , constants are declared using the c language preprocessor and not the compiler. Here’s the format : #define PI 3. 14 5
Declaring constants : #include<stdio. h> #define PI 3. 14 void main () { float R, area, square; R=2. 5; area = 4*PI*R*R; square =4/3*PI*R*R*R; printf(“%f %f”, area, square); } 6
Types of constant : constants Character constants Numeric constants Single character constants Integer constants String constants Real constants 7
Integer constants : An integer constant refers to a sequence of digits. there three types of integers , namely , decimal integer , octal integer and hexadecimal integer consist of a set of digit. valid example: 123 -312 0 67364 +56 an octal integer constant consists of any combination of digits from the set 0 through 7 with a leading 0. valid example: 037 0 043 0546 a sequence of digits preceded by 0 x or 0 X is considered as hexadecimal integer. Valid example: 0 X 2 0 x 9 F 0 Xbcd 0 x 8
Integer constants : constant valid 698354 L Yes 25, 000 No $255 No 0 X 7 B Yes remarks Represents long integer Comma is not allowed $ symbol is not permitted Hexadecimal integer
Real constants : Mantissa e exponent The mantissa is either a real number expressed in decimal notation or an integer. Example of legal floating-point constants are: 0. 65 e 4 12 e-2 1. 5 e+5 3. 18 E 3 -1. 2 E-1
Real constants : Constant Valid? +5. 0 E 3 Yes 3. 5 e-5 Yes 7. 1 e 4 No 1. 5 E+2. 5 No Remarks ANSI c support unary plus ----=----No white space is permitted Exponent must be an integer
Single char. constants : A single character constant contains a single character enclosed within a pair of single quote marks. Example of charater constants are: ‘ 5’ ‘X’ ‘; ’ ‘ ’ Note that the character constant ‘ 5’ is not the same as the number 5. Character constant have integer values known as ASCII value. For example , the statement Printf(“%d”, ’a’); Would print the number 97, the ASCII value of the letter a.
String Constants : A string constant is a sequence of characters enclosed in double quotes. The characters may be letters , number , special characters and blank space examples are : “Hello!” “ 1987” “well done” “? . . !” “ 5+3” “X” Remember that a character constant is not equivalent to single character string constant. Further , a single character string constant does not have an equivalent integer value while a character constant has an integer value. Character string are often used in programs to build meaningful programs.
Backslash char Constants : C supports some special backslash character constants that are used in output functions. For example , the symbol ‘n’ stands for newline character. A list of such backslash character constant is given in table. Note that each one of them represents one character , although they consist of two characters.
Backslash char Constants : Constant ‘n’ ‘b’ ‘f’ ‘n’ ‘r’ ‘t’ ‘v’ ‘”’ ‘? ’ ‘\’ ‘ ’ Meaning Audible alert (bell) Back space Form feed New line Carriage return Horizontal tab Vertical tab Single quote Double quote Question mark Backslash null
- Slides: 16