675 views
0 0 votes

Consider the code:

#include <stdio.h>
int addsub(int, int);
void display(int);
void show(int, int);
int main() {
    int i = 10, j = 20, k;
    k = addsub(i, j);
    printf("%d ", k);
    display(i);
    show(i, j);
    return 0;
}
int addsub(int c, int d) {
    int x, y;
    x = c - d;
    y = c + d;
    return (x, y);
}
void display(int c, int d) {
    printf("%d %d ", c, d);
}
void show(int c) {
    printf("%d %d ", c);
}

Which of the following is correct about above code?

(A) Error: function addsub

(B) Error: function display

(C) Error: function show

(D) None of the above

1 Answer

Best answer
1 1 vote

Errors in the Original Code:

  1. Function addsub:

    • The function addsub attempts to return two values using (x, y), but in C, the comma operator in a return statement will only return the rightmost value (y). Therefore, this function does not work as intended.
  2. Function display:

    • The display function in the original code is declared to take a single argument (void display(int c, int d);), but it is called with two arguments in main (display(i, j);). This mismatch causes a compilation error.
  3. Function show:

    • The show function in the original code is declared to take two arguments (void show(int c);), but it is called with only one argument in main (show(i);). This mismatch causes a compilation error.

Correct Answer:

Since all three functions (addsub, display, and show) have errors, but the question asks about the errors specifically mentioned in the options, the correct answer, considering the first identified error that appears in the code, is:

(A) Error: function addsub

If you want to identify the exact issues in the original code:

  • (A) Error: function addsub - Correct because the return statement is incorrect.
  • (B) Error: function display - Correct because the function signature and the call do not match.
  • (C) Error: function show - Correct because the function signature and the call do not match.

Given these options and the typical context of such questions, the primary and earliest error in the provided list is (A).

• selected by
1 flag:
✌ Edit necessary (justanotherguy “Function addsub() is working fine. It will return y.”)
Position:
Show:

Related questions

2 2 votes
2 answers 2 answers
922
922 views
Debargha Mitra Roy asked Aug 3, 2024
922 views
Study the following C programm code:#include <stdio.h int main() { static char *p = (char *)malloc(10); return 0; }Which one of the following optiions is true for...
1 1 vote
1 answers 1 answer
444
444 views
Debargha Mitra Roy asked Aug 3, 2024
444 views
Code:#include <stdio.h int abc() { printf("come"); } void main() { int abc(); abc(); (*abc)(); }When executes does - (A) results in compilation error(B) prints "comecome"...