You are on page 1of 14

SNS COLLEGE OF TECHNOLOGY

Coimbatore-35.
An Autonomous Institution

COURSE NAME : OBJECT ORIENTED PROGRAMMING USING C++

II YEAR/ III SEMESTER

UNIT – I Introduction

Topic: Data types

Mr.M.Karthick
Assistant Professor
Department of Computer Science and Engineering
08/23/2021 Datatypes / OOP/M.Karthick/CSE/SNSCT
C++ Tokens
A token is the smallest element of a program that is meaningful to the compiler.
Tokens can be classified as follows:
• Keywords
• Identifiers
• Constants
• Strings
• Special Symbols
• Operators

08/23/2021 Datatypes / OOP/M.Karthick/CSE/SNSCT 2


Keyword
• Keywords are pre-defined or reserved words in a programming language.
• Each keyword is meant to perform a specific function in a program. 

C language supports 32 keywords which While in C++ there are 31 additional


are given below: keywords other than C Keywords they are:

08/23/2021 Datatypes / OOP/M.Karthick/CSE/SNSCT 3


Identifiers
• Identifiers are used as the general terminology for naming of
variables, functions and arrays. 
There are certain rules that should be followed while naming
• int a; identifiers:
•They must begin with a letter or underscore(_).
• int add(); •They must consist of only letters, digits, or underscore. No other
• int a[10]; special character is allowed.
•It should not be a keyword.
•It must not contain white space.
•It should be up to 31 characters long as only first 31 characters are
significant.

08/23/2021 Datatypes / OOP/M.Karthick/CSE/SNSCT 4


Constants:
• Constants are also like normal variables. But, only difference is, their values can not be modified by
the program once they are defined.
• Constants refer to fixed values.
• They are also called as literals.
• Constants may belong to any of the data type.
• Syntax:
• const data_type variable_name; (or) 
• const data_type *variable_name;

• Types of Constants:
• Integer constants – Example: 0, 1, 1218, 12482
• Real or Floating point constants – Example: 0.0, 1203.03, 30486.184
• Octal & Hexadecimal constants – Example: octal: (013 )8 = (11)10, Hexadecimal: (013)16 = (19)10
• Character constants -Example: ‘a’, ‘A’, ‘z’
• String constants -Example: “SNSCT”

08/23/2021 Datatypes / OOP/M.Karthick/CSE/SNSCT 5


Strings
• Strings are array of characters ended with a null character (‘\0’).This
null character indicates the end of the string. Strings are always
enclosed in double quotes. Whereas, a character is enclosed in single
quotes 
• Example
• char string[20] = {‘s’, ’n’, ‘s’, ‘c’, ‘s’, ‘t’, ‘\0’};
• char string[20] = “snsct”;
• char string [] = “snsct”;

08/23/2021 Datatypes / OOP/M.Karthick/CSE/SNSCT 6


Special Symbols:
• [] () {}, ; * = #

08/23/2021 Datatypes / OOP/M.Karthick/CSE/SNSCT 7


Operators: 
• Operators are symbols that triggers an action when applied to C variables and other objects. The data items
on which operators act upon are called operands.
Depending on the number of operands that an operator can act upon, operators can be classified as follows:
• Unary Operators: Those operators that require only single operand to act upon are known as unary
operators.For Example increment and decrement operators
• Binary Operators: Those operators that require two operands to act upon are called binary operators. Binary
operators are classified into :
• Arithmetic operators
• Relational Operators
• Logical Operators
• Assignment Operators
• Conditional Operators
• Bitwise Operators
• Ternary Operators: These operators requires three operands to act upon. For Example Conditional
operator(?:).

08/23/2021 Datatypes / OOP/M.Karthick/CSE/SNSCT 8


C++ Data Types
• All variables use data-type during declaration to restrict the type of
data to be stored.
• Data types are used to tell the variables the type of data it can store.
• Whenever a variable is defined in C++, the compiler allocates some
memory for that variable based on the data-type with which it is
declared.
• Every data type requires a different amount of memory.

08/23/2021 Datatypes / OOP/M.Karthick/CSE/SNSCT 9/7


Data types in C++ is mainly divided into three types

Primitive Data Types:


These data types are built-in or predefined data types
and can be used directly by the user to declare
variables. example: int, char , float, bool etc.
Derived Data Types: 
The data-types that are derived from the primitive or
built-in datatypes are referred to as Derived Data
Types.
Abstract or User-Defined Data Types:
These data types are defined by user itself. Like,
defining a class in C++ or a structure

08/23/2021 Datatypes / OOP/M.Karthick/CSE/SNSCT 10/7


Data type Keyword Type/Format specifier Memory size Range
Short Integer short int %d or %i 2 bytes -32768 to +32767

Long Integer long int or int %d or %i 4 bytes 2-31 to 231


Signed Integer int or short int or long %d or %i 2 or 4 bytes -32768 to +32767 or 2-31 to 231
int

Unsigned Integer unsigned int %u 2 or 4 bytes 0 to +65535 or 0 to 232-1

Floating-point float %f 4 bytes +/- 3.4e +/- 38 (~7 digits)

Double double or long double %ld 8 bytes +/- 1.7e +/- 308 (~15 digits)

Signed Character char or signed char %c or %s 1 bytes -128 to +127

Unsigned Character unsigned char %c or %s 1 bytes 0 to +255

Boolean bool %d or none 1 bit 0 or 1


Void void none No memory No value
Wide Character wchar_t %c or %s 2 bytes 2-15 to 215

08/23/2021 Datatypes / OOP/M.Karthick/CSE/SNSCT 11/7


Assessment 1

08/23/2021 Datatypes / OOP/M.Karthick/CSE/SNSCT 12/7


References
• Larry L. Peterson, Bruce S. Davie, “Computer Networks: A systems approach”, Fifth Edition, Morgan Kaufmann
Publishers, 2011.
• Behrouz A. Forouzan, “Data communication and Networking”, Fourth Edition, Tata McGraw – Hill, 2011.
• James F. Kurose, Keith W. Ross, “Computer Networking - A Top-Down Approach Featuring the Internet”, Fifth Edition,
Pearson Education, 2009
• Nader. F. Mir, “Computer and Communication Networks”, Pearson Prentice Hall Publishers, 2010.

08/23/2021 Datatypes / OOP/M.Karthick/CSE/SNSCT 13/7


08/23/2021 Datatypes / OOP/M.Karthick/CSE/SNSCT 14/7

You might also like