C Programming Chapter 9 - Questions and Answers

Fill in the Blanks

a. Identifiers in C programming language start with ......

a letter or underscore

b. break is a keyword in C ...... (TRUE/FALSE)

TRUE

c. A variable in C can store more than one value at a time ...... (TRUE/FALSE)

FALSE

d. An integer variable typically reserves ...... bytes in memory.

4

e. Built-in function used to accept user input from the keyboard is ......

scanf()

f. Format specifier for accepting value from keyboard for an integer variable is ......

%d

g. Every operator in C requires exactly two operands ...... (TRUE/FALSE)

FALSE (some operators like unary operators require only one operand)

h. ...... sign is used to indicate reminder operation in C.

%

i. To compare two float variables in C, we can use ...... statement.

if

j. When we need to select a set of statements for execution in a C program based on some integral value, a good choice can be to use ...... construct.

switch-case

Short Answer Questions

a. If we need to execute many statements when an if condition in C returns true, how can we write them? Write a code segment.

We can use curly braces {} to group multiple statements that should be executed when the if condition is true.

if (condition) { statement1; statement2; statement3; // ... more statements }
b. List five commonly used operators of C programming language with their meaning.
  1. + (Addition): Adds two operands
  2. - (Subtraction): Subtracts second operand from the first
  3. * (Multiplication): Multiplies both operands
  4. / (Division): Divides numerator by denominator
  5. % (Modulus): Returns remainder after division
c. The scanf statement should always be preceded by a printf statement in C. Is it true? Explain briefly.

No, it's not strictly required, but it's a good programming practice. Using printf before scanf helps prompt the user about what input is expected, making the program more user-friendly.

Programming Assignments

a. Write a C program and try to declare variables of type int with the following variable names separately. Report if any variable declarations are not permitted by the compiler. Analyze the reasons.

Invalid variable names and reasons:

  • char: Invalid - 'char' is a reserved keyword in C
  • -: Invalid - cannot start with a hyphen
  • --: Invalid - cannot start with a hyphen and is also a decrement operator
  • 9months: Invalid - cannot start with a digit
  • 8724: Invalid - cannot start with a digit
  • gogamukh??: Invalid - contains special character '?'
  • I want to be a doctor: Invalid - contains spaces

Valid variable names: first_variable, secondVariable, ThirdVariable, number, _x, blp24, dimatala

b. Write the following code segment inside the main() function of a C program and run it. Analyse the output. Relate the output with the concepts learned in Section 9.2 about the allocated space of a variable in computer memory.
#include <stdio.h> int main() { int var = 7; printf("The value of var is %d. ", var); printf("The address of var in memory is %p.", &var); return 0; }

Output Analysis: The program will display the value of var (7) and its memory address. This demonstrates that each variable in C has a specific memory location where its value is stored. The & operator returns the address of the variable in memory.

c. Write the following code segment inside the main() function of a C program and run it. Analyse the output.
#include <stdio.h> int main() { int p = 9; printf("Size of the variable p is %ld \n", sizeof(p)); printf("Size of an integer is %ld \n", sizeof(int)); return 0; }

Output Analysis: The program will display the size of variable p and the size of int data type. Both will typically be 4 bytes (on most modern systems). This shows that the sizeof operator returns the memory allocated for a variable or data type in bytes.

d. Write a C program to do the following.
#include <stdio.h> int main() { int p = 9, q = 7, result = 0; // Arithmetic operations result = p + q; printf("p + q = %d\n", result); result = p - q; printf("p - q = %d\n", result); result = p / q; printf("p / q = %d\n", result); // Integer division result = p * q; printf("p * q = %d\n", result); result = p % q; printf("p %% q = %d\n", result); // Bitwise and logical operations result = p & q; printf("p & q = %d\n", result); result = p && q; printf("p && q = %d\n", result); // Increment and decrement operations result = 0; ++result; printf("After ++result: %d\n", result); result++; printf("After result++: %d\n", result); result--; printf("After result--: %d\n", result); return 0; }
e. Write a C program to find the area of a rectangle. The length and width of the rectangle are inputs.
#include <stdio.h> int main() { float length, width, area; printf("Enter the length of the rectangle: "); scanf("%f", &length); printf("Enter the width of the rectangle: "); scanf("%f", &width); area = length * width; printf("The area of the rectangle is: %.2f\n", area); return 0; }
f. Change the programming example B.1 to accept the integers from the keyboard using scanf() function.

Assuming example B.1 is a basic program that declares and uses integers:

#include <stdio.h> int main() { int num1, num2, sum; printf("Enter first integer: "); scanf("%d", &num1); printf("Enter second integer: "); scanf("%d", &num2); sum = num1 + num2; printf("The sum of %d and %d is %d\n", num1, num2, sum); return 0; }
g. Improve the programming example B.5 to check whether the student scores PASS or FAIL in individual subjects.

Assuming example B.5 is a student grade calculation program:

#include <stdio.h> int main() { float marks1, marks2, marks3, total, average; printf("Enter marks for subject 1: "); scanf("%f", &marks1); printf("Enter marks for subject 2: "); scanf("%f", &marks2); printf("Enter marks for subject 3: "); scanf("%f", &marks3); // Check PASS/FAIL for each subject if (marks1 >= 40) printf("Subject 1: PASS\n"); else printf("Subject 1: FAIL\n"); if (marks2 >= 40) printf("Subject 2: PASS\n"); else printf("Subject 2: FAIL\n"); if (marks3 >= 40) printf("Subject 3: PASS\n"); else printf("Subject 3: FAIL\n"); total = marks1 + marks2 + marks3; average = total / 3; printf("Total marks: %.2f\n", total); printf("Average marks: %.2f\n", average); return 0; }
h. Improve the programming example B.5 to check whether every mark entered from the keyboard is positive or not. If any mark is wrongly entered as negative, prompt the user to re-enter.
#include <stdio.h> int main() { float marks1, marks2, marks3, total, average; // Input and validation for subject 1 do { printf("Enter marks for subject 1: "); scanf("%f", &marks1); if (marks1 < 0) printf("Invalid marks! Please enter a positive value.\n"); } while (marks1 < 0); // Input and validation for subject 2 do { printf("Enter marks for subject 2: "); scanf("%f", &marks2); if (marks2 < 0) printf("Invalid marks! Please enter a positive value.\n"); } while (marks2 < 0); // Input and validation for subject 3 do { printf("Enter marks for subject 3: "); scanf("%f", &marks3); if (marks3 < 0) printf("Invalid marks! Please enter a positive value.\n"); } while (marks3 < 0); // Rest of the code for calculations and output total = marks1 + marks2 + marks3; average = total / 3; printf("Total marks: %.2f\n", total); printf("Average marks: %.2f\n", average); return 0; }
i. Write a program in C for awarding student grade (as example B.5) using switch-case construct.
#include <stdio.h> int main() { float marks1, marks2, marks3, average; int grade; printf("Enter marks for subject 1: "); scanf("%f", &marks1); printf("Enter marks for subject 2: "); scanf("%f", &marks2); printf("Enter marks for subject 3: "); scanf("%f", &marks3); average = (marks1 + marks2 + marks3) / 3; // Convert average to an integer for switch-case grade = (int)(average / 10); switch(grade) { case 10: case 9: printf("Grade: A\n"); break; case 8: printf("Grade: B\n"); break; case 7: printf("Grade: C\n"); break; case 6: printf("Grade: D\n"); break; case 5: printf("Grade: E\n"); break; default: printf("Grade: F (Fail)\n"); break; } return 0; }