0% found this document useful (0 votes)
17 views

Lecture 3 - Variables Constants and Data Types

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Lecture 3 - Variables Constants and Data Types

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

Lecture 3

Variables Constants and Data types


Objectives
• Explain the character set of C++
• Clarify different types of C++ tokens
• Give explanations of the keywords and
identifiers
• Differentiate constants and variables
• Clarify basic data types of C++
• Explain how to declare variables
• Give explanations of how to assign values to
variables
2
Character Set
• The characters in C++ are grouped into the
following categories:
– Letters
• Upper case A…Z
• Lower case a…z

– Digits
• All decimal digits 0…9

– Special characters

3
Special Characters
, Comma \ Back slash + Plus
. Period ~ Tilde < Less than
; Semicolon _ Underscore > Greater than
: Colon $ Dollar sign ( Left parenthesis
? Question mark % Percent sign ) Right parenthesis
‘ Apostrophe # Number sign [ Left bracket
“ Quotation mark & Ampersand ] Right bracket
! Exclamation mark ^ Caret { Left brace
| Vertical bar * Asterisk } Right brace
/ Slash - Minus

4
C++ Tokens
• The smallest individual units in a program are
known as TOKENS.

• C++ has the following tokens:


– Keywords
– Identifiers
– Constants
– Strings
– Operators

5
Reserved Keywords
alignas const for private throw
alignof constexpr friend protected true
and const_cast goto public try
and_eq continue if register typedef
asm decltype inline reinterpret_cast typeid
auto default int return typename
bitand delete long short union
bitor do mutable signed unsigned
bool double namespace sizeof using
break dynamic_cast new static virtual
case else noexcept static_assert void
catch enum not static_cast volatile
char explicit not_eq struct wchar_t
char16_t export nullptr switch while
char32_t extern operator template xor
class false or this xor_eq
compl float or_eq thread_local
6
Identifiers
• Identifiers refer to the names of variables,
functions, arrays, classes etc. created by the
programmer.

• Rules for naming identifiers


– Only alphabetic characters, digits and underscores
are permitted.
– The name cannot start with a digit.
– Uppercase and lowercase letters are distinct.
– A declared keyword cannot be used as a variable
name.
7
Constants
Constants refer to the fixed values that do not change
during the execution of a program.

Constants

String Numeric Character


Constants Constants Constants

Integer Floating Point


Constants Constants
8
Integer Constants
• Integer constant refers to a sequence of digits.

• There are three types

• Decimal
– Decimal integers consists of a set of digits, 0
through 9, proceeded by an optional – or + sign.
– Embedded spaces, commas, and non-digit
characters are not permitted between digits.
– Example: 123 and -134
9
Integer Constants cont.…
• Octal
– An octal integer consists of any combination of
digits from the set 0 through 7.
– Example: 037 and 0552
• Hexadecimal
– An hexadecimal integer consists of any
combination of digits from the set 0 through 15.
– They may also include alphabets A through F or a
through f. The letters A through F represent the
numbers 10 to 15 respectively.
– Example: 9F and 4A
10
Floating Point Constants
• Floating point numbers are used to represent
real-time quantities having decimal values.

• It is possible to omit digits before the decimal


point or digits after the decimal point.

• Example: 0.005, +2.67, 215., and .78

11
Character Constants
• A single character constant contains a single
character enclosed within a Single Quote
marks.
– Example: ‘5’ and ‘x’

• A string constant is a set of sequence of


characters enclosed in Double Quotes.
• The characters may be letters, numbers, special
characters and blank spaces.
– Example: “Hello” and “10 + 5”
Backslash Character Constants
Character Name Description
\n new line To print output in a new line
\r carriage return Control will be at the beginning of the
output.
\t Horizontal tab Next output will print after tabulation
\' single quotes(') Will print the single quotes
\" double quotes (") Will print the double quotes
\? question (?) Will print the question mark
\\ backslash Will print the backslash
Defining Symbolic Constants
• A constant value can be created using const
qualifier.

• The general syntax:


– const data type identifier = constant value;
• Example:
– const int r = 45;

• It is also possible to declare constant using


#define.

• The general syntax:


– #define symbolic-name value of constant
• Example: #define number 100
An Example with Constant
#include<iostream>
using namespace std;

int main()
{
#define w 23
const int r = 45;
cout<<"Constant value W = "<<w<<endl;
cout<<"Constant value r = "<<r<<endl;
cout<<"Sum of the two values = "<<w+r;
}
Data Types
Data Types

User defined data types Built-in Derived Data Types


Structure Data Types Array
Union Function
Class Pointer
Enumeration

Integer Types char Floating point


Data Types

int float double


Character Data Types
• They can represent a single character, such as
'X' or '@'.

• char – 1 byte
Integer Data Types
• Integers can store whole numbers, such as 5 or 125.

• There are different types of integers.


• int – 2 bytes
• unsigned int – 2 bytes
• signed int – 2 bytes
• short int - 2 bytes
• unsigned short int – 2 bytes
• signed short int – 2 bytes
• long int – 4 bytes
• signed long int – 4 bytes
• unsigned long int – 4 bytes
Floating Point Types
• Floating point represent real values, such as 3.14 or 0.01,
with different levels of precision.

• float – 4 bytes
• double – 8 bytes
• long double – 10 bytes

• Floating point numbers are stored in 32-bits, with 6 digits of


precision.
• If float data type is not sufficient for more accurate values,
the type double can be used.
• A double data type number uses 64-bits giving a precision
of 14 digits.
• To extend the precision further, we may use long double
which uses 80 bits.
Variables
• A variable is a data name that may be used to
store a data value. It may take different values at
different times during execution.

• Variable names may consist of letters, digits and


the underscore character.

• The variable name should be short and


meaningful.

• Example:
– name, marks, mark1, max_mark
Declaration of Variables
• If there are many variables of the same data type, it is
possible to declare them as a single statement.
• Example:
– int a, b, c, d;

• The above declaration is same as the following:


• Example:
– int a;
– int b;
– int c;
– int d;
Example of Variables
• Example: Sum of the two numbers
#include <iostream>
using namespace std;

int main ()
{
int num1, num2;
int add;

num1 = 15;
num2 = 10;
add = num1 + num2;
cout <<"Sum of num1 and num2 = "<<add;
}
Initialization of Variables
• It is possible for a variable to have a specific
value from the moment it is declared.
• Values can be initialize to variables using the
assignment operator =
• There are three ways to initialize variables in
C++.
• The first is known as c-like initialization
• The general syntax:
– type identifier = initial_value;
– Example: int year = 2014;
Initialization of Variables
• A second method is constructor initialization that
encloses the initial value between parentheses.
• General syntax:
– type identifier (initial_value);
– Example: int year (2014);

• A third method is uniform initialization by using


curly braces { }
• General syntax:
– type identifier {initial_value};
– Example: int year {2014};
Example of Initialization
• Example: Initialization of values using three
methods.

#include <iostream>
using namespace std;

int main ()
{
int num1=21;
int num2(75);
int num3{15};
cout <<num1<<","<<num2<<" and "<<num3;
}
Initialization of Variables
• It is possible to separate the declaration of
variables and initialization of values to
variable by using the c-like initialization
method.

• Example:
– int num1, num2, num3;
– num1 = 23;
– num2 = 56;
– num3 = 5
Initialization of Variables
• It is also possible to initialize more than one variable in
one statement using multiple assignment operators.

• The general syntax:


– type variable1, variable2, variable3;
– variable1 = variable2 = variable3 = value;
• Example:
– int a, b, c;
– a = b = c = 0;

• It is also possible to assign different values to the


variables
• Example:
– int a = 10, b = 5;
Example of Initialization
• Example: variable declaration and initialization in
separate statements.

#include <iostream>
using namespace std;

int main ()
{
int num1, num2, num3;
num1 = 21;
num2 = 75;
num3 = 15;
cout <<num1<<","<<num2<<" and "<<num3;
}
What is the Output?
#include <iostream>
using namespace std;

int main ()
{
int a = 10;
double b = 10.00;
signed short int c = -10;
double sum;
sum= a+b+c;
cout<< sum;

} 10 29
What is the Output?
#include <iostream>
using namespace std;

int main ()
{
int a =10;
double b = -10.24;
signed short int c = 10;
double sum;
sum = a+b+c;
cout<< sum;

} 9.76 30
What is the Output?
#include <iostream>
using namespace std;

int main ()
{
int a = -10;
double b = -10.24;
signed short int c = -10;
double sum;
sum = a+b+c;
cout<< sum;

} -30.24 31
What is the Output?
#include <iostream>
using namespace std;

int main ()
{
int a = -10;
double b = -10.24;
signed short int c = -10;
unsigned int sum;
sum = a+b+c;
cout<<sum;

} Garbage Value 32
What is the Output?
#include <iostream>
using namespace std;

int main ()
{
int a = -10;
double b = -10.24;
signed short int c = -10;
signed int sum;
sum = a+b+c;
cout<< sum;

} -30 33
THANK YOU

34

You might also like