Which of the following are legal statements in 𝐶 programming language? (a)…
2019
Which of the following are legal statements in 𝐶 programming language?
(a) int *P=&44;
(b) int *P=&r;
(c) int P=&a;
(d) int P=a;
Choose the correct option:
- A.
(a) and (b)
- B.
(b) and (c)
- C.
(b) and (d)
- D.
(a) and (d)
Attempted by 810 students.
Show answer & explanation
Correct answer: C
Explanation: Determine whether each declaration is legal in C.
int *P = &44; — Illegal: you cannot take the address of an integer literal like 44. The & operator requires an lvalue (a named variable or object).
int *P = &r; — Legal if r is declared as int: &r has type int* which matches P's type.
int P = &a; — Illegal: &a has type int* (pointer), which cannot be assigned to an int without an explicit cast; the types are incompatible.
int P = a; — Legal if a is declared as int: this copies the integer value of a into P.
Conclusion: The legal declarations are int *P = &r; and int P = a; (assuming r and a are variables of type int).
A video solution is available for this question — log in and enroll to watch it.