Sei sulla pagina 1di 4

CS125: Programming Fundamentals

Lab No. 2 Data Types and Variables


Objective
1. 2. 3. 4. 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. Primitive built-in data types Type of literals Escape sequences Variables Remember that every declaration must specify a type (there is no implicit int) Avoid unnecessary assumptions about the numeric value of characters. Avoid unnecessary assumptions about the size of integers. Avoid unnecessary assumptions about the range of floating-point types. Prefer a plain int over a short int or short or a long int or a long. Prefer a double over a float or a long double. Prefer plain char over signed char and unsigned char. Avoid making unnecessary assumptions about the sizes of objects. Avoid unsigned arithmetic. View signed to unsigned and unsigned to signed conversions with suspicion. View floating-point to integer conversions with suspicion. View conversions to a smaller type, such as int to char, with suspicion. Develop habit to declare and initialize the variable at the same time.

Programming Practices

Primitive Data Types


Type Meaning Minimum Size in Bytes MSVC Size in Bytes Signed/ Unsigned Make Sense?

bool char wchar_t char16_t char32_t short int long long long float double long double

boolean character
Wide character

Unicode character Unicode character short integer integer long integer long long integer single-precision floating point double-precision floating point Extended-precision floating point

NA 1 2 2 4 2 2 4 8 NA NA NA

As a general rule for the minimum size of integral types; 1 == sizeof(char) <= sizeof(short) <= sizeof(int) <= sizeof(long) <= sizeof(long long)

Lab No. 2: Data Types and Variables

CS125: Programming Fundamentals

Exercise A
Following is the example which will produce correct size of various data type on your computer.

int main() { int b = sizeof(bool); int c = sizeof(char); int wchart = sizeof(wchar_t); int s = sizeof(short); int i = sizeof(int); int l = sizeof(long); int ll = sizeof(long long); int f = sizeof(float); int d = sizeof(double); int ld = sizeof(long double); return 0; }

1. Check the output of variables in each of the above program by debugging. 2. In the table given on previous page, fill the column MSVC (Microsoft Visual C) compilers reference of data types size in bytes. 3. For which data types, we can define signed and unsigned attributes of the data types. Fill in the corresponding column for each data type in the table.

4. Does the signed/unsigned specification of the given integral data type affects its size?

5. Debug the Above program, if errors then explain the errors and remove errors, If not error then write the values of the variables.
int main() { Int population=167050245; Char int=B; return 0; }

6. Check out the output of the variable i and j in the following program by debugging;
int main() { short i; unsigned short j; j = 50000; i = j;

Lab No. 2: Data Types and Variables

CS125: Programming Fundamentals

return 0; }

Type of Literals
A value, such as 42, is known as a literal because its value self-evident. Every literal has a type. The form and value of a literal determine its type.

Escape Sequences
Some characters, such as backspace or control characters, have no visible image. Such characters are nonprintable. Other characters (single and double quotation marks, question mark, and backslash) have special meaning in the language. Our programs cannot use any of these characters directly. Instead, we use an escape sequence to represent such characters. An escape sequence begins with a backslash. Some useful escape sequences are as follows;

Exercise B
1. Determine the type of each of the following literals. Check the value by debugging. Explain the differences among the literals in each of the four examples: i. 'a', "a"

Lab No. 2: Data Types and Variables

CS125: Programming Fundamentals ii. 10, 10u, 10L, 10uL, 012, 0xC, 0x11

iii.

3.14, 3.14f, 3.14L

iv.

10, 10u, 10., 10e-2

2. What, if any, are the differences between the following definitions:


int month = 9, day = 7; int month = 09, day = 07;

3. Using escape sequences, write a program to print 2M followed by a newline. Modify the program to print 2, then a tab, then an M, followed by a newline.

Variables
A variable provides us with named storage that our programs can manipulate. Each variable in C++ has a type. The type determines the size and layout of the variables memory, the range of values that can be stored within that memory, and the set of operations that can be applied to the variable.

Exercise C
1. Is the following program correct? Compile and debug it in VC. You will observe different ways of initializing the variable.
int sum = 0, value, units_bought = 7; double price = 109.99, discount = price * 0.16; int units_sold1 = 0; int units_sold2 = {10}; int units_sold3{20}; int units_sold4(30);

Final Exercise
Temperature of Islamabad (in Centigrade rounded to nearest integer) for last 7 days is as follows; Day1= 42, Day2=42, Day3=40, Day4=41, Day5=40, Day6=39, Day7=39 Find the exact average temperature of the last week. Choose data types appropriately.

Checked By:

Date:

Lab No. 2: Data Types and Variables

Potrebbero piacerti anche