Showing posts with label fundamental.C/Cpp. Show all posts
Showing posts with label fundamental.C/Cpp. Show all posts

Thursday, February 2, 2012

C/C++ Exercise: array and pointer

#include <iostream>
#include <conio.h>

using std::cin;
using std::cout;
using std::endl;

int main(){
 cout << "http://dev-microsoft.blogspot.com/" << endl;
 cout << "C/C++ Exercise: array and pointer" << endl << endl;

 const int SIZE = 5;
 int number[SIZE] = {0, 1, 2, 3, 4};

 cout << "address of &number[i] - " << endl;
 for(int i = 0; i < SIZE; i++ ){
  cout << &number[i] << ":";
 }
 cout << endl;

 cout << "accessed using pointer - " << endl;
 int *pnumber = &number[0];
 for(int i = 0; i < SIZE; i++ ){
  cout << pnumber++ << ":";
 }
 cout << endl;
 
 cout << endl;
 system("PAUSE");
 return 0;

}

C/C++ Exercise: array and pointer

Tuesday, January 31, 2012

C/C++ Exercise: sizeof() and pointer

C/C++ Exercise: sizeof() and pointer
#include <iostream>
#include <conio.h>

using std::cin;
using std::cout;
using std::endl;

int main(){
 cout << "http://dev-microsoft.blogspot.com/" << endl;
 cout << "C/C++ Exercise: sizeof" << endl << endl;

 cout << "sizeof(int): " << sizeof(int) << endl;
 cout << "sizeof(char): " << sizeof(char) << endl;
 cout << endl;

 //Array
 char chArray[10];
 cout << "sizeof array: " << sizeof(chArray) << endl;
 cout << "sizeof a element in array: " << sizeof(chArray[0]) << endl;
 cout << "number of element in array: " << sizeof(chArray)/sizeof(chArray[0]) << endl;
 cout << endl;

 //pointer to int
 int i = 7;
 int* ptInt = &i;
 cout << "ptInt = " << ptInt << endl;
 cout << "*ptInt = " << *ptInt << endl;
 cout << "sizeof(ptInt): " << sizeof(ptInt) << endl;
 cout << "sizeof(*ptInt): " << sizeof(*ptInt) << endl;
 cout << endl;

 //pointer to char
 char* ptChar ("Hello World!");
 cout << "ptChar = " << ptChar << endl;
 cout << "*ptChar = " << *ptChar << endl;
 cout << "sizeof(ptchar): " << sizeof(ptChar) << endl;
 cout << "sizeof(*ptchar): " << sizeof(*ptChar) << endl;
 cout << endl;

 cout << endl;
 system("PAUSE");
 return 0;

}

Sunday, January 29, 2012

C/C++ Exercise: cout number in dec, hex, oct format using std::

C/C++ Exercise: cout number in dec, hex, oct format using std::
#include <iostream>
#include <conio.h>

using std::cin;
using std::cout;
using std::endl;

int main(){
 cout << "http://dev-microsoft.blogspot.com/" << endl;
 cout << "C/C++ Exercise: cout number in dec, hex, oct format using std::" << endl << endl;

 int number = 1024;
 cout << "dec: " << std::dec << number << endl;
 cout << "hex: " << std::hex << number << endl;
 cout << "oct: " << std::oct << number << endl;

 cout << endl;
 system("PAUSE");
 return 0;

}

C/C++ Exercise: Initializing pointer

C/C++ Exercise: Initializing pointer
#include <iostream>
#include <conio.h>

using std::cin;
using std::cout;
using std::endl;

int main(){
 cout << "http://dev-microsoft.blogspot.com/" << endl;
 cout << "C/C++ Exercise: Initializing pointer" << endl << endl;

 int number = 10;
 int *p_number = &number;
 cout << "pointer to address of another variable" << endl;
 cout << "p_number: " << p_number << endl;
 cout << "*p_number: " << *p_number << endl;
 cout << endl;

 int *p_nullptr = nullptr;
 cout << "pointer to nullptr" << endl;
 cout << "p_nullptr: " << p_nullptr << endl;
 //cout << "*p_nullptr: " << *p_nullptr << endl; // <- Invalid
 /* Will cause Unhandled exception at 0x003F13AE in Win32ConsoleApplication.exe:
  *  0xC0000005: Access violation reading location 0x00000000.
  */
 cout << endl;

 //Check if pointer point to anything
 if(p_number){
  cout << "p_number point to something" << endl;
 }else{
  cout << "p_number point to nothing" << endl;
 }
 cout << endl;

 if(p_nullptr){
  cout << "p_nullptr point to something" << endl;
 }else{
  cout << "p_nullptr point to nothing" << endl;
 }
 cout << endl;


 cout << endl;
 system("PAUSE");
 return 0;

}


nullptr represents a null pointer value. Use a null pointer value to indicate that an object handle, interior pointer, or native pointer type does not point to an object.

The nullptr keyword is equivalent to Nothing in Visual Basic and null in C#.

C/C++ Exercise: Initializing two-dimension array

#include <iostream>
#include <conio.h>

using std::cin;
using std::cout;
using std::endl;

int main(){
 cout << "http://dev-microsoft.blogspot.com/" << endl;
 cout << "C/C++ Exercise: Initializing two-dimension array" << endl << endl;

 const int size_i = 3;
 const int size_j = 4;
 int a[size_i][size_j] = 
 {
  {0, 1, 2, 3},
  {10}
 };

 cout << endl;
 cout << "Accessed as two dimension array" << endl;
 for(int i = 0; i < size_i; i++){
  for(int j = 0; j < size_j; j++){
   cout << a[i][j] << " ";
  }
  cout << endl;
 }

 cout << endl;

 int *p = *a;
 cout << "Accessed by pointer" << endl;
 for(int i = 0; i < size_i * size_j; i++){
  cout << i << ":" << *(p + i) << " ";
 }
 cout << endl;

 cout << endl;
 system("PAUSE");
 return 0;

}

C/C++ Exercise: Initializing two-dimension array

C/C++ Exercise: Access two-dimension array by pointer

#include <iostream>
#include <conio.h>

using std::cin;
using std::cout;
using std::endl;

int main(){
 cout << "http://dev-microsoft.blogspot.com/" << endl;
 cout << "Exercise: Access two-dimension array using pointer" << endl << endl;

 const int size_i = 3;
 const int size_j = 4;
 int a[size_i][size_j];
 

 //init array
 for(int i = 0; i < size_i; i++){
  for(int j = 0; j < size_j; j++){
   a[i][j] = i * 10 + j;
  }
 }

 cout << endl;
 cout << "Accessed as two dimension array" << endl;
 for(int i = 0; i < size_i; i++){
  for(int j = 0; j < size_j; j++){
   cout << a[i][j] << " ";
  }
  cout << endl;
 }

 cout << endl;

 int *p = *a;
 cout << "Accessed by pointer" << endl;
 for(int i = 0; i < size_i * size_j; i++){
  cout << i << ":" << *(p + i) << " ";
 }
 cout << endl;

 cout << endl;
 system("PAUSE");
 return 0;

}

C/C++ Exercise: Access two-dimension array by pointer

Exercise of cin.getline()

The function getline(_Str, _Count, _Delim) Gets a line from the input stream.
where:
  • _Str: A string in which to write.
  • _Count: The number of characters to read from strbuf.
  • _Delim: The character that should terminate the read if it is encountered before _Count.


Exercise:
#include <iostream>
#include <conio.h>

using std::cin;
using std::cout;
using std::endl;

int main(){
 cout << "http://dev-microsoft.blogspot.com/" << endl;
 cout << "Exercise cin.getline()" << endl << endl;

 const int MAX = 10;
 char CharArray[MAX];

 int ch;
 do{
  cout << "Type something: " << endl;
  cin.getline(&CharArray[0], 5, '\n');
  cout << "CharArray: " << CharArray << endl << endl;

  cout << "Retry? <y/n>" << endl;
  ch = _getch();
  ch = toupper( ch );
 }while(ch == 'Y');

 return 0;
}


Exercise of cin.getline()

Size of String, char array

If you declare char array with pre-defined size, the size is number of char plus Termination Charcter, /0. So, in the fllowing case:

char CharArray[10];

The maxmum number of char can be stored in CharArray is 9.
Size of String, char array

Thursday, January 26, 2012

Basic usage of enum in C++

#include<iostream>

using std::cout;
using std::endl;

int main(){
 cout << "http://dev-microsoft.blogspot.com/" << endl << endl;

 enum Day {SUN, MON, TUE, WED, THU, FRI, SAT};

 cout << SUN << endl;
 cout << MON << endl;
 cout << TUE << endl;
 cout << WED << endl;
 cout << THU << endl;
 cout << FRI << endl;
 cout << SAT << endl;

 cout << endl;
 system("PAUSE");
}

Basic usage of enum in C++


Compare to Basic usage of enum class in CLR C++

Initializing array in C++

Initializing array in C++
#include <iostream>

using std::cout;
using std::endl;

int main(){

 int array1[10];
 array1[0] = 100;
 int array2[10] = {201, 202, 203, 204, 205, 206, 207, 208, 209, 200};
 int array3[10] = {301, 302, 303};

 cout << "http://dev-microsoft.blogspot.com/" << endl << endl;
 cout << "Array without initializing, all elements contain junk value!" << endl;
 for(int i = 0; i < 10; i++){
  cout << array1[i] << " ";
 }

 cout << endl << endl;
 cout << "Array with all element initialized." << endl;
 for(int i = 0; i < 10; i++){
  cout << array2[i] << " ";
 }

 cout << endl << endl;
 cout << "Array with partial initialized, un-initialized elemented will be set 0." << endl;
 for(int i = 0; i < 10; i++){
  cout << array3[i] << " ";
 }

 cout << endl << endl;
 system("PAUSE");
}

Monday, January 16, 2012

static variable

static variable is a variable that has been allocated statically — whose lifetime extends across the entire run of the program. If no initial value have been provided when declare it, it will be initialized for you.

Example:
// ConsoleApplication1.cpp : Defines the entry point for the console application.
//

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

using namespace std;

void sub();

int _tmain(int argc, _TCHAR* argv[])
{
 cout << "First run - " << endl;
 sub();
 cout << "Second run - " << endl;
 sub();

 system("PAUSE");
 return 0;
}

void sub(){
 int i;
 static int statici;

 cout << "before assign in sub()" << endl;
 cout << "i = " << i << endl;
 cout << "statici = " << statici << endl;

 i = 1;
 statici = 2;

 cout << "after assign in sub()" << endl;
 cout << "i = " << i << endl;
 cout << "statici = " << statici << endl;
 cout << endl;
}


Example of static variable

Scope Resolution Operator (::)

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

using namespace std;

//define Global variable
int i = 10;

int _tmain(int argc, _TCHAR* argv[])
{
 //define variable inside function main()
 int i = 5;


 cout << "i : " << i << endl;
 cout << "::i : " << ::i << endl;

 system("PAUSE");
 return 0;
}


Using of Scope Resolution Operator (::)

Tuesday, January 10, 2012

typeid(expression)

typeid(expression) return an type_info object of expression. To read get the name of the type_info, call it's name() method.

Example:
typeid(expression)
// 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 << "typeid(): " << endl << endl;

 cout << "typeid(0.1234): " << typeid(0.1234).name() << endl;
 cout << "typeid(45): " << typeid(45).name() << endl;
 cout << "typeid(0.1234 * 45): " << typeid(0.1234 * 45).name() << endl;

 short shortArray[4];


 cout << "typeid(shortArray): " << typeid(shortArray).name() << endl;
 cout << "typeid(\"Hello\"): " << typeid("Hello").name() << endl;
 cout << "typeid(true): " << typeid(true).name() << endl;

 system("PAUSE");
 return 0;
}




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;
}