cs125 - lab 02 - data types and variables

Upload: asadhppy

Post on 04-Jun-2018

213 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/13/2019 CS125 - Lab 02 - Data Types and Variables

    1/4

    CS125: Programming Fundamentals

    Lab No. 2: Data Types and Variables

    Lab No. 2Data Types and Variables

    Objective1. Primitive built-in data types2. Type of literals3. Escape sequences4. Variables

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

    Primitive Data TypesType Meaning Minimum

    Size in BytesMSVC Sizein Bytes

    Signed/UnsignedMakeSense?

    bool boolean NAchar character 1wchar_t Wide character 2char16_t Unicode character 2char32_t Unicode character 4short short integer 2int integer 2long long integer 4long long long long integer 8

    float single-precision floating point NAdouble double-precision floating point NAlong double Extended-precision floating point NA

    As a general rule for the minimum size of integral types;

    1 == sizeof(char)

  • 8/13/2019 CS125 - Lab 02 - Data Types and Variables

    2/4

    CS125: Programming Fundamentals

    Lab No. 2: Data Types and Variables

    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) compilersreference of data types size in bytes.

    3. For which data types, we can define signed and unsigned attributes of the data types. Fill in thecorresponding 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 thenwrite 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 ;

  • 8/13/2019 CS125 - Lab 02 - Data Types and Variables

    3/4

    CS125: Programming Fundamentals

    Lab No. 2: Data Types and Variables

    return 0; }

    Type of LiteralsA value, such as 42, is known as a literal because its value self-evident. Every literal has a type. Theform and value of a literal determine its type.

    Escape SequencesSome characters, such as backspace or control characters, have no visible image. Such characters arenonprintable. 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 abackslash. Some useful escape sequences are as follows;

    Exercise B1. 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"

  • 8/13/2019 CS125 - Lab 02 - Data Types and Variables

    4/4

    CS125: Programming Fundamentals

    Lab No. 2: Data Types and Variables

    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 theprogram to print 2, then a tab, then an M, followed by a newline.

    VariablesA 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 valuesthat can be stored within that memory, and the set of operations that can be applied to the variable.

    Exercise C1. 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 ExerciseTemperature 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: