KEMBAR78
C Programming-Lecture Notes-02-Constants.pdf
B.Bhuvaneswaran, AP (SG) / CSE
9791519152
bhuvaneswaran@rajalakshmi.edu.in
Programming Logic
Constants
Constants
Constants Rajalakshmi Engineering College 2
Rajalakshmi Engineering College 2
Character Set
 The character set is a set of valid characters that a language can
recognize.
 A character represents any alphabet, digit or special symbols,
which can be used to form variables, numbers and expressions.
Constants
Constants Rajalakshmi Engineering College 3
Rajalakshmi Engineering College 3
Character Set - Groups
 Letters / Alphabets
 Digits
 Special characters
 White spaces
Character
Set
Letters /
Alphabets
Digits
Special
Characters
White
Spaces
Constants
Constants Rajalakshmi Engineering College 4
Rajalakshmi Engineering College 4
Character Set - Groups
Letters / Alphabets
Uppercase A . . . Z
Lowercase a . . . z
Digits All decimal digits 0 . . . 9
Special Characters
Comma , Ampersand &
Period . Caret ^
Semicolon ; Asterisk *
Colon : Minus sign -
Question mark ? Plus sign +
Apostrophe ‘ Opening angle bracket <
Quotation mark “ (or less than sign)
Exclamation mark ! Closing angle bracket >
Vertical bar | (or greater than sign)
Slash / Left parenthesis (
Backslash  Right parenthesis )
Tilde ~ Left bracket [
Underscore _ Right bracket ]
Dollar sign $ Left brace {
Percent sign % Right brace }
Number sign #
White Spaces Blank space, Horizontal tab, Carriage return, New line, Form feed
Constants
Constants Rajalakshmi Engineering College 5
Rajalakshmi Engineering College 5
Tokens
 In C program, the smallest individual units are known as C tokens.
Constants
Constants Rajalakshmi Engineering College 6
Rajalakshmi Engineering College 6
Types of Tokens
 Keywords
 Identifiers
 Constants
 Strings
 Special symbols
 Operators
Constants
Constants Rajalakshmi Engineering College 7
Rajalakshmi Engineering College 7
Examples
float
while
main
price
10
-12.15
“REC”
“cse”
[ ]
{ }
+ -
* /
Tokens
Keywords Identifiers Constants Strings
Special
Symbols
Operators
Constants
Constants Rajalakshmi Engineering College 8
Rajalakshmi Engineering College 8
C Constants
 Constants in C refer to fixed values that do not change during the
execution of a program.
Constants
Constants Rajalakshmi Engineering College 9
Rajalakshmi Engineering College 9
Integer Constants
 Integer constants are whole numbers (0 through 9) without any
fractional part.
Constants
Constants Rajalakshmi Engineering College 10
Rajalakshmi Engineering College 10
Rules for Integer Constants
 An integer constant must have at least one digit and must not
have a decimal point.
 An integer constant may be positive or negative. If no sign is
preceded an integer constant, it is assumed to be positive.
 Embedded spaces, commas and non-digit characters are not
allowed.
Constants
Constants Rajalakshmi Engineering College 11
Rajalakshmi Engineering College 11
Types of Integer Constants
Constants
Constants Rajalakshmi Engineering College 12
Rajalakshmi Engineering College 12
Decimal Integer Constants
 Decimal integer constants can consist any combinations of digits
from 0 to 9, preceded by an optional + or – sign.
Constants
Constants Rajalakshmi Engineering College 13
Rajalakshmi Engineering College 13
Examples
 Valid Examples:
0 +47 179
-240 22099
 Invalid Examples:
35 750 10,000 $5000 12.55
Constants
Constants Rajalakshmi Engineering College 14
Rajalakshmi Engineering College 14
Octal Integer Constants
 Octal integer constants can consist of any combinations of digits
from 0 to 7.
 However, the first digit must be zero (0), in order to identify the
constant as an octal number.
 For instance, decimal integer 8 will be written as 010 as octal
integer.
Constants
Constants Rajalakshmi Engineering College 15
Rajalakshmi Engineering College 15
Examples
 Valid Examples:
0 047 0240
 Invalid Examples:
01 70 010,000 $05000
012.55 0786
Constants
Constants Rajalakshmi Engineering College 16
Rajalakshmi Engineering College 16
Hexadecimal Integer Constants
 Hexadecimal integer constants can consist of any combination of
digits from 0 to 9 and letters from A to F (either uppercase or
lowercase represents 10 to 15).
 A hexadecimal integer constant must begin with either 0x or 0X.
 For instance, decimal integer 12 will be written as 0XC or 0xc as
hexadecimal integer.
Constants
Constants Rajalakshmi Engineering College 17
Rajalakshmi Engineering College 17
Examples
 Valid Examples:
0x 0X2 0x7A 0xbcd
 Invalid Examples:
0x35 75A 0x10,000 $0x5000
0x12.55 0x7AG
Constants
Constants Rajalakshmi Engineering College 18
Rajalakshmi Engineering College 18
Real Constants
 Real constants are also called as floating point constants. Real
constants are numbers having fractional parts.
Constants
Constants Rajalakshmi Engineering College 19
Rajalakshmi Engineering College 19
Forms of Real Constants
 Fractional form
 Exponential form
Constants
Constants Rajalakshmi Engineering College 20
Rajalakshmi Engineering College 20
Fractional Form
 The fractional form consists of a series of digits representing the
whole part followed by a decimal point and series of digits
representing the floating part.
 The whole part or fractional part can be omitted, but not both.
 The decimal point cannot be omitted.
Constants
Constants Rajalakshmi Engineering College 21
Rajalakshmi Engineering College 21
Rules for Fractional Form
 A real constant must have at least one digit and it must have a
decimal point.
 A real constant could be either positive or negative (default sign is
positive).
 Embedded spaces, commas and non-digit characters are not
allowed.
Constants
Constants Rajalakshmi Engineering College 22
Rajalakshmi Engineering College 22
Examples
 Valid Examples:
0.0002 -0.96 179.47 +31.79
+.2 -.47 179. .99
 Invalid Examples:
12,000.50 31 12.30.45 $1000.75 15 750.25
Constants
Constants Rajalakshmi Engineering College 23
Rajalakshmi Engineering College 23
Exponential Form
 The exponential (or scientific) form of representation of real
constants is usually used if the value of the constant is either too
small or too large.
 In exponential form of representation, the real constant is
represented in two parts namely, mantissa and exponent.
 Syntax:
mantissa e exponent
or
mantissa E exponent
Constants
Constants Rajalakshmi Engineering College 24
Rajalakshmi Engineering College 24
Rules for Exponential Form
 The mantissa and the exponent should be separated by a letter e
(either uppercase or lowercase).
 The mantissa and exponent part may have a positive or negative
sign (default sign is positive).
 The mantissa and exponent part must have at least one digit.
 Embedded spaces, commas are not allowed.
Constants
Constants Rajalakshmi Engineering College 25
Rajalakshmi Engineering College 25
Examples
 Valid Examples:
0.31e2 14e-3 3.1e+5 3.14E4
-1.2E-2
 Invalid Examples:
12,000e2 3.1e 2 3.1E+2.4
Constants
Constants Rajalakshmi Engineering College 26
Rajalakshmi Engineering College 26
Single Character Constants
 A single character constant or a character constant consist of a
single character encloses within apostrophes i.e., single quotes (‘).
Constants
Constants Rajalakshmi Engineering College 27
Rajalakshmi Engineering College 27
Rules for Single Character Constants
 A character constant is a single alphabet, a single digit, a single
special symbol or a blank space.
 The maximum length of a character constant can be one character.
Constants
Constants Rajalakshmi Engineering College 28
Rajalakshmi Engineering College 28
Examples
 Valid Examples:
'B' 'V' '+' '$' ' '
 Invalid Examples:
'abc' '143'
Constants
Constants Rajalakshmi Engineering College 29
Rajalakshmi Engineering College 29
String Constants
 A string constant is a sequence of characters encloses within
double quotation marks ("). Technically, a string is an array of
characters.
 The compiler automatically places the null character (0) at the
end of each such string, so the program can conveniently find the
end.
Constants
Constants Rajalakshmi Engineering College 30
Rajalakshmi Engineering College 30
Rules for String Constants
 The string constant may be letters, numbers, special characters
and blank spaces.
 Every string constant ends up with a null character, which is
automatically assigned.
Constants
Constants Rajalakshmi Engineering College 31
Rajalakshmi Engineering College 31
Examples
 Valid Examples:
"India" "2015" "WELCOME" "*.*"
"B" "$120"
Constants
Constants Rajalakshmi Engineering College 32
Rajalakshmi Engineering College 32
Points to Remember
 A character constant 'B' is not the same as the string constant that
contains the single character "B". The former is the single
character and the later a character string consisting of character B
and 0 (null).
 A character constant as an equivalent integer value, whereas
single character string constant does not have an equivalent
integer value. It occupies two bytes, one for the ASCII code of B
and another for the null character with a value 0, which is used to
terminate strings.
Constants
Constants Rajalakshmi Engineering College 33
Rajalakshmi Engineering College 33
Escape Sequences
 Certain constants are non-printable, which means that they are
not displayed on the screen or in the printer, called as escape
sequences (backslash character constants).
 C supports some special backslash character constants that are
used in output functions.
Constants
Constants Rajalakshmi Engineering College 34
Rajalakshmi Engineering College 34
Escape Sequences
ASCII value Escape sequence Meaning
000 0 Null
007 a Audible alert (bell)
008 b Backspace
009 t Horizontal tab
010 n New line
011 v Vertical tab
012 f Form feed
013 r Carriage return
034 " Double quote
039 ' Single quote
063 ? Question mark
092  Backslash
Thank You

C Programming-Lecture Notes-02-Constants.pdf

  • 1.
    B.Bhuvaneswaran, AP (SG)/ CSE 9791519152 bhuvaneswaran@rajalakshmi.edu.in Programming Logic Constants
  • 2.
    Constants Constants Rajalakshmi EngineeringCollege 2 Rajalakshmi Engineering College 2 Character Set  The character set is a set of valid characters that a language can recognize.  A character represents any alphabet, digit or special symbols, which can be used to form variables, numbers and expressions.
  • 3.
    Constants Constants Rajalakshmi EngineeringCollege 3 Rajalakshmi Engineering College 3 Character Set - Groups  Letters / Alphabets  Digits  Special characters  White spaces Character Set Letters / Alphabets Digits Special Characters White Spaces
  • 4.
    Constants Constants Rajalakshmi EngineeringCollege 4 Rajalakshmi Engineering College 4 Character Set - Groups Letters / Alphabets Uppercase A . . . Z Lowercase a . . . z Digits All decimal digits 0 . . . 9 Special Characters Comma , Ampersand & Period . Caret ^ Semicolon ; Asterisk * Colon : Minus sign - Question mark ? Plus sign + Apostrophe ‘ Opening angle bracket < Quotation mark “ (or less than sign) Exclamation mark ! Closing angle bracket > Vertical bar | (or greater than sign) Slash / Left parenthesis ( Backslash Right parenthesis ) Tilde ~ Left bracket [ Underscore _ Right bracket ] Dollar sign $ Left brace { Percent sign % Right brace } Number sign # White Spaces Blank space, Horizontal tab, Carriage return, New line, Form feed
  • 5.
    Constants Constants Rajalakshmi EngineeringCollege 5 Rajalakshmi Engineering College 5 Tokens  In C program, the smallest individual units are known as C tokens.
  • 6.
    Constants Constants Rajalakshmi EngineeringCollege 6 Rajalakshmi Engineering College 6 Types of Tokens  Keywords  Identifiers  Constants  Strings  Special symbols  Operators
  • 7.
    Constants Constants Rajalakshmi EngineeringCollege 7 Rajalakshmi Engineering College 7 Examples float while main price 10 -12.15 “REC” “cse” [ ] { } + - * / Tokens Keywords Identifiers Constants Strings Special Symbols Operators
  • 8.
    Constants Constants Rajalakshmi EngineeringCollege 8 Rajalakshmi Engineering College 8 C Constants  Constants in C refer to fixed values that do not change during the execution of a program.
  • 9.
    Constants Constants Rajalakshmi EngineeringCollege 9 Rajalakshmi Engineering College 9 Integer Constants  Integer constants are whole numbers (0 through 9) without any fractional part.
  • 10.
    Constants Constants Rajalakshmi EngineeringCollege 10 Rajalakshmi Engineering College 10 Rules for Integer Constants  An integer constant must have at least one digit and must not have a decimal point.  An integer constant may be positive or negative. If no sign is preceded an integer constant, it is assumed to be positive.  Embedded spaces, commas and non-digit characters are not allowed.
  • 11.
    Constants Constants Rajalakshmi EngineeringCollege 11 Rajalakshmi Engineering College 11 Types of Integer Constants
  • 12.
    Constants Constants Rajalakshmi EngineeringCollege 12 Rajalakshmi Engineering College 12 Decimal Integer Constants  Decimal integer constants can consist any combinations of digits from 0 to 9, preceded by an optional + or – sign.
  • 13.
    Constants Constants Rajalakshmi EngineeringCollege 13 Rajalakshmi Engineering College 13 Examples  Valid Examples: 0 +47 179 -240 22099  Invalid Examples: 35 750 10,000 $5000 12.55
  • 14.
    Constants Constants Rajalakshmi EngineeringCollege 14 Rajalakshmi Engineering College 14 Octal Integer Constants  Octal integer constants can consist of any combinations of digits from 0 to 7.  However, the first digit must be zero (0), in order to identify the constant as an octal number.  For instance, decimal integer 8 will be written as 010 as octal integer.
  • 15.
    Constants Constants Rajalakshmi EngineeringCollege 15 Rajalakshmi Engineering College 15 Examples  Valid Examples: 0 047 0240  Invalid Examples: 01 70 010,000 $05000 012.55 0786
  • 16.
    Constants Constants Rajalakshmi EngineeringCollege 16 Rajalakshmi Engineering College 16 Hexadecimal Integer Constants  Hexadecimal integer constants can consist of any combination of digits from 0 to 9 and letters from A to F (either uppercase or lowercase represents 10 to 15).  A hexadecimal integer constant must begin with either 0x or 0X.  For instance, decimal integer 12 will be written as 0XC or 0xc as hexadecimal integer.
  • 17.
    Constants Constants Rajalakshmi EngineeringCollege 17 Rajalakshmi Engineering College 17 Examples  Valid Examples: 0x 0X2 0x7A 0xbcd  Invalid Examples: 0x35 75A 0x10,000 $0x5000 0x12.55 0x7AG
  • 18.
    Constants Constants Rajalakshmi EngineeringCollege 18 Rajalakshmi Engineering College 18 Real Constants  Real constants are also called as floating point constants. Real constants are numbers having fractional parts.
  • 19.
    Constants Constants Rajalakshmi EngineeringCollege 19 Rajalakshmi Engineering College 19 Forms of Real Constants  Fractional form  Exponential form
  • 20.
    Constants Constants Rajalakshmi EngineeringCollege 20 Rajalakshmi Engineering College 20 Fractional Form  The fractional form consists of a series of digits representing the whole part followed by a decimal point and series of digits representing the floating part.  The whole part or fractional part can be omitted, but not both.  The decimal point cannot be omitted.
  • 21.
    Constants Constants Rajalakshmi EngineeringCollege 21 Rajalakshmi Engineering College 21 Rules for Fractional Form  A real constant must have at least one digit and it must have a decimal point.  A real constant could be either positive or negative (default sign is positive).  Embedded spaces, commas and non-digit characters are not allowed.
  • 22.
    Constants Constants Rajalakshmi EngineeringCollege 22 Rajalakshmi Engineering College 22 Examples  Valid Examples: 0.0002 -0.96 179.47 +31.79 +.2 -.47 179. .99  Invalid Examples: 12,000.50 31 12.30.45 $1000.75 15 750.25
  • 23.
    Constants Constants Rajalakshmi EngineeringCollege 23 Rajalakshmi Engineering College 23 Exponential Form  The exponential (or scientific) form of representation of real constants is usually used if the value of the constant is either too small or too large.  In exponential form of representation, the real constant is represented in two parts namely, mantissa and exponent.  Syntax: mantissa e exponent or mantissa E exponent
  • 24.
    Constants Constants Rajalakshmi EngineeringCollege 24 Rajalakshmi Engineering College 24 Rules for Exponential Form  The mantissa and the exponent should be separated by a letter e (either uppercase or lowercase).  The mantissa and exponent part may have a positive or negative sign (default sign is positive).  The mantissa and exponent part must have at least one digit.  Embedded spaces, commas are not allowed.
  • 25.
    Constants Constants Rajalakshmi EngineeringCollege 25 Rajalakshmi Engineering College 25 Examples  Valid Examples: 0.31e2 14e-3 3.1e+5 3.14E4 -1.2E-2  Invalid Examples: 12,000e2 3.1e 2 3.1E+2.4
  • 26.
    Constants Constants Rajalakshmi EngineeringCollege 26 Rajalakshmi Engineering College 26 Single Character Constants  A single character constant or a character constant consist of a single character encloses within apostrophes i.e., single quotes (‘).
  • 27.
    Constants Constants Rajalakshmi EngineeringCollege 27 Rajalakshmi Engineering College 27 Rules for Single Character Constants  A character constant is a single alphabet, a single digit, a single special symbol or a blank space.  The maximum length of a character constant can be one character.
  • 28.
    Constants Constants Rajalakshmi EngineeringCollege 28 Rajalakshmi Engineering College 28 Examples  Valid Examples: 'B' 'V' '+' '$' ' '  Invalid Examples: 'abc' '143'
  • 29.
    Constants Constants Rajalakshmi EngineeringCollege 29 Rajalakshmi Engineering College 29 String Constants  A string constant is a sequence of characters encloses within double quotation marks ("). Technically, a string is an array of characters.  The compiler automatically places the null character (0) at the end of each such string, so the program can conveniently find the end.
  • 30.
    Constants Constants Rajalakshmi EngineeringCollege 30 Rajalakshmi Engineering College 30 Rules for String Constants  The string constant may be letters, numbers, special characters and blank spaces.  Every string constant ends up with a null character, which is automatically assigned.
  • 31.
    Constants Constants Rajalakshmi EngineeringCollege 31 Rajalakshmi Engineering College 31 Examples  Valid Examples: "India" "2015" "WELCOME" "*.*" "B" "$120"
  • 32.
    Constants Constants Rajalakshmi EngineeringCollege 32 Rajalakshmi Engineering College 32 Points to Remember  A character constant 'B' is not the same as the string constant that contains the single character "B". The former is the single character and the later a character string consisting of character B and 0 (null).  A character constant as an equivalent integer value, whereas single character string constant does not have an equivalent integer value. It occupies two bytes, one for the ASCII code of B and another for the null character with a value 0, which is used to terminate strings.
  • 33.
    Constants Constants Rajalakshmi EngineeringCollege 33 Rajalakshmi Engineering College 33 Escape Sequences  Certain constants are non-printable, which means that they are not displayed on the screen or in the printer, called as escape sequences (backslash character constants).  C supports some special backslash character constants that are used in output functions.
  • 34.
    Constants Constants Rajalakshmi EngineeringCollege 34 Rajalakshmi Engineering College 34 Escape Sequences ASCII value Escape sequence Meaning 000 0 Null 007 a Audible alert (bell) 008 b Backspace 009 t Horizontal tab 010 n New line 011 v Vertical tab 012 f Form feed 013 r Carriage return 034 " Double quote 039 ' Single quote 063 ? Question mark 092 Backslash
  • 35.