[ C++ ] DAY-3 (number_char, #define and const,)

Posted: January 1, 2011 in C++

//using number to printout character

//there change the number to character

#include <iostream>

int main()
 for(int i=0; i<129; i++)
  std::cout << (char) i << i<< “\t”;
 return 0;

//there using “unsigned char” variable here, so directly change the number to charactor, so here without number printout

#include <iostream>

int main()
 for (unsigned char i=0; i<129; i++)
  std::cout << i<< i<< “\t”;
 return 0;

//#define is a way to define constant. But this way is a outdated way.

Although out of date, but it still has it value for existing. Because it’s so easy to use.

eg:   #define    classes    10

Note: there without the type of the value (int , char …)

When compiler see “classes”, it will directly change to “10”.

//There is other way to define constant, that’s “const“.

To define constant variable, “classes” , we can do like this:

const unsigned short int classes = 10 ;

//So why we should use this way?

// Because it has type “unsigned short int” and it’ll help compiler to define and protect the type of the value using in correct place.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s