how to define more in c++?

you can define one variable for ex. #define shoutx cout. But if you write for ex. #define kint int. Or #define LNUMBER CALLBACK. Compiler show error. How to define statement, or block of statements to one word. For ex. When you write whilex; compiler reads it as while (a > 10) {
cout << a; a++;}.
1
2
3
#define PROGRAM int main(){ return 0; }

PROGRAM
is perfectly valid. You must keep the whole #define statement in one line. If you end a line with "\", compiler still sees that as one line.
I assume you wanted it to be while (a < 10). Otherwise, your loop would be infinite for unsigned ints and would go for a long time for ints.

#define WHILEX(Var,Num) while(Var < Num){cout << Var; Var++;}

or as mulitple lines

1
2
3
4
5
#define WHILEX(Var,Num) while(Var < Num)\
{\
     cout << Var;\
     Var++;\
} 


Example:

1
2
3
4
5
6
7
8
9
10
#include <iostream>
using namespace std;

#define WHILEX(Var,Num) while(Var < Num){cout << Var; Var++;}

int main()
{
     int a = 0;
     WHILEX(a,10);
}
Usually it is neater and safer to use an inline function for this purpose:
1
2
3
4
5
6
7
inline void WhileX()
{
   while (a < 10) {
      cout << a;
      a++;
   }
}


Coincidentally, this construct is what a 'for' loop is for:
1
2
3
4
for (unsigned a=0; a<10; a++)
{
   cout << a;
}

is the same as
1
2
3
4
5
unsigned a=0;
while (a < 10) { 
   cout << a;
   a++;
}


-Xander
thx a lot I will try these codes!
shacktar wrote:
your loop would be infinite for unsigned ints and would go for a long time for ints.

Are you saying that unsigned types don't wrap around the same way signed types do? All primitive integer data types, signed or not, wrap around (overflow).
why compiler wont define this line? [code] #define declarex LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM) [/CODE]
Last edited on
code was in one line.
What compiler error are you getting?
Are you saying that unsigned types don't wrap around the same way signed types do?

Oops, nevermind, brain fart there.
I´m not going to give any tips, tricks or hints here, but I´d suggest someone to create an article about this preprocessor feature. There might be some people (including me) that are not aware of this feature.
You mean something like this?
http://cplusplus.com/doc/tutorial/preprocessor/
Sheesh, yes. No I feel myself like an idiot.
Topic archived. No new replies allowed.