Prog 1 Module Lesson 3
Prog 1 Module Lesson 3
Introduction
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
18
Data Types and Identifiers
Course Materials
The data type specifies the size and type of information the variable can store:
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
https://www.geeksforgeeks.org/c-data-types/
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:
Float 4
Double 8
long double 12
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
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.
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
https://www.tutorialspoint.com/cplusplus/cpp_basic_syntax.htm
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:
P o l y t e … \0
n
0 1 2 3 4 5 41 95 96 97 98 99
With the new version of C language, you can now use the string data type.
They are enclosed with double quotation marks ( “ “).
string schoolName = “ “;
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:
Online References
https://www.geeksforgeeks.org/c-data-types/
https://www.learncpp.com/cpp-tutorial
https://www.tutorialspoint.com/cplusplus/cpp
24