Saturday, January 7, 2012

C/C++ Casting and static_cast

Example:
C/C++ Casting and static_cast
// ConsoleApplication1.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
 cout << "casting: " << endl << endl;

 float f1 = 0.3345;
 float f2 = 12.41;

 float fa = f1 * f2;
 int i1 = f1 * f2;
 int i2 = (int)f1 * (int)f2;
 int i3 = (int)(f1 * f2);
 int i4 = static_cast<int>(f1 * f2);
 int i5 = static_cast<int>(f1) * static_cast<int>(f2);

 cout << "fa = " << fa << endl;
 cout << "i1 = " << i1 << endl;
 cout << "i2 = " << i2 << endl;
 cout << "i3 = " << i3 << endl;
 cout << "i4 = " << i4 << endl;
 cout << "i5 = " << i5 << endl;

 system("PAUSE");
 return 0;
}


Friday, January 6, 2012

Manipulator, setw()

Examples of using setw():
Manipulator, setw()
// ConsoleApplication1.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <iomanip>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
 cout << "Usage of setw(6): " << endl;
 cout << setw(6) << 1234 << endl;
 cout << setw(6) << 1234567890 << endl;
 cout << setw(6) << 0.1 << endl;
 cout << setw(6) << "abc" << endl;
 cout << setw(6) << "abcdefghijklmn" << endl;
 cout << setw(6) << 12.3456789 << endl;
 cout << setw(6) << 123456789.0 << endl;
 cout << setw(6) << 'a' << endl ;

 system("PAUSE");
 return 0;
}


Basic input/output: cin & cout

Example:
Basic input/output: cin & cout
// ConsoleApplication1.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
 int input1;
 float input2;
 char input3;
 
 char retry;
 do{
  cout << "Hello! Enter: int, float and char" << endl;
  cin >> input1 >> input2 >> input3;
  cout << "You enter: " << endl;
  cout << input1 << endl;
  cout << input2 << endl;
  cout << input3 << endl << endl;

  cout << "Retry? <y>" << endl;
  cin >> retry;
 } while ((retry==(char)'y')||(retry==(char)'Y'));

 system("PAUSE");
 return 0;
}


Thursday, January 5, 2012

Wide character - wchar_t

According to Wikipedia:

A wide character is a computer character datatype that generally has a size greater than the traditional 8-bit character. The increased datatype size allows for the use of larger coded character sets.

in C/C++:
The standard library of the C programming language includes a lot of facilities for dealing with wide characters and strings composed of them. The wide characters are defined using datatype wchar_t, which in the original C90 standard was defined as 16-bit value due to historical compatibility reasons. C and C++ compilers that comply with the 10646-1:2000 Unicode standard generally assume 32-bit values.

However, the ISO/IEC 10646:2003 Unicode standard 4.0 says that:

"ANSI/ISO C leaves the semantics of the wide character set to the specific implementation but requires that the characters from the portable C execution set correspond to their wide character equivalents by zero extension."

and that

"The width of wchar_t is compiler-specific and can be as small as 8 bits. Consequently, programs that need to be portable across any C or C++ compiler should not use wchar_t for storing Unicode text. The wchar_t type is intended for storing compiler-defined wide characters, which may be Unicode characters in some compilers."


Example:
Wide character - wchar_t
// ConsoleApplication1.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
 
 cout << "_WCHAR_T_SIZE: " << _WCHAR_T_SIZE << endl;
 cout << "WCHAR_MIN= " << WCHAR_MIN << endl;
 cout << "WCHAR_MAX= " << WCHAR_MAX << endl << endl;

 wchar_t wideChar_heart = L'?';
 cout << wideChar_heart << endl;
 wprintf(L"wideChar_heart: %lc \n", wideChar_heart);

 system("PAUSE");
 return 0;
}



Size and range of integer; INT, SHORT, LONG and LONGLONG

Size and range of integer; INT, SHORT, LONG and LONGLONG

It's tested under Windows 8 (Developer Preview) and Visual Studio 11 Developer Preview.
Size and range of integer; INT, SHORT, LONG and LONGLONG
// ConsoleApplication1.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{

 cout << "_INT_SIZE: " << _INT_SIZE << endl;
 cout << "INT_MIN= " << INT_MIN << endl;
 cout << "INT_MAX= " << INT_MAX << endl << endl;

 cout << "_SHORT_SIZE: " << _SHORT_SIZE << endl;
 cout << "SHRT_MIN= " << SHRT_MIN << endl;
 cout << "SHRT_MAX= " << SHRT_MAX << endl;
 cout << "USHRT_MAX= " << USHRT_MAX << endl << endl;

 cout << "_LONG_SIZE: " << _LONG_SIZE << endl;
 cout << "LONG_MIN= " << LONG_MIN << endl;
 cout << "LONG_MAX= " << LONG_MAX << endl;
 cout << "ULONG_MAX" << ULONG_MAX << endl << endl;

 cout << "_LONGLONG_SIZE: " << _LONGLONG_SIZE << endl;
 cout << "LLONG_MIN= " << LLONG_MIN << endl;
 cout << "LLONG_MAX= " << LLONG_MAX << endl;
 cout << "ULLONG_MAX= " << ULLONG_MAX << endl << endl;

 system("PAUSE");
 return 0;
}


C/C++ Fundamental data type, int

C/C++ Fundamental data type, int

In C/C++, variable of int can be defined and initiated using the form:
int i1 = 10;
int i2(20);


In Windows 8(Developer Preview), it's size id 4 byte.

Example:
C/C++ Fundamental data type, int
// ConsoleApplication1.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
int i1 = 10;
int i2(20);

cout << "size of i1(int): " << sizeof(i1) << endl;
cout << "i1 = " << i1 << endl;
cout << "size of i2(int): " << sizeof(i2) << endl;
cout << "i2 = " << i2 << endl;
system("PAUSE");
return 0;
}

Wednesday, January 4, 2012

Create Win32 C++ Console application using Microsoft Vistal Studio 11 Developer Preview

- Start Microsoft Vistal Studio 11 Developer Preview

- Click New Project... in the start-up page.
New Project...

- Select template of Visuall C++ Win32 Console Application, enter Name, and optionally enter Location, and click OK.
New Project Wizard: Visuall C++ Win32 Console Application

- Review the project settings and click Next.
Project Settings

- Review and accept the default application settings, click Finish.
Application Settings

- The Wizard will generate a default project:
Auto-generated project

- Modify the code:
// ConsoleApplication1.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>


int _tmain(int argc, _TCHAR* argv[])
{
 std::cout << "Hello World!" << std::endl;
 system("PAUSE");
 return 0;
}

Add the code

- Finally, Save, Build and Run the first Visuall C++ Win32 Console Application:
Run