Data Types – The Sequel

1. Which of the following declaration statements are valid.

1. short int radius, double area;

Ans: Invalid. Two different data types in same declaration statement.

 

2. short int radius = 0, double area = 0.0;

Ans: Invalid. Two different data types in same declaration statement.

 

3. short math, science = 0;

Ans: Valid. math is uninitialized while science is.

 

4. short math, int total;

Ans: Invalid. Two different data types in same declaration statement.

 

5. short int age = 19;

Ans: Valid.

 

6. short num_countries_in_UN = 193;

Ans: Valid.

 

2. Trace control flow of below program for following user inputs

weight = 87, height = 168

weight = 59, height = 172

 

#include <stdio.h>

int main(int argc, char * argv[]) {
    short weight = 0, height = 0;
    double bmi = 0;

    printf("Enter your height in cms: ");
    scanf("%hd",&height);
    printf("Enter your weight in kgs: ");
    scanf("%hd",&weight);

    bmi=weight/((height/100.0) * (height/100.0));

    if (bmi < 18.5) {
        printf("Weight of %hd kgs for height of %hd cms is too less\n",weight, height);
    } else if (bmi > 24.9){
        printf("Weight of %hd kgs for height of %hd cms is too high\n",weight, height);
    } else {
        printf("You have the right balance of height and weight\n");
    }

return 0;
}

 

weight = 87, height = 168

  1. After user input, control reaches line 12.
  2. Value of bmi is computed, it comes to 30.82483.
  3. Control advances to line 14. if control expression evaluates to false. Control skips if block and jumps to else, line 16.
  4. if control expression evaluates to true. Control enters if block, line 17.
  5. Message Weight of 87 kgs for height of 168 cms is too high is printed.
  6. Control reaches end of if block, jumps to line 22.
  7. Program terminates.

weight = 59, height = 172

  1. After user input, control reaches line 12.
  2. Value of bmi is computed, it comes to 943213.
  3. Control advances to line 14. if control expression evaluates to false. Control skips if block and jumps to else, line 16.
  4. if control expression evaluates to false, control jumps to else, line 18.
  5. Control advances to line 19. Message You have the right balance of height and weight is printed.
  6. Control reaches end of else and advances to line 22.
  7. Program terminates.

 

 

3. Which of the following statements are true.

1. sizeof is a C language keyword.

Ans: True.

 

2. sizeof is a C language operator.

Ans: True.

 

3. sizeof evaluates to number of bits required to store the value of its operand.

Ans: False. It evaluates to number of bytes, not bits.

 

4. sizeof evaluates to number of bytes required to store the data type of its operand.

Ans: True.

 

 

4. Assume int a = 2 and int b = 100000, which of the following statements are true

1. sizeof(a) is equal to sizeof(b).

Ans: True. Size of a data type is independent of the value stored in it.

 

2. sizeof(a) is less than sizeof(b).

Ans: False

 

3. sizeof(a) is equal to sizeof(int).

Ans: True.

 

5. Write a program to print size of double.

#include <stdio.h>

int main (int argc, char * argv[]) {
    printf("sizeof double is %lu",sizeof(double));
    return 0;
}

 

6. Assume that size of double is 8, size of int is 4. Given, int a = 4 and double b = 2.0, what would be the result of sizeof(a/b).

Ans: int/double will give a double. Hence sizeof(a/b) is same as sizeof(double), i.e. 8.

 

7. Which of the following are valid declarations?

1. int long count = 222222;

Ans: Invalid. Two different data types in same declaration statement.

 

2. long int count = 222222;

Ans: Valid. “long int” and “long” refer to same data type.

 

3. short long count = 22;

Ans: Invalid. Two different data types in same declaration statement.

 

4. short long int count = 22;

Ans: Invalid. Two different data types in same declaration statement.

 

5. long count = 222222;

Ans: Valid.

 

8. Write a program to print size of long.

#include <stdio.h>

int main (int argc, char * argv[]) {
    printf("sizeof double is %lu",sizeof(long));
    return 0;
}

 

9. Modify the above example to check for erroneous input such that memory_used is less than zero or is more than phone_memory.

#include <stdio.h>

int main(int argc, char * argv[]) {
    long phone_memory = 8589934592;
    long memory_used = 0;
    double percent_used = 0;

    printf("Enter memory used: ");
    scanf("%ld", &memory_used);

    if (memory_used < 0 || memory_used > phone_memory) {
        printf("Invalid input\n");
    } else {
        percent_used = (memory_used*100.0)/phone_memory;

        if (percent_used > 90) {
            printf("Running out of storage, cleanup now\n");
        } else {
            printf("Still have %ld bytes free",phone_memory-memory_used);
        }
    }
    return 0;
}

 

10. Write a program to print size of long long.

#include <stdio.h>

int main (int argc, char * argv[]) {
    printf("sizeof double is %lu",sizeof(long long));
    return 0;
}

 

 

11. Which of the following are valid declarations?

1. float pi = 3.14;

Ans: Valid.

 

2. Float pi = 3.14;

Ans: Invalid. Float is not a data type – C is case sensitive.

 

3. double float pi = 3.14;

Ans: Invalid. Two different data types in same declaration statement.

 

4. float int = 3.14;

Ans: Invalid. Two different data types in same declaration statement.

 

5. float double pi = 3.14;

Ans: Invalid. Two different data types in same declaration statement.

 

6. float pi;

Ans: Valid.

 

12. Modify the above program to include invalid input value checks, percent and average should be between 0 and 100.

#include <stdio.h>

int main(int argc, char * argv[]) {
    float percent = 0, average = 0;
    printf("Enter your percent score: ");
    scanf("%f",&percent);
    printf("Enter class average score: ");
    scanf("%f",&average);

    if (percent < 0 || percent > 100 || average < 0 || average > 100) {
        printf("Invalid input\n");
    } else {
        if (percent > average) {
            printf("You qualify for scholarship exam\n");
        } else {
            printf("You don't qualify for scholarship exam\n");
       }
    }

    return 0;
}

 

 

13. Further enhance the program to qualify students that have scored above average and a minimum of 80%.

#include <stdio.h>

int main(int argc, char * argv[]) {
    float percent = 0, average = 0;
    printf("Enter your percent score: ");
    scanf("%f",&percent);
    printf("Enter class average score: ");
    scanf("%f",&average);

    if (percent < 0 || percent > 100 || average < 0 || average > 100) {
        printf("Invalid input\n");
    } else {
        if (percent > average && percent > 80) {
            printf("You qualify for scholarship exam\n");
        } else {
            printf("You don't qualify for scholarship exam\n");
        }
    }

    return 0;
}

 

 

14. Write a program to print size of float.

#include <stdio.h>
int main (int argc, char * argv[]) {
    printf("sizeof double is %lu",sizeof(long long));
    return 0;
}

 

 

15. Modify the above program to print input sample width. Use a very small value < 1X10-10. Print it using %Lf as well as %Le format specifier and observe the difference in readability.

#include <stdio.h>

int main(int argc, char * argv[]) {

    /* Typical human hair is 17um to 181um thick */
    long double hair_min = 17e-06;
    long double hair_max = 181e-06;
    long double sample = 0;
    printf("Enter breadth of slice: ");
    scanf("%Lf",&sample);

    if (sample < hair_min) {
        printf("This sample is thinner than human hair\n");
    } else if (sample > hair_max) {
        printf("This sample is thicker than human hair\n");
    }

    printf("Sample breadth %Le",sample);
    printf("Sample breadth %Lf",sample);
    return 0;
}

If you run this program, you would notice that scientific notation is far more readable for such values as compared to regular decimal notation.

 

 

16. Write a program to print size of long double.

#include <stdio.h>

int main (int argc, char * argv[]) {
    printf("sizeof double is %lu",sizeof(long double));
    return 0;
}

 

 

17. Which of the following are valid char declarations

1. char flag = “A”;

Ans: Invalid. char literals are enclosed in single quotes.

 

2. char flag = ‘A’;

Ans: Valid.

 

3. char flag;

Ans: Valid.

 

4. char flag = ‘”’;

Ans: Valid. Double quotes is the char literal enclosed in single quotes.

 

5. char flag = ‘\\’;

Ans: Invalid. \\ is not a char literal.

 

6. char flag = ‘\’;

Ans: Valid.

 

18. Write a program incorporating the following printf statements and observe the output.

printf(“This is a double quote \” ”);

printf(“This is a backslash \\ ”);

#include <stdio.h>
int main(int argc, char * argv[]) {
    printf("This is a double quote \" ");
    printf("\n");
    printf("This is a backslash \\ ");
    return 0;
}

 

Below is the output.

This is a double quote ”

This is a backslash \

 

As we can observe, backslash is our escape character that helps print double quote and backslash as part of print string.

 

19. Write a program to print string given below, including the quotes

“In C \\ prints \ on console”

#include <stdio.h>
int main(int argc, char * argv[]) {
    printf("\"In C \\\\ prints \\ on console\"");
    printf("\n");
    return 0;
}

 

20. Using the ASCII table above for encoding characters, what will be the output of below program?

#include <stdio.h>

int main(int argc, char * argv[]) {

    char small = 'a';
    char big = 'A';
    if (big > small) {
        printf ("'A' is bigger than 'a'");
    } else {
        printf ("'a' is bigger than 'A'");
    }
    return 0;
}

 

Ans: From the ASCII table, we can see that int encoding value for ‘A’ is 65 and value for ‘a’ is 97. Hence, ‘A’ is smaller than ‘a’.  Output of the program will be

‘a’ is bigger than ‘A’

 

21.

01: #include <stdio.h>

02: int main (int argc, char * argv[]) {
03:     char test = 0;
04:     printf("Enter a small case alphabet [a-z]: ");
05:     scanf("%c",&test);

06:     switch(test) {
07:         case 'a':
08:         case 'e':
09:         case 'i':
10:         case 'o':
11:         case 'u':
12:             printf("You entered a vowel\n");
13:             break;
14:         default:
15:             printf("You did not enter a vowel\n");
16:     }

17:     return 0;
18: }

Trace the control flow of above program for following user inputs
  1. ‘r’
  2. ‘E’
  3. ‘e’
  4. ‘%’

 

‘r’

  1. After user input, control reaches line 06. Control expression evaluates to (integer value of) ‘r’. There is no matching case label. Control jumps to default case line 14.
  2. Control advances to line 15. Message You did not enter a vowel is printed.
  3. Control advances to end of switch case, end of program and terminates.

 

‘E’

  1. After user input, control reaches line 06. Control expression evaluates to (integer value of) ‘E’. There is no matching case label. Control jumps to default case line 14.
  2. Control advances to line 15. Message You did not enter a vowel is printed.
  3. Control advances to end of switch case, end of program and terminates.

 

‘e’

  1. After user input, control reaches line 06. Control expression evaluates to (integer value of) ‘e’. Control jumps to matching case, line 08.
  2. Control falls through line 09, 10, and 11.
  3. Control advances to line 12, message You entered a vowel is printed.
  4. Control advances to line 13. break causes control to jump out of switch case, line 17.
  5. Program terminates.

 

‘%’

  1. After user input, control reaches line 06. Control expression evaluates to (integer value of) ‘%’. There is no matching case label. Control jumps to default case line 14.
  2. Control advances to line 15. Message You did not enter a vowel is printed.
  3. Control advances to end of switch case, end of program and terminates.

 

22. Write a calculator program using switch case. Program should read in two integers and an operator (+,-,*,/) and print the result of the operation

#include <stdio.h>

int main(int argc, char * argv[]) {
    int operand1 = 0, operand2 = 0;
    char operator = 0;

    printf("Enter operator: ");
    scanf("%c", &operator);
    printf("Enter first operand: ");
    scanf("%d", &operand1);
    printf("Enter second operand: ");
    scanf("%d", &operand2);

    switch(operator) {
        case '+':
            printf("%d\n", operand1 + operand2);
            break;
        case '-':
            printf("%d\n", operand1 - operand2);
            break;
        case '*':
            printf("%d\n", operand1 * operand2);
            break;
        case '/':
            printf("%d\n", operand1 / operand2);
            break;
        default:
            printf("Unrecognised operator\n");
    }
    return 0;
}

 

 

23. Write a program to print size of following data types

unsigned char

unsigned int

unsigned short

unsigned long

unsigned long long

#include <stdio.h>

int main(int argc, char * argv[]) {
    printf("sizeof(unsigned char) = %lu\n",sizeof(unsigned char));
    printf("sizeof(unsigned int) = %lu\n",sizeof(unsigned int));
    printf("sizeof(unsigned short) = %lu\n",sizeof(unsigned short));
    printf("sizeof(unsigned long) = %lu\n",sizeof(unsigned long));
    printf("sizeof(unsigned long long) = %lu\n",sizeof(unsigned long long));
    return 0;
}

 

 

24. Write a program in which you assign a negative value to an unsigned integer. Observe the warnings issued by the compiler. Compiler warnings are your friend that can help you avoid mistakes.

You should observe warning about assigning negative value to an unsigned variable. If you do not see a warning then your compiler is not configured to show all warnings. Check your compiler documentation on how to enable such warnings.

 

25. Which of the following statements are true

1. sizeof(int) is greater than sizeof(unsigned int)

Ans: False. Only difference between the two types is use of sign bit. Their size is the same.

2. Max value stored in unsigned int is greater than max value stored in int

Ans: True. Due to an extra bit available to unsigned type, it can store higher maximum value.

 

26. Which of the following declarations are equivalent to long int a = 5;

1. int a = 5;

Ans: No. int is not same as long.

 

2. signed long a = 5;

Ans: Yes. long and long int are same data type. By default long declaration is a signed long.

 

3. signed long int a = 5;

Ans: Yes. long and long int are same data type. By default long declaration is a signed long.

 

4. unsigned long a = 5;

Ans: No. By default long declaration is a signed long.

 

5. long a = 5;

Ans: Yes. long and long int are same data type.

 

27. Run the following program and explain its output.

#include <stdio.h>

int main(int argc, char * argv[]) {
    double double_var = 3.142;
    int int_var = 5;
    printf("%u %u %u \n", sizeof(double_var),sizeof(int_var),sizeof(double_var+int_var));
}

 

Ans:

Result of addition of int and double is a double. Hence, program effectively prints sizeof(double) sizeof(int) sizeof(double)

 

28. What will be the data type of result of following expressions. Assume int i; float f; double d; char c; unsigned int ui; short s; long double ld;

1. (ld + f)

Ans: long double

 

2. (ld + f)/s

Ans: long double

 

3. (i * c)/f

Ans: float

 

4. (i + ui)/d

Ans: double

 

 

29. Try overwriting the value of pi in the below example by adding an assignment statement assigning value 9.9 to pi. What error do you observe?

#include <stdio.h>
int main(int argc, char * argv[]) {
    const double pi = 3.142;
    float radius = 2.7;
    printf ("Area of circle = %f\n", pi*radius*radius);
}

Ans: You should see an error (note this is not a warning but an error) indicating that you are trying to assign value to a const.