Lecture 3 - Variables Constants and Data Types
Lecture 3 - Variables Constants and Data Types
– 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.
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.
Constants
• 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.
11
Character Constants
• A single character constant contains a single
character enclosed within a Single Quote
marks.
– Example: ‘5’ and ‘x’
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
• char – 1 byte
Integer Data Types
• Integers can store whole numbers, such as 5 or 125.
• float – 4 bytes
• double – 8 bytes
• long double – 10 bytes
• 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;
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);
#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.
#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