datatype in c++ unit 3 -topic 2

19
Datatype in C++ •Data type is a keyword used to identify type of data. •It is used for storing the input of the program into the main memory (RAM) of the computer by allocating sufficient amount of memory space in the main memory of the computer. •In general every programming language is containing three categories of data types. They are 1.Fundamental or primitive data types 2.Derived data types 3.User defined data types

Upload: mohit-tomar

Post on 13-Feb-2017

57 views

Category:

Education


0 download

TRANSCRIPT

Page 1: Datatype in c++ unit 3 -topic 2

Datatype in C++•Data type is a keyword used to identify type of data.•It is used for storing the input of the program into the main memory (RAM) of the computer by allocating sufficient amount of memory space in the main memory of the computer.•In general every programming language is containing three categories of data types. They are1. Fundamental or primitive data types2. Derived data types3. User defined data types

Page 2: Datatype in c++ unit 3 -topic 2
Page 3: Datatype in c++ unit 3 -topic 2

Primitive data typesThese are the data types whose variable can hold

maximum one value at a time, in C++ language it can be achieve by int, float, double, char.

Page 4: Datatype in c++ unit 3 -topic 2
Page 5: Datatype in c++ unit 3 -topic 2

Derived data types1. These data type are derived from fundamental

data type.

2. Variables of derived data type allow us to store multiple values of same type in one variable but never allows to store multiple values of different types.

Page 6: Datatype in c++ unit 3 -topic 2

User defined data types1. User defined data types related variables allows us

to store multiple values either of same type or different type or both.

2. This is a data type whose variable can hold more than one value of dissimilar type, in C++ language it is achieved by structure.

Page 7: Datatype in c++ unit 3 -topic 2

Data type modifiers • The data type modifiers are listed here:1. signed2. unsigned3. long4. Short• The modifiers signed, unsigned,

long, and short can be applied to integer base types.

• In addition, signed and unsignedcan be applied to char, and long can be applied to double.

Page 8: Datatype in c++ unit 3 -topic 2
Page 9: Datatype in c++ unit 3 -topic 2
Page 10: Datatype in c++ unit 3 -topic 2

Constants/Literals

• Constants refer to fixed values that the program may not alter and they are called literals.

• Constants can be of any of the basic data types and can be divided into Integer Numerals, Floating-Point Numerals, Characters, Strings and Boolean Values.

C++ allows several kinds of literal1. Integer literals2. Floating-point literals3. Boolean literals4. Character literals5. String literals

Page 11: Datatype in c++ unit 3 -topic 2

Integer Constants• An integer literal can be a decimal, octal, or

hexadecimal constant.• A prefix specifies the base or radix: 0x or 0X for

hexadecimal, 0 for octal, and nothing for decimal.• An integer literal can also have a suffix that is a

combination of U and L, for unsigned and long, respectively.

Page 12: Datatype in c++ unit 3 -topic 2

Floating-point Constants• A floating-point literal has an integer part, a decimal

point, a fractional part, and an exponent part.

Page 13: Datatype in c++ unit 3 -topic 2

Boolean Constants• There are two Boolean literals and they are part of

standard C++ keywords:1. A value of true representing true.2. A value of false representing false.• You should not consider the value of true equal to 1

and value of false equal to 0.

Page 14: Datatype in c++ unit 3 -topic 2

Character Constants• Character literals are enclosed in single quotes.

Page 15: Datatype in c++ unit 3 -topic 2

String Constants• String literals are enclosed in double quotes.• A string contains characters that are similar to

character literals: plain characters, escape sequences, and universal characters.

Page 16: Datatype in c++ unit 3 -topic 2

The const Keyword

Page 17: Datatype in c++ unit 3 -topic 2

Variable Declaration Rule in C++• To Declare any variable in C++ you need to follow rules and

regulation of C++ Language, which is given below;• Every variable name should start with alphabets or underscore (_).• No spaces are allowed in variable declaration.• Except underscore (_) no special symbol are allowed in the middle

of the variable declaration.• Maximum length of variable is 8 characters depend on compiler

and operation system.• Every variable name always should exist in the left hand side of

assignment operator.• No keyword should access variable name.Syntax:

Datatype variable_name; int a;

Page 18: Datatype in c++ unit 3 -topic 2

Scope of Variable in C++• In C++ language, a variable can be either of global or

local scope.Global variable

1. Global variables are defined outside of all the functions, generally on top of the program.

2. The global variables will hold their value throughout the life-time of your program.

Local variable3. A local variable is declared within the body of a

function or a block. 4. Local variable only use within the function or block

where it is declare.

Page 19: Datatype in c++ unit 3 -topic 2