374 views
11 11 votes

Consider the statement:

double ans = 18.0 / squared(2 + 1);

For each of the following macro definitions, find the value of $\texttt{ans}$.

#define squared(x) x*x
#define squared(x) (x*x)
#define squared(x) (x)*(x)
#define squared(x) ((x)*(x))

Which option gives the correct values in order?

  1. $\texttt{12, 3.6, 18, 2}$
     
  2. $\texttt{2, 2, 2, 2}$
     
  3. $\texttt{12, 12, 18, 2}$
     
  4. $\texttt{18, 3.6, 18, 2}$

4 Answers

1 1 vote

For:

$\texttt{#define squared(x) x*x}$

The statement becomes:

$\texttt{18.0 / 2 + 1 * 2 + 1}$

So:

$\texttt{9 + 2 + 1 = 12}$

 

For:

$\texttt{#define squared(x) (x*x)}$

The statement becomes:

$\texttt{18.0 / (2 + 1 * 2 + 1)}$

So:

$\texttt{18 / 5 = 3.6}$

 

For:

$\texttt{#define squared(x) (x)*(x)}$

The statement becomes:

$\texttt{18.0 / (2 + 1) * (2 + 1)}$

So:

$\texttt{6 * 3 = 18}$

 

For:

$\texttt{#define squared(x) ((x)*(x))}$

The statement becomes:

$\texttt{18.0 / ((2 + 1) * (2 + 1))}$

So:

$\texttt{18 / 9 = 2}$


Answer: A

0 0 votes
  1. #define squared(x) x*x → 12
  2. #define squared(x) (x*x) → 3.6
  3. #define squared(x) (x)*(x) → 18
  4. #define squared(x) ((x)*(x)) → 2

Final answer: 12, 3.6, 18, 2

option = A

 

 

Answer:
Position:
Show:

Related questions

11 11 votes
3 3 answers
453
453 views
GO Classes asked Jul 1
453 views
Consider the macro:#define square(x) (x*x)Now consider the statement:c = square(a + b);After macro expansion, which expression is produced?$\texttt{c = ((a + b) * (a + b)...
11 11 votes
2 2 answers
452
452 views
GO Classes asked Jul 1
452 views
What is the output of the following code?#include <stdio.h void func(int a, int *bptr) { a = 42; *bptr = 42; return; } int main(void) { int x = 100, y = 100; func(x, &y);...
7 7 votes
3 3 answers
401
401 views
GO Classes asked Jul 1
401 views
What does the following code do?#include <stdio.h int main() { char c; while ((c = getchar()) != EOF) { if (c >= 'A' && c <= 'Z') c = c - 'A' + 'a'; putchar(c); } return ...
9 9 votes
2 2 answers
373
373 views
GO Classes asked Jul 1
373 views
Consider the following program:#include <stdio.h int main(void) { char ch; printf("Enter your input: "); while ((ch = getchar()) != 'C') { putchar(ch); } return 0; }For t...