It seems that you're using an outdated browser. Some things may not work as they should (or don't work at all).
We suggest you upgrade newer and better browser like: Chrome, Firefox, Internet Explorer or Opera

×
I'm having an exam tomorrow... C language... I am having a little problem understanding the logic of this program. Can anyone explain me how the things are calculated? In particular, the j variable. I don't understand how it becomes 20.
#include <stdafx.h>
#include <stdio.h>
#define A 5
#define MACRO(j,i) i=j/i
#define PR printf("i=%d, j=%d, k=%d\n",i,j,k);
void main()
{
int i=8,j=2,k;
int kraj;
k=MACRO(i,j)*A;
PR;
j/=A;
k+=MACRO(++j*A,i);
PR;
scanf_s("%d",&kraj);
}
Post edited November 30, 2009 by KavazovAngel
This question / problem has been solved by Barefoot_Monkeyimage
I don't see how it could possibly be 20. I think I don't understand the code fully. Can you give us the printout of the two calls to the PR macro?
int i=8,j=2,k;
int kraj;
k=MACRO(i,j)*A;
// expand MACRO to get:
// k=j=i/j*A
// k=j=8/2*5
// k=j=4*5
// k=j=20
// so both k and j are now 20
PR;
// this should print "i=8,j=20,k=20
j/=A;
// j = j / 5, so j is now 4
k+=MACRO(++j*A,i);
// *sigh*, don't they have something more useful to test you on than deliberate obfuscation?
// oh well - you'll probably get something like this in your test then if this is how they think,
// so once you understand what's going on you should practice reading the program
// on your own a few times.
//
// k += i = ++j*5 / i
// this basically says j = j + 1 and i = j*5 / i and k += i
// in other words, j = 5, i = 5*5 / 8 and k += i
// so now j is 5, k is 23, and i is 3
There's plenty of room for me to have made a mistake there, so run the program and see if my results are correct.
I hope this helps you to understand what's going on.
avatar
Barefoot_Monkey: k=MACRO(i,j)*A;
// expand MACRO to get:
// k=j=i/j*A
// k=j=8/2*5
// k=j=4*5
// k=j=20
// so both k and j are now 20

Okay, I've never encountered the macro function before. It makes absolutely no sense to me that the multiplication by A outside the macro call should influence the assignment taking place inside it. What an extraordinarily weird bit of functionality. But then I never learned C, my school taught programming using C++.
avatar
Barefoot_Monkey: k=MACRO(i,j)*A;
// expand MACRO to get:
// k=j=i/j*A
// k=j=8/2*5
// k=j=4*5
// k=j=20
// so both k and j are now 20
avatar
Wishbone: Okay, I've never encountered the macro function before. It makes absolutely no sense to me that the multiplication by A outside the macro call should influence the assignment taking place inside it. What an extraordinarily weird bit of functionality. But then I never learned C, my school taught programming using C++.

MACRO is not a function (persay) but instead an actual macro/preprocessor directive (I think it counts as one of those).
Basically, when you see
#define A 5
That means that every time the preprocessor sees "A" it replaces it with "5", BEFORE compiling.
So MACRO(i,j) becomes i=j/i , BEFORE compiling.
Macros are very useful (even though I almost always would suggest a typedef or an inline if you are doing anything more than a #ifndef), but they are also very difficult to debug and tend to do little but obfuscate code.
avatar
Wishbone: Okay, I've never encountered the macro function before. It makes absolutely no sense to me that the multiplication by A outside the macro call should influence the assignment taking place inside it. What an extraordinarily weird bit of functionality. But then I never learned C, my school taught programming using C++.
avatar
Gundato: MACRO is not a function (persay) but instead an actual macro/preprocessor directive (I think it counts as one of those).

In this context, MACRO is just the name of the macro being defined. I meant that I didn't know about the macro functionality, meaning the ability to define macros in the syntax.
avatar
Gundato: Macros are very useful (even though I almost always would suggest a typedef or an inline if you are doing anything more than a #ifndef), but they are also very difficult to debug and tend to do little but obfuscate code.

Yeah, I'd certainly prefer an actual function. I can imagine the difficulty in debugging code with lots of macros in it. Especially with nested macro calls (I just read about that).
Thank you for the help, guys. :) It is much clearer and easier now. :)
Wish me luck tomorrow. :p