Uploaded by hey2

2.C++-Variables- data -typesIdentifiers

advertisement
1
Lecture note
: Computer Language C++
Lesson
: 2 Variables/ data types/Identifiers
Year
: 2019 Semester 1
Classes
: Automation & Robotic Technology
Electronic Technology
Electrical Technology
Mechatronic Technology
2
C++ Data Types
As explained in the Variables chapter, a variable in C++ must be a specified
data type:
Example
int myNum = 5;
// Integer (whole number)
float myFloatNum = 5.99;
// Floating point number
double myDoubleNum = 9.98; // Floating point number
char myLetter = 'D';
// Character
bool myBoolean= true;
//Boolean
string myText = "Hello";
// String
Data
Type
Size
Description
Example
int myNum= 5;
int
4 bytes Stores whole numbers, without
decimals
float
float myFloatNum = 5.99;
4 bytes Stores fractional numbers,
containing one or more
decimals. Sufficient for storing 7
decimal digits
double
8 bytes Stores fractional numbers,
containing one or more
decimals. Sufficient for storing
15 decimal digits
boolean 1 byte
Stores true or false values
char
Stores a single
character/letter/number, or
ASCII values
1 byte
double myDoubleNum = 9.98;
bool myBoolean= true;
boolean exam= true;
char myLetter = 'D';
string myText = "Hello";
C++ Identifiers
C++ identifiers in a program are used to refer to the name of the variables,
functions,classes, arrays, or other user-defined data types created by the programmer. They
are the basic requirement of any language. Every language has its own rules for naming the
identifiers.
3
C++ Variable
A variable is a name of memory location. It is used to store data. Its value can be changed
and it can be reused many times.
It is a way to represent memory location through symbol so that it can be easily identified.
syntax to declare a variable:
Method 1
dataType VariableName=value or expression;
Method 2
int x = 5;
Method 2
dataType VariableName;
VariableName = value or expression;
int x ;
x = 5;
The example of declaring variable is given below:
1.
int x;
2.
float y;
3.
char z;
Here, x, y, z are variables and int, float, char are data types.
We can also provide values while declaring the variables as given below:
4.
5.
6.
int x=5,b=10; //declaring 2 variable of integer type
float f=30.8;
char c='A';
Rules for defining variables
1.
2.
3.
4.
5.
A variable can have alphabets(a_z, A-Z), digits (0-9) and underscore (_).
A variable name can start with alphabet and underscore only. It can't start with digit.
No white space is allowed within variable name.
A variable name must not be any reserved word or keyword e.g. char, float , void etc.
Variable names are case-sensitive. (eg: age, Age, AGE are 3 variable names)
Valid variable names:
int a;
int _ab;
int a30;
Invalid variable names:
int 4; // can't start with digit
int basic salary; // space
int double; // reserved word in c++
4
C++ Keywords
A keyword is a reserved word. You cannot use it as a variable name, constant name etc.
auto
break
case
char
const
continue
default
do
double
else
enum
extern
float
for
goto
if
int
long
register
return
short
signed
sizeof
static
struct
switch
typedef
union
unsigned
void
volatile
while
asm
dynamic_cast
namespace
reinterpret_cast
bool
explicit
new
static_cast
false
catch
operator
template
friend
private
class
this
inline
public
throw
const_cast
delete
mutable
protected
true
try
typeid
typename
using
virtual
wchar_t
Download