View Full Version: #define

C++ Learning Community > C++ Tips > #define


Title: #define
Description: tired of cout?


shortys team - September 6, 2003 11:48 PM (GMT)
#include <iostream.h>
#define print(x) cout<<x;
int main()

{

print("hello world");
return 0;
}

i found another way to print, well not really but you can rename cout<< ;
i was just fooling around with #define

Shadow of the Moon - September 6, 2003 11:58 PM (GMT)
You could also use the C function printf("hello") as well. :)

shortys team - September 7, 2003 12:13 AM (GMT)
yea ya could but does anyone know how to make it so if you put

CODE

#include <iostream.h>
#define print(x, /n) cout<<x;
int main()

{

print("hello world", 1);
return 0;
}
//code doesnt work


something so that if you put 1 then it uses /n but if you put 0 it doesnt??

Shadow of the Moon - September 7, 2003 12:22 AM (GMT)
CODE

#include <iostream.h>
#define print(x,  n) cout<<x if(n==1) cout<<"\n";
int main()

{

print("hello world", 1);
return 0;
}


TheHawgMaster - September 7, 2003 12:27 AM (GMT)
Perhaps you were thinking of:
QUOTE
#include <STDIO.h>
void main {
  char s[] = "Whazzup!";
  int i = 2;
  char c = 'U';
  printf("Whazzup! 2 U : %s %d %c", (char*)&s[0], i, c);
}
Actually there is a more useful function called sprintf that is the same except it takes a char* as its first argument and instead of putting it on the screen it puts it in the spring pointed to by the first argument (Be sure you have enough storage in the string).

Anyway, heres your code fixed:
CODE
#include <iostream.h>
#define print(x,  n) cout<<x if(n==1) cout << endl;
int main()

{

print("hello world", 1);
return 0;
}

Incubator - September 8, 2003 06:50 PM (GMT)
and in your printf() function you dont really need to cast it to a pointer to char, the following works perfectly:

char s[] = "hello";
printf("%s", s);


Shadow of the Moon - September 8, 2003 09:56 PM (GMT)
Actually the names of variables are pointers themselves. They just point to the area in memory where the value of a variable is stored. Especially evident in NULL terminated strings.

Dragon - September 8, 2003 11:11 PM (GMT)
QUOTE
#include <iostream.h>
#define print(x) cout<<x;
int main()

{

print("hello world");
return 0;
}


Why would you want to do something like that, though? To me it seems less work just using cout instead of defining a macro and then using it.

Here's another example of using a macro. I haven't worked with macros for a while, so that might not be correct, but I don't see any errors in the code.

CODE
#include <iostream>

using namespace std;

#define AddNum(A, B) cout << "Answer: " << A + B << endl;

int main()
{
   int X, Y;
   cout << "Enter first number:" << endl;
   cin >> X;
   cout << "Enter second number:" << endl;
   cin >> Y;
   AddNum(X, Y);
   system("pause");
}

TheHawgMaster - September 9, 2003 12:32 AM (GMT)
Some compilers abstract data extra far and treat arrays as a separate data type so they won't work in functions that take char*, I was just protecting against stupid compilers like that.

myork - January 21, 2004 04:29 PM (GMT)


A beginers mistake (or just bad style) that everybody seems to be doing is adding the ';' at the end of a macro.



CODE

#include <iostream.h>
#define print(x) cout<<x;
int main()

{

print("hello world");
return 0;
}


In the above code, the compiler will see 2 ';' after the print statment. In this case the extra ';' is benign but you should try and get in the habbit of not adding ';' onto macros.



Responding to a subsequent post:
How to add the '\n' to the end of line. Try this.
CODE


#include <iostream.h>
#define print(x,  n)      std::cout <<x << ((n==1)?"\n":"")

int main(int argc,char* argv[])
{
   print("hello world", 1);
   return 0;
}







* Hosted for free by InvisionFree