1) # define cube(x)(x*x*x)
main()
int x=2. y, z;
y= cube(++x);
z= ++y + 386 / cube(++x);
printf("%d %d %d", ++x, y, z);


convert into hexadecimal
2) a = 0*aa b = a<<1
a) b=a
b) b= 2a

Dani AI

Generated

Short answer: the original snippet is not portable — the macro causes multiple evaluations of an argument that has side effects, so the C standard does not guarantee any particular numeric result and you cannot meaningfully "convert it to hexadecimal" for a single correct answer. That is the point made by and unpacked by (the problem is the textual substitution a macro performs). rightly corrected his initial reading after that.

A safe, definite version computes each increment or call in its own statement so values are well sequenced. Example (defined behavior, prints decimal then hex):

#include <stdio.h>

static inline int cube(int v) { return v * v * v; }

int main(void) {
    int x = 2, y, z;
    x = x + 1;            /* defined increment */
    y = cube(x);
    x = x + 1;            /* another defined increment */
    z = ++y + 386 / cube(x);
    int final_x = ++x;    /* compute final x separately */

    printf("decimal: %d %d %d\n", final_x, y, z);
    printf("hex: %x %x %x\n", final_x, y, z);
    return 0;
}

Best-practice guidance:

  • Replace function-like macros that evaluate their argument more than once with an inline function to avoid accidental multiple side effects.
  • Never pass expressions with side effects (++, --, function calls that modify state) to macros that expand their argument multiple times.
  • Use compiler warnings (GCC/Clang: -Wall -Wextra and, on older GCCs, -Wsequence-point) to catch suspicious unsequenced modifications.
  • For the second part: a << 1 behaves like a * 2 only when the operation does not cause overflow and types are appropriate. For unsigned integers it is equivalent modulo the type width; for signed integers shifting into the sign bit or shifting negative values is undefined. See the C language undefined-behavior and shift-operator rules for details: and Shift operators.

Recommended Answers

All 7 Replies

Your question seems clumsily constructed and you haven't used code tags. Be specific when asking questions.

This is homework assignment and gayatri apparently wants us to do it for him/her.

The code you posted will have undefined behavior. cube(++x) could result in different answers on different compilers, so the ourput can not be determined exactly.

Hi i am also new to C

I think ans may be

X=2, Y=8,z=61

We wont do your homework for you, and please use code tags in future please. Check the rules and welcome guide for details.

The code you posted will have undefined behavior. cube(++x) could result in different answers on different compilers, so the ourput can not be determined exactly.

Why? It increments i, calls cube with the new value, then returns the answer to y. What's undefined about it? i isn't modified twice.

Why? It increments i, calls cube with the new value, then returns the answer to y. What's undefined about it? i isn't modified twice.

That's because cube is not a function name: it's a macros. We (not me, I never use macros) have:

# define cube(x)(x*x*x)
int x = 2;
y= cube(++x);
is
y = (++x*++x*++x);

But operator ++ side effect (incr x) committing point is the outer expression terminator - semicolon. So every factor may be equal to 3, 4, or 5!..

My bad. I thought he wrote a function. I didn't know the macro existed, never needing it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.