Study resource

Read at your pace, then save it for later.

Unit 2: Operators, Expressions, and Input/Output Operations in C

Section A — 1 Mark Questions

1. What is an arithmetic operator? [1 Mark]

Answer: An arithmetic operator is a symbol used to perform mathematical calculations like addition, subtraction, multiplication, division, and modulus.

2. Give the symbol for the modulus operator in C. [1 Mark]

Answer: The symbol for the modulus operator is %.

3. What is the purpose of relational operators? [1 Mark]

Answer: Relational operators are used to compare two values or expressions.

4. List the three logical operators. [1 Mark]

Answer: The three logical operators are logical AND (&&), logical OR (||), and logical NOT (!).

5. What is an assignment operator? [1 Mark]

Answer: An assignment operator (=) is used to assign the value of an expression on the right to a variable on the left.

6. What are increment and decrement operators? [1 Mark]

Answer: Increment (++) and decrement (--) operators are unary operators used to increase or decrease the value of a variable by 1.

7. What is operator precedence? [1 Mark]

Answer: Operator precedence determines the order in which different operators are evaluated in an expression.

8. What is operator associativity? [1 Mark]

Answer: Operator associativity defines the direction (left-to-right or right-to-left) in which operators of the same precedence level are evaluated.

9. What is a library function? [1 Mark]

Answer: A library function is a pre-written, built-in function available in standard C libraries to perform common tasks.

10. Which header file is required for standard input/output library functions? [1 Mark]

Answer: The <stdio.h> header file is required for standard input/output functions.

11. Name the formatted input function used to read data from the console. [1 Mark]

Answer: The formatted input function is scanf().

12. Name the formatted output function used to display data on the console. [1 Mark]

Answer: The formatted output function is printf().

13. What is an unformatted input/output operation? [1 Mark]

Answer: An unformatted input/output operation reads or writes data without requiring format specifiers.

14. Give an example of an unformatted input function for characters. [1 Mark]

Answer: An example of an unformatted input function for characters is getchar().

15. Give an example of an unformatted output function for characters. [1 Mark]

Answer: An example of an unformatted output function for characters is putchar().

16. What does the format specifier %d represent in printf()? [1 Mark]

Answer: The format specifier %d represents a signed decimal integer.

17. What does the format specifier %f represent? [1 Mark]

Answer: The format specifier %f represents a floating-point number.

18. Which operator has the highest precedence among arithmetic, relational, and logical operators? [1 Mark]

Answer: Arithmetic operators generally have higher precedence than relational and logical operators.

19. What is the result of a relational expression if the condition is false? [1 Mark]

Answer: The result of a relational expression when false is 0.

20. What is the function of gets() in C? [1 Mark]

Answer: The gets() function is an unformatted input function used to read a string from the standard input.

Section B — 2/3 Mark Questions

1. Differentiate between prefix and postfix increment operators with an example. [2 Marks]

Answer: In the prefix increment (++x), the value of the variable is incremented first and then used in the expression. In the postfix increment (x++), the current value is used in the expression first and then incremented.

2. Explain the concept of operator precedence with a brief example. [2 Marks]

Answer: Operator precedence determines the priority of operators when evaluating expressions containing multiple operators. For example, in the expression 10 + 5 * 2, multiplication has higher precedence than addition, so it evaluates to 10 + 10 = 20 rather than (10 + 5) * 2 = 30.

3. What are compound assignment operators? Give two examples. [2 Marks]

Answer: Compound assignment operators combine an arithmetic operation with an assignment. They provide a shorthand way to update a variable's value. Examples include += (e.g., x += 5 is equivalent to x = x + 5) and *= (e.g., y *= 2 is equivalent to y = y * 2).

4. Differentiate between formatted and unformatted input/output functions. [3 Marks]

Answer: Formatted I/O functions use format specifiers like %d or %f to read and write data in a specified format (e.g., printf(), scanf()). Unformatted I/O functions deal with characters or strings directly without format specifiers (e.g., getchar(), putchar()). Formatted functions offer greater control over data presentation, whereas unformatted functions are simpler and faster for basic character handling.

5. Explain the working of logical AND (&&) and logical OR (||) operators with truth table concepts. [3 Marks]

Answer: The logical AND (&&) operator returns true (1) only if both operands are true; if any operand is false, it returns false (0). The logical OR (||) operator returns true (1) if at least one operand is true; it returns false (0) only when both operands are false.

6. Write the purpose of scanf() and explain its syntax components. [3 Marks]

Answer: The scanf() function reads formatted input from the standard input device. Its syntax is scanf("format_string", &variable); where format_string specifies the data type using format specifiers and &variable provides the memory address where the read value is stored.

7. What are library functions? Why are they useful in C programming? [2 Marks]

Answer: Library functions are pre-defined functions grouped into standard header files like <stdio.h> or <math.h>. They are useful because they save development time, optimize code performance, and provide reliable implementations for common mathematical, input/output, and string-handling tasks.

8. Explain the role of relational operators in conditional execution. [2 Marks]

Answer: Relational operators (such as >, <, ==) compare two numerical or character values to test conditions. The outcome of a relational expression is either true (1) or false (0), which is directly used by control flow statements like if or while to make decisions.

9. How do putchar() and getchar() function? Explain with a short description. [2 Marks]

Answer: getchar() is an unformatted input function that reads a single character from the standard input device and returns it. putchar() is an unformatted output function that writes a single character to the standard output device.

10. Explain associativity with an example of an operator having left-to-right associativity. [3 Marks]

Answer: Associativity dictates the evaluation direction when operators of the same precedence appear in an expression. Arithmetic operators like +, -, *, and / have left-to-right associativity. For example, in 10 - 4 - 2, because subtraction is left-to-right, it is evaluated as (10 - 4) - 2 resulting in 4, rather than 10 - (4 - 2) which would result in 8.

Section C — 5 Mark Questions

1. Explain the classification of operators in C with detailed descriptions and examples for arithmetic, relational, and logical operators. [5 Marks]

Answer:

Introduction

Operators are symbols that tell the compiler to perform specific mathematical, relational, or logical manipulations on data variables and constants.

Arithmetic Operators

These operators perform fundamental mathematical calculations. C supports binary operators like + (addition), - (subtraction), * (multiplication), / (division), and % (modulus reminder), as well as unary operators.

Relational Operators

Relational operators are used to compare two values to determine their relationship. The result is always a boolean representation where 1 indicates true and 0 indicates false. Common relational operators include <, <=, >, >=, ==, and !=.

Logical Operators

Logical operators combine multiple conditions or expressions. The logical AND (&&) requires all conditions to be true, logical OR (||) requires at least one condition to be true, and logical NOT (!) inverts the truth value of an expression.

Example

If a = 10 and b = 5, then the arithmetic expression a + b results in 15, the relational expression a > b evaluates to 1 (true), and the logical expression (a > b) && (b > 0) evaluates to 1.

2. Discuss operator precedence and associativity in C expressions with rules, examples, and how they dictate evaluation order. [5 Marks]

Answer:

Introduction

When an expression contains multiple operators, the compiler relies on operator precedence and associativity rules to determine the sequence in which operations are performed.

Operator Precedence

Precedence establishes the hierarchy of execution among different operator types. For instance, multiplication and division possess higher precedence than addition and subtraction. Consequently, they are computed first regardless of their position in the expression string.

Operator Associativity

When operators of identical precedence level appear consecutively in an expression, associativity dictates the evaluation direction. Most arithmetic, relational, and logical operators exhibit left-to-right associativity, while assignment operators evaluate from right to left.

Detailed Example

Consider the expression: result = 10 + 5 * 2 - 4 / 2; First, multiplication and division are performed due to higher precedence: 5 * 2 = 10 and 4 / 2 = 2. The expression simplifies to 10 + 10 - 2. Next, applying left-to-right associativity for addition and subtraction: (10 + 10) - 2 = 20 - 2 = 18.

3. Explain formatted input and output operations in C with reference to printf() and scanf(), including format specifiers and modifiers. [5 Marks]

Answer:

Introduction

Formatted input and output operations allow data to be read and displayed in a human-readable, structured format using format specifiers.

Formatted Output: printf()

The printf() function writes output to the standard output device. It accepts a format string followed by a comma-separated list of variables. Format specifiers define how the corresponding variable values are rendered.

Formatted Input: scanf()

The scanf() function reads input from the standard keyboard. It requires format specifiers to interpret the incoming character stream and memory address operators (&) to store the values into variables.

Common Format Specifiers

  • %d: Signed decimal integer
  • %f: Floating-point number
  • %c: Single character
  • %s: Character string

Example

The statement printf("Age is %d and Salary is %.2f", age, salary); prints the integer variable age and formats the floating-point variable salary to two decimal places.

4. Explain unformatted input and output operations in C, detailing function types, syntax, and working mechanisms for character and string handling. [5 Marks]

Answer:

Introduction

Unformatted input and output operations deal directly with raw characters and strings without requiring format specifiers, making them efficient for basic character manipulation.

Character Input/Output Functions

Functions such as getchar() and putchar() handle single characters. getchar() reads a single character from the standard input stream, while putchar() writes a single character to the standard output stream.

String Input/Output Functions

Functions like gets() and puts() handle entire character strings. gets() reads a line of text until a newline character is encountered, and puts() outputs a string followed by an automatic newline.

Advantages and Limitations

Unformatted functions are faster and simpler for basic character processing. However, they lack formatting control and require caution regarding buffer overflow, particularly with older functions like gets().

Example

char ch = getchar(); putchar(ch); reads a single character typed by the user and immediately displays it back on the console.

5. Write a comprehensive note on library functions in C, covering their classification, header files, and usage in arithmetic, input/output, and general operations. [5 Marks]

Answer:

Introduction

Library functions are pre-written routines provided by the C standard library to perform standard programming tasks, eliminating the need to write custom code for routine operations.

Classification and Header Files

Standard library functions are grouped into specific header files based on their functionality. Programmers must include the corresponding header file using the #include directive before invoking any library function.

Key Categories of Library Functions

  • Input/Output Functions: Included in <stdio.h> (e.g., printf, scanf).
  • Mathematical Functions: Included in <math.h> (e.g., sqrt, pow, sin).
  • Character Handling Functions: Included in <ctype.h> (e.g., toupper, tolower).
  • String Manipulation Functions: Included in <string.h> (e.g., strlen, strcpy).

Example of Mathematical Library Functions

The sqrt() function calculates the square root of a number, and pow() computes a base raised to an exponent. Using double result = sqrt(25.0); invokes the math library function to compute the square root and store 5.0 in the result variable.


xxx

Did this help you understand better?

Your feedback improves the quality of this resource for everyone.