0

I'm told the following C code

#define ADD(a, b) a + b

// example function
void foo()
{
  int i = ADD(1, 2); // add two ints
  double d = //doubles
    ADD(3.4, 5.6);

  int sly = ADD(1, 2) * 3; // not what it appears to be
}

converts to this Java code

package demo;

public class DemoTranslation {
    public static int add(int a, int b) {
        return a + b;
    }

    public static double add(double a, double b) {
        return a + b;
    }

    /**
     * example function
     */
    public static void foo() {
        int i = add(1, 2); // add two ints
        double d = /* doubles */ add(3.4, 5.6);

        int sly = 1 + 2 * 3; // not what it appears to be
    }
}

1+2*3 in java = 7. How does the C code produce that and not 9?

5
  • 3
    C macros are pure textual replacement. And that particular C macro is buggy due to missing parenthesis Commented Feb 5, 2023 at 18:14
  • (1+2)*3 = 9, 1+(2*3)=7 Commented Feb 5, 2023 at 18:17
  • could you provide an answer that explains why the c code says ADD(1,2) * 3 = 9 and not 7? Commented Feb 5, 2023 at 18:18
  • ‘Converts’? By what conversion process? Commented Feb 5, 2023 at 18:48
  • @user3840170: Conversion by human translation. Sometimes humans do things too, not just computers. Commented Feb 5, 2023 at 19:47

1 Answer 1

10

C macro replacement operates on lexical tokens, at a lower level than semantic analysis of the program.

Given #define ADD(a, b) a + b, the source text ADD(1, 2) * 3 is replaced by 1 + 2 * 3, which is evaluated as 1+(2•3) = 7.

Macro replacement was created for convenience in early primitive programming environments. As such, some of the motivation for developing it was simply typing convenience (editing source files could be a burdensome task in hardware of the era), and additional uses of it grew, including using macros to represent simple expressions. To deal with the fact that macros perform lexical substitution rather than semantic functions, C programmers have learned to parenthesize arguments in macro replacement lists as well as the entire list when macros are being used for expressions. So a C programmer would define ADD as:

#define ADD(a, b) ((a) + (b))

Then ADD(1, 2) * 3 will be replaced by ((1) + (2)) * 3, which will be evaluated as (1+2)•3 = 9.

Sign up to request clarification or add additional context in comments.

3 Comments

I don't understand. Doesn't Add(1,2) = 3? so why would add(1,2) * 3 = 7?
@DCR: Re “Doesn't Add(1,2) = 3”: No, Add(1,2) does not equal 3, nor does ADD(1,2). ADD is not a function nor an instruction to evaluate an expression. ADD is a macro; it is a direction to the C implementation to replace ADD(1, 2) with the replacement list, so it is replaced with 1 + 2. In the context of ADD(1, 2) * 3, that replacement gives 1 + 2 * 3. No expression evaluation is performed during this. Only after replacement is complete does the C implementation perform semantic analysis and interpret 1 + 2 * 3 as an expression.
ADD(1,2) * 3 = 7 because the substitution is 1 + 2 * 3 which is 7.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.