Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
5 views

Prog 1 Module Lesson 3

This document discusses data types and identifiers in C++. It describes the basic data types like int, float, double, boolean, and char, including their sizes and what types of values they can store. It also discusses type modifiers that can change the range of values a data type can hold. The document then explains that identifiers are names given to variables, functions, and other items. It provides rules for naming identifiers and lists C++ keywords that cannot be used as identifiers.

Uploaded by

Zay Caratihan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Prog 1 Module Lesson 3

This document discusses data types and identifiers in C++. It describes the basic data types like int, float, double, boolean, and char, including their sizes and what types of values they can store. It also discusses type modifiers that can change the range of values a data type can hold. The document then explains that identifiers are names given to variables, functions, and other items. It provides rules for naming identifiers and lists C++ keywords that cannot be used as identifiers.

Uploaded by

Zay Caratihan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Data Types and Identifiers

UNIT 3: DATA TYPES AND IDENTIFIERS

Introduction

A program is normally written to process data or information. To manage these data,


program must identify data according to their type. Some data can be:

 numeric (whole number or with fractional part),


 alphabetic (in uppercase A-Z, or in lowercase a-z),
 special character ( ex. !”#$%&’()?= ), or
 combinations of digits, alphabetic and special characters

Data or information is placed in the memory of computer while the program is executing.
Imagine the memory of computer as a big storage with many shelves. Each shelf can only hold
one piece of data. To be able to access these data, each shelf containing a piece of data should
be named in order to be identified. Data are placed anywhere in the memory of your computer.
The size of memory allocated for each data depends on its type. For now, you don’t need to
know where they are located or to know their exact address to be able to run your program.

50 2020

age cYear

1970

bYear

Learning Objectives

After successful completion of this lesson, you should be able to:

1. Identify the basic types of data.


2. Create appropriate identifier / name to hold data based on the rules of C++ language.
3. Identify C++ keywords that cannot be use as an identifier.

18
Data Types and Identifiers

Course Materials

3.1 Basic Data Types

The data type specifies the size and type of information the variable can store:

Data Size Description


Type

int 4 bytes Stores whole numbers, without decimals

float 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 1 byte Stores a single character/letter/number, or ASCII values

https://www.geeksforgeeks.org/c-data-types/

Data type modifiers available in C++ are:


 Signed
 Unsigned
 Short
 Long

It is written before the data type to widen or shorten the range of values it can represent. If
data type is written without the modifiers it will use the default range.

19
Data Types and Identifiers

The table summarizes the modified size and range of built-in data types when combined
with the type modifiers:

DATA TYPE SIZE (IN BYTES) RANGE

short int 2 -32,768 to 32,767

unsigned short int 2 0 to 65,535

unsigned int 4 0 to 4,294,967,295

Int 4 -2,147,483,648 to 2,147,483,647

long int 4 -2,147,483,648 to 2,147,483,647

unsigned long int 4 0 to 4,294,967,295

long long int 8 -(2^63) to (2^63)-1

unsigned long long int 8 0 to 18,446,744,073,709,551,615

signed char 1 -128 to 127

unsigned char 1 0 to 255

Float 4

Double 8

long double 12

wchar_t 2 or 4 1 wide character


https://www.geeksforgeeks.org/c-data-types/

Note : Above values may vary from compiler to compiler. In above example, we have
considered GCC 64 bit.

In declaring data type, make sure that it will suit the kind of data you will process. For
example, if your data is

 population use unsigned int


 price of an item use float
 name of a person use string
 middle initial use char
 phone number use string since it will not be used for computation

20
Data Types and Identifiers

3.2 Identifier

The C++ identifier is a name given to identify a variable, function, class, module, or any
other user-defined item. An identifier always starts with a letter A to Z or a to z or an underscore
(_) followed by zero or more letters, underscores, and digits (0 to 9).
C++ does not allow punctuation characters such as @, $, and % within identifiers. C++ is a
case-sensitive programming language. Thus, grossPay and GrossPay are two different
identifiers in C++.
In giving names, make sure that it somehow describes the data that it holds or the purpose
of the function or module. Appropriate identifiers can help programmers in debugging program,
especially if they are not the ones who wrote it.

Examples of good identifiers:


 grossPay
 gross_pay
 midtermGrade
 customerName
 VAT
 Quarter3

Examples of bad identifiers


 X
 Gp
 Mg
 Cn
 Q3

You can easily forget what kind of data a variable holds if you use initials as identifiers.

Note: To make your identifier easier to read you can use an underscore ( _ )in between
two words (i.e. net_pay) or you can capitalize the beginning of succeeding words (i.e.
finalGrade, firstQuarterSales, cityPopulation). C++ keywords are reserved words and
cannot be used as an identifier.

21
Data Types and Identifiers

3.3 C++ Keywords


The following list shows the reserved words in C++. These reserved words may not be
used as constant or variable or any other identifier names.

asm else new this

auto enum operator throw

bool explicit private true

break export protected try

case extern public typedef

catch false register typeid

char float reinterpret_cast typename

class for return union

const friend short unsigned

const_cast goto signed using

continue if sizeof virtual

default inline static void

delete int static_cast volatile

do long struct wchar_t

double mutable switch while

dynamic_cast namespace template

https://www.tutorialspoint.com/cplusplus/cpp_basic_syntax.htm

char and string data type

Values declared as char must be enclosed with a single quotation mark.

Examples: ‘s’, ‘3’, ‘ ‘, ‘$’

String data type is actually an array of characters. In the earlier version of C language,
there is no string data types, so if data is more than one characters you need to declare it as
an array of characters.

22
Data Types and Identifiers

Example:

In older version, you need to specify the maximum length of the variable, like:

char schoolName[100] = {‘ ‘};

// with this declaration schoolName is limited only to 100 characters

cin >> schoolName;

P o l y t e … \0
n
0 1 2 3 4 5 41 95 96 97 98 99

‘\n’ character specify the end of the string

cout << schoolName.length(); 41

cout << schoolName[0]; P

cout << schoolName[7]; h

cout << schoolName[40]; s

With the new version of C language, you can now use the string data type.
They are enclosed with double quotation marks ( “ “).

string schoolName = “ “;

How many characters can a string hold?

size_type string::max_size() const

 Returns the maximum number of characters that a string is allowed to


have.
 This value will vary depending on operating system and system
architecture.

Sample code:

1 string sString("MyString");
2 cout << sString.max_size() << endl;

Output:

4294967294

https://www.learncpp.com/cpp-tutorial/17-3-stdstring-length-and-capacity/

23
Data Types and Identifiers

Activities

Write appropriate identifier and data type to represent the following data:

Data Type Identifier

1. Name of hospital ______________ _________________


2. Discounted price ______________ _________________
3. Number of attendees in a meeting ______________ _________________
4. Letter grade ______________ _________________
5. Weekly pay ______________ _________________
6. Hourly rate ______________ _________________
7. Gender ______________ _________________
8. Speed of car in kph ______________ _________________
9. Size of room in sqm ______________ _________________
10. Mobile number ______________ _________________

Online References

https://www.geeksforgeeks.org/c-data-types/

https://www.learncpp.com/cpp-tutorial

https://www.tutorialspoint.com/cplusplus/cpp

24

You might also like