| Reference C++ by Chirag B Patel - updated April 30, 2006 |
| Table of Content |
| 1.Basic data types |
| 2.Structure, enum, union |
| 1. Basic data types |
|
C++ has many flavors. Whenever you read a C++ help, try to look for ANSI compliant word
as this is the least portion of C++ supported by any C++ compiler of recent times.
Let us understand some basic data types defined in the language. |
| [Type] [Size] [Notation(not ANSI)] [Example] |
| -------------------------------------------- |
| [char] [1 byte] [c] ['c', 'B', 0x9f, -29] |
| [bool] [1 byte] [b] [true, false] |
| [short] [2 byte] [s] [230, -24000, 0x67ef] |
| [int] [depends - 4byte on Win32] [i] [245678, 0xff678e] |
| [long] [4 byte] [l] [7896L] size is >= to int |
| [float] [4 byte] [f] [234.56, -1234.05] |
| [double] [8 byte] [d] [same as float] |
| [long long] [8 byte] [ll] [same as long] |
| [long double] [8 byte] [ld] [same as double] |
| -------------------------------------------- |
|
Using these basic data types, you can replace real time calculations into your programs.
When a variable of one of these types is defined, it can take values limited by the size.
E.g. char cLetter; - takes any value ranging from 0x00 to 0xff i.e. -127 to 128. This range
covers ASCII character-set. Do Google to search.
When more elements are used, the collection is called arrays. E.g.int iTable[230]; - collection of 230 elements of type int. Array of characters is known as string and is a special case array in the language. Now, download a free C++ compiler and build your first application. Write the following from your compiler editor or notepad (windows), and save as "test.cpp" on your hard disk drive.
|
| #include <cstdio> // standard library include for printf |
| using namespace std; // might be necessarry |
| int main ( int argc, char * argv[] ) // starting point function |
| { // starting of main function |
| char cVal = 0x31; // ASCII '1' |
| int iVal; // Not initialized here |
| char szName[12] = {0}; // Puts all 0s in 12 elements |
| printf ( "\ncVal=%c iVal=%d szName=%s\n", cVal, iVal, szName );// output |
| cVal = 'A'; // assign ASCII 'A' |
| iVal = 'A'; // =65 or 0x41(hex) |
| strncpy ( szName, "Chirag", 11 ); // Leave room for NULL('\0') in string |
| printf ( "\ncVal=%c iVal=%d(%x)(%#x) szName=%s\n", cVal, iVal, iVal, iVal, szName );// output |
| } // end of main function |
| [Top] |
| 2. Structure, enum, union |
| [Top] |
| [Top] | Write: [email protected] | Copyright © 2006 Chirag B Patel |