Hello
I am just testing how to get the fatorial of a number by using MACRO -
Here is my wrong code -

#include <stdio.h>
#include <conio.h>
#define FAC(n) for(typeof(n) i=2; i<=n; i++){f *= i}
void main()
{
int i=5;
int j=FAC(i);
printf("%d",j);
getch();
}

And these are error
Error MACRO.C 7: Expression syntax
Warning MACRO.C 7: Code has no effect
Error MACRO.C 7: Statement missing;

How can I use for loop in Macro please guide me.

Thank you

The immediate cause of the errors are a) the statement typeof(n) in the initialization section of the for loop, and b) the lack of a semi-colon at the end of f *= i. However, this ignores a more serious conceptual error (for which the compiler gives only a warning), in that it assumes that the for() loop will return a value, which is not the case.

What is really happening is that you are making one of the classic C/C++ conceptual errors, namely, confusing macros with functions. While parameterized macros do indeed seem to act like functions under certain circumstances, they are not functions, and treating them as if they were can come back to bite you.

In this case, if you look at the actual code generated by the pre-processor, the statement would become:

int j=for(typeof(i) i=2; i<=i; i++){f *= i};

A few moment's thought should make it clear why this won't work, even aside from the collision of the two i variables.

As an aside, you should never use void as the return value of main(); the correct return type for the main function is int.

When you're using macros like this, please pay attention to exactly what the code looks like when your macro is expanded. The code you have here is equivalent to writing the following:

#include <stdio.h>
#include <conio.h>
void main()
{
int i=5;
int j=for(typeof(i) i=2; i<=n; i++){f *= i};
printf("%d",j);
getch();
}

Obviously this code makes no sense. You can't set a variable equal to a for loop, because it's not a function and therefore doesn't return a value. Furthermore, the variable f doesn't even exist in this context. This sort of thing is simply too complex to do with a macro. What you'll want to do instead is make a function to perform the same task.

#include <stdio.h>

int fac(int parm)
{
    int total = 1;
    int mult;

    for(mult = 2; mult <= parm; mult++)
    {
        total *= mult;
    }

    return total;
}

int main(void)
{
    int i = 5;
    int j = fac(i);
    printf("%d", j);
    return 0;
}

Thank you for considering my question.
I know every issue which can be arise from this type of work and making a UDF and calling that UDF for factorial is one of the best option but, I want to learn and test how can I do this if I want such think to be done by MACRO. How can I return the value of f from MACRO to my printf( ) in main( )
Its all about knowledge I want to gain from this Group.

OK I got it here is the code may someone need again this code will copile and run on GCC only because typeof() is for GCC

#include <stdio.h> 
#include <conio.h> 
#define FAC(n) ({typeof(n) i=1,j=n,f=1; for(; i<=j; i++) {f *= i;}; f;}) 
int main() 
{ 
int x=5; 
int y=FAC(x); 
printf("%d",y); 
getch(); 
return 0; 
}

Thank you every body for this co-operation

because typeof() is for GCC

It must be for a really old version of GCC. I'm running GCC 4.7 and it's telling me that typeof() is undefined. Even if it did, I don't see how this code could possibly compile on any compiler. You're setting y equal to a parenthesized block of code.

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.