samedi 27 juillet 2019

Can we implement preprocessor level decision-making in C and C++ by combining #if-#else if and macros with arguments?

I wish to implement preprocessor level 'loops' which use #if #else directives as decision making statements so that I can exponentiate a number (base) to a certain power without using any functions and then using the main() and printf() statements to only print the output. Is this possible in C or C++?

I've tried this and the following error occurs:'#' is not followed by a macro parameter

(In reality I've written the entire #define statement on ONE line(till #endif).So please ignore that.)

/* C program to exponentiate a number to a certain power without using functions.(Using main() and printf() to ONLY output the final result.) */

 #define exp(base,power) #if power exp(base * base,power-1) #else 
 #include<stdio.h> void main(){printf("the exponentiated form is:%d",base);} #endif

exp(2,3)

I've tried this and the following error occurs:'#' is not followed by a macro parameter

Aucun commentaire:

Enregistrer un commentaire