DATA TYPES AND
VARIABLES IN C++
Datatypes in C++
◦ Data type refer to the kind of data to be stored in variable.
◦ There are two types of data type
◦ Fundamentals/Built in datatype.
◦ Derived datatype/user defined datatype.
Fundamentals datatype
◦ Fundamental data types are the data types which are predefined in the language and can be directly used
to declare a variable in C++.
Example are:
◦ Int ,float,char,double,bool etc.
Syntax:
◦ Datatype varible_name;
User defined datatype:
◦ Data types that are derived from fundamental data types are derived types. For example:
arrays, pointers, function types, structures, etc.
Data Type modifier in C++.
• As the name suggests ,these are used to modify primitive data type i.e int,char,float.
• They are used as a prefix to the primitive data types.
• These modifiers change the size and the type of values that a primitive data type can hold.
Examples are:
singed int, unsigned int, short int etc.
Variables in C++
◦ Variables are containers for storing data values.
◦ Int -stores integers (whole numbers), without decimals, such as 123 or -123.
◦ double- stores floating point numbers, with decimals, such as 19.99 or -19.99
◦ char-Stores single characters, such as 'a' or 'B'. Char values are surrounded by
single quotes.
◦ string-stores text, such as "Hello World". String values are surrounded by double
quotes
◦ Bool-Stores values with two states: true or false.
Declaring variable and initializing
variable
◦ To create a variable, specify the type .
◦ Syntax; datatype varible_name;
◦ Example: int number;
Initializing a variable:
To create a variable, specify the type and assign it a value:
◦ Syntax; datatype varible_name=value;
◦ Example: int number=10;
a
10
20
b
Memory
Value
Value
Variable/Memory
location
Variable/Memory
location
4byte
4byte
Taking size of 4
byte in memory
Memory Allocation:
Implantation:
• Checking size of datatype.
• Declaring variable.
• Initializing variable.
• Taking value from user.
• Putting invalid value in a
datatype

Data Types and variables in C++.pptx

  • 1.
  • 2.
    Datatypes in C++ ◦Data type refer to the kind of data to be stored in variable. ◦ There are two types of data type ◦ Fundamentals/Built in datatype. ◦ Derived datatype/user defined datatype.
  • 3.
    Fundamentals datatype ◦ Fundamentaldata types are the data types which are predefined in the language and can be directly used to declare a variable in C++. Example are: ◦ Int ,float,char,double,bool etc. Syntax: ◦ Datatype varible_name;
  • 4.
    User defined datatype: ◦Data types that are derived from fundamental data types are derived types. For example: arrays, pointers, function types, structures, etc.
  • 5.
    Data Type modifierin C++. • As the name suggests ,these are used to modify primitive data type i.e int,char,float. • They are used as a prefix to the primitive data types. • These modifiers change the size and the type of values that a primitive data type can hold. Examples are: singed int, unsigned int, short int etc.
  • 7.
    Variables in C++ ◦Variables are containers for storing data values. ◦ Int -stores integers (whole numbers), without decimals, such as 123 or -123. ◦ double- stores floating point numbers, with decimals, such as 19.99 or -19.99 ◦ char-Stores single characters, such as 'a' or 'B'. Char values are surrounded by single quotes. ◦ string-stores text, such as "Hello World". String values are surrounded by double quotes ◦ Bool-Stores values with two states: true or false.
  • 8.
    Declaring variable andinitializing variable ◦ To create a variable, specify the type . ◦ Syntax; datatype varible_name; ◦ Example: int number; Initializing a variable: To create a variable, specify the type and assign it a value: ◦ Syntax; datatype varible_name=value; ◦ Example: int number=10;
  • 9.
  • 10.
    Implantation: • Checking sizeof datatype. • Declaring variable. • Initializing variable. • Taking value from user. • Putting invalid value in a datatype