Home arrow C and C++ Tips
C and C++ Tips
Static Casting in C++ PDF
Thursday, 05 December 2013
If you want to type cast in C++ you might want to use static_cast. See next example:

static_cast
#include <iostream>

using namespace std;

int main(int argc, const char * argv[])
{

    float var1 = 1.5;

    cout << "var1 = " << static_cast<int>(var1) << endl;
 
    return 0;
}

In this example we've type casted var1 to int.

 
Generate Random Numbers in C++ PDF
Thursday, 05 December 2013
If you want to generate a random number in C++ you can use arc4random() function. For example if you want to simulate roll of a dice you can use the following code:

dice_roll.cc
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main(int argc, const char * argv[])
{

    srand((unsigned int)time(0));

    int var1 = arc4random() % 6 + 1 ;

    cout << "var1 = " << var1 << endl;
 
    return 0;
}

Note: The numbers generated are not really true random numbers.

Last Updated ( Thursday, 05 December 2013 )
 
Display True or False values for Booleans instead of 0 and 1 in C++ PDF
Tuesday, 24 September 2013
As you probably know C++ let you define boolean variables (having the only possible values 0 or 1). You can set cout function to display true or false values instead of 0 or 1 using cout.setf(ios_base::boolalpha) call:

example_true_false_bools.cc
#include <iostream>

using namespace std;

int main(int argc, const char * argv[])
{
    cout.setf(ios_base::boolalpha);
   
    bool var1 = true;
    bool var2 = false;
   
    cout << "var1 = " << var1 << endl;
    cout << "var2 = " << var2 << endl;

    return 0;
}


Last Updated ( Tuesday, 24 September 2013 )
 
Solve Multiple Definition Error When Including C Header files PDF
Sunday, 26 February 2012
If you have many C files and by mistake you've included a header file multiple time when compiling the program you will get an error. To solve this problem use #include guards (also called macro guard) to define each header file.

Example:

file1.h
#ifndef _INC_FILE1
#define _INC_FILE1

//... your header file

# endif

That way if for example you have multiple inclusions of file1.h, you will not get a compilation error, well the preprocessor will skip to the end of the second (definition) inclusion of this file.

 
Print UNICODE Chars in C/C++ PDF
Saturday, 12 November 2011
For that purpose we will use '/u' character. UNICODE-8 is on 2 bytes, 4 digits. See next example:

unicode_example_in_c.cc
#include <iostream>

using namespace std;

int main(void)
{
  char const *greek = "\u03A9\u03A8\u03EA";
  printf("char: %s\n", greek);
}


Last Updated ( Saturday, 12 November 2011 )
 
Convert an Integer to String in C++ PDF
Tuesday, 17 May 2011
We can use a stringstream variable for that. See next example:

example1
#include <iostream>
#include <sstream>

using namespace std;

int main(void)
{
  stringstream sstream;
  int var=100;

  sstream << var;
  cout << sstream.str() << endl;
}


If we later asign other value to sstream variable, we must first clear it. See next example:


example2
#include <iostream>
#include <sstream>

using namespace std;

int main(void)
{
  stringstream sstream;
  int var=100, var2=200, var3=300;

  sstream << var;
  cout << sstream.str() << endl;

  sstream << var2;
  cout << sstream.str() << endl;

  sstream.str(""); // this will clear sstream var
  sstream << var3;
  cout << sstream.str() << endl;
}


 

Other BSD Systems

OpenBSD

Misc

Solaris

Polls

Best BSD firewall?