c++ basics tutorial 5
DESCRIPTION
C++ Basics Tutorial 5. Constants. Literal Constants Defined Constants Declared Constants. Topics Covered. Most easy to see and most obvious constants. Literal constants. Integer Numerals Floating-Point Numerals Boolean literals Character literals String literals. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: C++ Basics Tutorial 5](https://reader035.vdocuments.us/reader035/viewer/2022062222/56816049550346895dcf7122/html5/thumbnails/1.jpg)
C++ Basics Tutorial 5Constants
![Page 2: C++ Basics Tutorial 5](https://reader035.vdocuments.us/reader035/viewer/2022062222/56816049550346895dcf7122/html5/thumbnails/2.jpg)
Topics Covered
• Literal Constants• Defined Constants• Declared Constants
![Page 3: C++ Basics Tutorial 5](https://reader035.vdocuments.us/reader035/viewer/2022062222/56816049550346895dcf7122/html5/thumbnails/3.jpg)
Literal constants
• Most easy to see and most obvious constants
![Page 4: C++ Basics Tutorial 5](https://reader035.vdocuments.us/reader035/viewer/2022062222/56816049550346895dcf7122/html5/thumbnails/4.jpg)
Types of literal constants
• Integer Numerals• Floating-Point Numerals• Boolean literals• Character literals• String literals
![Page 5: C++ Basics Tutorial 5](https://reader035.vdocuments.us/reader035/viewer/2022062222/56816049550346895dcf7122/html5/thumbnails/5.jpg)
Integer Numerals
• A number without decimal points (duh!)• Examples: 23, 56, 8…
• In C++, expressing numerical constants does not require any special character like “”.
• Integer numerals can be defined in other numeral forms like octal or hexadecimal.• 255 Decimal Value• 0377 Octal value• 0xff Hexadecimal Value
All represent same number
![Page 6: C++ Basics Tutorial 5](https://reader035.vdocuments.us/reader035/viewer/2022062222/56816049550346895dcf7122/html5/thumbnails/6.jpg)
Decimal, Octal, hexadecimal, signed and unsigned representation
• To denote an octal number(Base 8 number) start the number with 0 (zero)
• To denote a hexadecimal value(base 16 number) start with 0 x (zero “x”)
• Write normally for decimal numerical(base 10) system value.• Force int to be unsigned by adding u(or U) at the end of
number. (Ex: 19U) or l(or L) to make it long( 19L or 19UL)
![Page 7: C++ Basics Tutorial 5](https://reader035.vdocuments.us/reader035/viewer/2022062222/56816049550346895dcf7122/html5/thumbnails/7.jpg)
Floating Point literals
• To represent number with decimal or exponents• A decimal point “.” or a “e” can be added to represent the
number, where e means to the “power by 10” to number after e. • Also can have both “e” and “.”• Example: 3.14 Value of Pi
5.97e24(Mass of earth) = 5.97 x 10 ^ 24 or 1.67e-27(Mass of proton) = 1.67 x 10 ^ -27• Force number to be long double add L or l, to force number to
be float add f or F in the end of the number• E or e both are same. C++ is not case sensitive in this case.
![Page 8: C++ Basics Tutorial 5](https://reader035.vdocuments.us/reader035/viewer/2022062222/56816049550346895dcf7122/html5/thumbnails/8.jpg)
Boolean literals
• Only two boolean literals in C++: true or false.• Can be represented by bool data type.
![Page 9: C++ Basics Tutorial 5](https://reader035.vdocuments.us/reader035/viewer/2022062222/56816049550346895dcf7122/html5/thumbnails/9.jpg)
Character literals
• One character. Example: ‘a’, ‘b’, ‘A’ etc…• To represent character literals we put them inside single
quotes. This is done to differentiate them from possible variable identifiers that we might define in the program.
• If you just write 1 it is numerical literal. But ‘1’ makes it character literal.
• ‘a’ is a character literal where as just a is a variable identifier named a.
![Page 10: C++ Basics Tutorial 5](https://reader035.vdocuments.us/reader035/viewer/2022062222/56816049550346895dcf7122/html5/thumbnails/10.jpg)
String literals
• Combination of characters.• Inside double quotes “”.• Example “Dean”
![Page 11: C++ Basics Tutorial 5](https://reader035.vdocuments.us/reader035/viewer/2022062222/56816049550346895dcf7122/html5/thumbnails/11.jpg)
Escape Characters
• Characters and string both can have a special character called escape character.
• Impossible or at least difficult to express otherwise• Precede by a backslash(\) and a character. • Example: \n New Line, \t Tab • ‘\n’ or ‘\t’ or “LineOne\nLineTwo\nLineThree”• “\”DoubleQuote\”” = “DoubleQuote” when you print.
![Page 12: C++ Basics Tutorial 5](https://reader035.vdocuments.us/reader035/viewer/2022062222/56816049550346895dcf7122/html5/thumbnails/12.jpg)
Few list of escape characters:
• \n Newline• \t tab• \r carriage return• \v vertical tab• \b backspace• \f form feed• \a alert beep• \’ single quote(‘)• \” Double quote(“)• \? question mark(?)• \\ backslash(\)
![Page 13: C++ Basics Tutorial 5](https://reader035.vdocuments.us/reader035/viewer/2022062222/56816049550346895dcf7122/html5/thumbnails/13.jpg)
Defined constants
• Using something called preprocessor directive• Use #define preprocessor directive• Define constant that will be used frequently.• Example: #define PI 3.14159#define NEWLINE ‘\n’• This now have defined two constants called PI and NEWLINE. Now
you can use PI and NEWLINE as other constants we learned earlier. • Due to #define the C++ compiler literally replaces all the
occurrences of PI and NEWLINE with assigned value(3.14159 or ‘\n’)
![Page 14: C++ Basics Tutorial 5](https://reader035.vdocuments.us/reader035/viewer/2022062222/56816049550346895dcf7122/html5/thumbnails/14.jpg)
Directive
• A directive is not a C++ statement. It is interpreted by the preprocess that happens before even looking at the code of the program. So these directive does not need to have a semicolon in the end.
• Directives start with #. Applies to #include as well.
![Page 15: C++ Basics Tutorial 5](https://reader035.vdocuments.us/reader035/viewer/2022062222/56816049550346895dcf7122/html5/thumbnails/15.jpg)
Declared Constants (const)
• Using const prefix with specific data type you make a variable unchangeable throughout the program.• Example:
const int a = 23;const char newline = ‘\n’
• The are completely regular variables except that you cannot modify these after you initialize.
![Page 16: C++ Basics Tutorial 5](https://reader035.vdocuments.us/reader035/viewer/2022062222/56816049550346895dcf7122/html5/thumbnails/16.jpg)
Please Rate Comment and subscribe
• Next Chapter: We will talk about operators• Then: Basic input/ output.• End of Basics.
• Then to Control structures / Functions