C++ Article: The Basics of Every Program You Will Ever Make By: infinite_reality aka `uN[In$tall]` (c) March 25th, 2004 This is released under TGS-Security, and is therefore TGS property. ~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~ ~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~ [7:36:04AM] ill take yur hole group then ill hack and nuke yur computer [2:09:53AM] <*Rowan-> brb i am gonna download the hacks to hack into your IP ~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~ ~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~ || Purpose: Someone always asks what the basics of C++ are and why they should learn it--so, now they can have a look and decide for themselves without bugging me or anyone else in IRC or forums. Ta-da, die. || Comments, questions, bitching: Please forward all your constructive comments and questions of value to Me, your lord and mastor, Infinite. Email: clientside@hotmail.com Location: www.tgs-security.com www.hackerscenter.com www.hackerlounge.no-ip.com http://lnx.hackerscenter.com/Forums/index.php Please forward all bitchings, complaints and whines to someone who could give a fuck. I dont care for you peons; do not tell me your problems, piss off, and die from a million papercuts to your eyeballs. || Disc{lamer}: Don't a wanker. Don't do anything stupid to get yourself busted. Because when you act like an irresponsible child and get arrested and shit yourself, I'll laugh at you. And you won't be able to blame me, because I didn't tell you to go and haxxor some webserver. I just provided a nice informative piece of documentation. Die. || Copyright: This text is distributed under TGS-Security, so all rights belong to us. Not you. K? Thx. TGS includes (honourary/missing not included) o Th3_R@v3n o NoUse o Phlame o The_IRS o Knightmare o The_Messiah o FanaticalRed o Slarty o Enjoi o Infinite If you're gonna copy this or use it on your site, just ask me via forums or email - chances are I'd say yes, because I'm a nice guy. Don't be an arsefuck and put your name on it, that's just gay and weak and childish and really really lame. || Shout Outs: Gotta send shout outs to Mr.Mind, Raven, Knightmare/DJSplit, asTHma, ShamusTheScott, Subby, myself, VBFavre, Phlame, Messiah, Messiah's Mum, Enjoi, Slarty, Kevin aka DarkImpulse, Dyndrilliac, 2Th3Fing3r, magic, the crew at HackersCenter.com, Brain-Hack.org, and HackerLounge forums, and everyone in/from -[DarK`Nexu$]-. And everyone else I missed putting in at 3am this fine morning. And to my lovely girlfriend, whom I love deeply, and who opposes my involvement in hacking and other illegal activites. || Fuck Yous: ...go to the skiddies, the lamers, and the all-round mindless, media-loving, DDoSing twat arsefucks. Fuck you retards. I don't like you. I don't like what you do. Grow up, fucking skiddies. ~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~ ~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~ CONTENTS: 1.0 -- Preface 1.1 -- "a little insight...", by Moi 1.2 -- introduction to C++ 1.3 -- Iimportant notes to read before going on: 2.0 -- Some Light Reading to Get You Acquainted (simplified) 2.1 -- writing C++ source code 2.2 -- the compiler 2.3 -- the linker 2.4 -- the basics of a C++ source 2.5 -- C++ reserved words (commonly used) 3.0 -- The Basic Theories of C++ 3.0.1 - short introduction 3.0.2 -- Some Notes on this article 3.1 -- input/output streams 3.1.1 -- standard output stream - cout 3.1.2 -- standard input stream - cin 3.1.3 -- standard error stream - cerr 3.2 -- #include directive 3.3 -- escape sequence characters 4.0 -- The Infamous "Hello World" 4.0.1 -- short introduction 4.1 -- example source code 4.2 -- explanation of 5.0 -- C and C++ Style Comments (in-source) 5.0.1 -- short introduction 5.1 -- comment styles 5.1.1 -- C style comments 5.1.2 -- C++ style comments 5.3 -- example source code 5.4 -- explanation of 6a.0 -- Fundamental C++ Data Types, Including Variables and Constants (simplified, skimmed-over-but-not-too-basic version) 6.1 -- definitions 6.2 -- tables of variable information, and explanation 6.3 -- example source code 6b.0 -- Fundamental C++ Data Types, Including Variables and Constants (confusing, in-depth version) 6b.0.1 -- (short introduction) what are data types?: 6b.1 -- explanation of fundamental data types 6b.1.1 -- the integer type 6b.1.2 -- the character type 6b.1.3 -- the boolean 6b.1.4 -- floating-point and double-precision 6b.2 -- derived data types 6b.1.1 -- Arrays 6b.1.2 -- 6.2.2 -- Pointers and References 6b.2.3 -- Strucutres, Unions, and Classes 6b.3 -- Variables (Introduction to) 7.0 -- Declaring and Defining Variables and Constants 7.0.1 -- short introduction 7.1 -- assign/initialise 7.1.1 -- example source code 7.1.2 -- explanation of 7.2 -- typedef 7.2.1 -- example source code 7.3 -- constant types (and using) 7.3.1 -- defining constants 8.0 -- References, and Such Shit ~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~ ~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~ 1.0--PREFACE: 1.1 -- First there was Hacking, which needed Intelligence. Now there are Scripts, which need a PC: Just some words of wisdom before I get into this big mess I brought upon myself. If you are going to hack, you must be doing this for a reason, and I do not care what it is. All hackers have their reasons for doing what they do; the original (and for me, most real) is to satisfy the curiosity of knowledge and systems. Most of what I have learned about UNIX, I have learned on someone else's system (I think I told them before hand i'd be on there...). I like to be hands on. Within this text file, you should get an idea, a grasp, of what "real" hacking is, and come up with your own style. It isn't really hard work, but more or less time, practice and experience that will make you whatever you become. Peons that crack password files and log into boxes are not hackers. There is more to hacking than just doing - it is, really, a way of life. You will learn your own style, figure out your own "code" or ethics, and know what rason you do what you do. Reasons like knowledge, free information, and access are the typical hacker idea. All these 14 year old's running around with their trojans and nukers, and wants of destroying boxes and exacting revenge on an ex-friend are NOT hackers. No, they are nowhere nearhacking. They are annoying little shits that corrupt the philosophy and soil the name of hacking. These little "haxxors" will not be invisible, and will do many stupid things: o delete or corrupt data; o down the machine; o run bots or irc clients from root accounts or remote systems; o give the passwords he cracked to everyone in the world to prove that he can crack; =OR= o be found in the Darwin Awards, Vol. 4. I think sometimes this is done purposefully, just to draw attention to themselves so they can get caught and make the announcement "Hey! I'm a cyber-asshole, look at me and watch me cause unprovoked, unnecessary damage!" I hope that this text gives enogh insight to the world of hacking so that you can have a real heart in this matter, and and be a good clean hacker; not a fucking moron with a nuker. 1.2 -- Introduction to C++: Just to be montonous, and like every other C++ tutorial, I'm going to include an "Introduction to C++" for you to read. First, some basic history. The C++ programming language was developed in the early 1980's by Dr. Bjarne Stroustrup at AT&T's Bell Labs. Stroustrup developed C++ as an object oriented C - or, "C with classes", as it was originally called. The C programming language was developed at the same place, AT&T's Bell Labs, but 10 years earlier. Dr. Stroustrup wanted a flexible, object-oriented programming language that would be compatible with the existing C code; the result was C++. After C was standardised in 1989, a committe was formed to look into standardising C++. After of modifications and evolutions of C++ into what we know of it today, the standards committee (rep's from ANSI (american National Standards Institute) and ISO (International Standards Organisation)) approved a final draft of the C++ tandard in 1998. Finally. Why would anyone choose C++? Only answer I can think of is because they want to learn to program. Logical eh? But, there are benefits to using C++. One good thing I've noticed with C++ is that no matter your skill level, it is always working at the same level. As a novice (beginner, newb, w/e, same shit), you can use a small subset of C++ to write simple programs. As you learn more and advance along the way, more features become available to you to create more complex, useful programs. C++ supports the two most popular programming models, too: o procedural programming - a program model (paradigm) that uses sub-routines (or sub- programs) that allow replacement of a groupd of statements with a single statement, o object-oriented programming - a pardigm that is an evolution of procedural programming, where the procedures and the data they act on are placed into objects. But, be aware, C++ does require alot of reading (theory), whether it be from text books, tutorials, articles, white papers, eBooks, etc etc. You will see from this article that there is alot of reading, and most of it is necessary for further understanding. So if you're interested, it's best to read it all and not skip parts. 1.3 -- Important Notes to Read Before Going On: This entire article was written on a Windows XP Pro system (32-bit), using Bloodshed Dev-C++ Compiler. Everything I write will be written according to my Windows XP Pro system, using my Bloodshed Dev-C++ compiler. That means, if anything is off, such as data sizes (bytes), or things look different to your own source/compiler, that is not my fault, as this is writte accurate to me. ~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~ ~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~ 2.0--Some Light Reading to Get You Acquainted (simplified): 2.1 -- Writing C++ Source Code: C++ is a compiler-using programming language (such technical terms). There are different compilers out there for the different OS's, but I would recommend Bloodshed Dev-C++ over the others, just because it's the only one I like. You can find it here: http://www.bloodshed.net/devcpp.html The source code for C++ is human-readable, assuming that 1) you can read, 2) you are human, and 3) you understand C++ syntax. The basic outlay of a C++ source code looks something like this: [code] #include int main() { return 0; } [/code] This is what you'll see in Dev-C++ compiler. You will learn to use this as we move along in the article some more, and explore source code. 2.2 -- The Compiler: Compiling is the action you take of transforming your C++ source code into a working program execuatable file.To do this in Dev-C++, you simply go to Execute|Compile, and then view the results. Before you can run a program you're written, you have to build an executable file. When creating C++ programs--especially larges ones that require multiple header files and sources--it's important to understand what's going on when you compile. Generally, compiling is the process of converting text source codes into binary object files, or machine code. Obviously, a program called a compiler compiles the source code. Another program, called a linker, links together all the compiled object files needed by your program. The end result is an exectuable file, that is the final product. A C++ compiler makes several over your source code, converting plain-text source file into a binary object coe file, usually as .obj or .o extension. The resulting object code file contains the machine code. This object code is the main part of what's required for your source code to become the executable program. 2.3 -- The Linker: The linker takes any .obj code files compiled from your source code, and links them with special execution code and with any C++ library code required by your programs. The code in a library consists of several .obj modules grouped together logically and combined by a special librarian program into a single library file (with .lib extenstion). The end result is your executable file, with .exe extension. 2.4 -- The Basics of a C++ Source: This will be reinforced throughout the text: C++ is CasE SenSiTivE. This, THIS, this, are not the same according to C++. Be careful to follow some form of convention when creating new names in your programs. If you happen to change case halfway through from THIS to ThiS, there will be errors all over the place. All sources contain: o a preprocessor, o a main() function, o a main() code block. The uses of these will be explained along the way, so don't fret if you don't know what they are just yet. 2.5 -- C++ reserved words (commonly used): You won't be using all of these just yet, but this list is to more or less just get you acquainted and let you know what words you can and can't use due to restrictions or already inbuilt uses. [code] Keyword Category ------------------------------------------------------ break flow control statement case flow control statement class abstract data type const type modifier continue flow control statement delete memory management keyword do loop statement double data type else decision statement float data type for loop statement goto flow control statement if decision statement int data type long data type namespace access specifier new memory management keyword operator function behaviour private access specifier protected access specifier public access specifier struct abstract data tyoe switch flow control statement union abstract data type void data type while loop statement [/code] ~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~ ~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~ 3.0--The Basic Theories of C++: 3.0.1 -- Short Introduction: To make your programs actually do anything, you need to put some code into the code body of main() funtion. In order to write some code, you need to know more about the C++ syntax and commands. Writing source code is pretty much where C++ begins. As mentioned earlier, and in many texts already around the place, C++ uses object-oriented programming--OOP--, and was designed with OOP as its main goal. Because of this, alot of the basic programming concepts in C++ use the C++ objects - that is, variables or constants used in programming. 3.1 -- Input/Output Streams: Three standard C++ stream (stream - a linear, ordered sequence of bytes) objects are used for input, output, and error output. The standard input object (cin) reads from the keyboard; the standard output object (cout) and the standard error object (cerr) print to the screen. Writing data to the screen is done through the cout object and its insertion operator (<<). Reading the user input from the keyboard is done through the cin object and its extraction operator (>>). Example output code: cout << "This is output, it shows on your screen\n"; Example input code: cin >> blahblah; 3.1.1 -- Standard Output Stream - cout: The standard output stream objective, cout, uses its insertion operator ( << ) to redirect text output to the screen. By using a literal string of characters (a string of characters, or string) surrounded by quotation marks (" and "), you can weite some text to the screen. cout << "Type the message inbetween the quotation marks.\n"; This just simply outputs the text "Type the message inbetween the quotation marks." to the screen when the program is run. 3.1.2 -- Standard Input Stream - cin: Writing data to the screen is fun for a while, but we really need something more substantial for the program. This is where things start to get more interesting, and fun. The standard input objective stream, cin, is the easiest way to get input from a user. The cin object uses the extraction operator ( >> ) to extract information from a stream, and in this case, the standard input stream--which would also be the keyboard. cin >> name; is an example of the cin object in use. This simply means that you would now be prompted to input a string of characters for 'name'. Cin object is "smart enough" to know how to extract seperate words from the keyboard input by the whitespace used between words. cin >> firstName >> lastName; would allow you to enter a character string for 'firstName' and also 'lastName'. But that's getting more advanced than I plan to at this moment, you'll see more exmples and uses of cin later in example codes. 3.1.3 -- Standard Error Stream - cerr: The standard error object stream, cerr, uses the same syntax that the cout object uses. You can use the standard error stream to output messages to the screen when an error occurs in a program, while still sending the cout statements. 3.2 -- #Include Directive: The first thing you see in any source code is a little piece of information known as the preprocessor, and in whole looks a little like this: #include The preprocessor portion of the compiler looks for certain commands, called preprocessor directives, that preform special tasks during the compilation of the source code. One of these preprocessing directives is #include. The #include directive is a preprocessor instruction that tells the preprocessor "what follows in the angled brackets < > is a standard C++ Library filename. Find that file and read it right here". The commonly used iostream library, which you'll see in all the following source codes I use, is defined in the header file . To give your program access to the library, you must include this header file in your source code #include The angled brackets, < and >, around the header filename tell the preprocessor to look in all directories that should contain header files for the compiler. These header files are also known as h files - can you imagine why? 3.3 -- Escape Sequence Characters: These are special character combinations consisting of a back-slash ( \ ), and followed by a letteror combination of digits. These are used to provide literal representations of non-printing characters, characters with special meanings, and actions like carriage returns and tab movements. [code] Escape Sequence Description -------------------------------------------------------------------------- \a alert (bell, beep) \b backspace \n new line \r carriage return \t horizontal tab \v verticle tab \' single quotation mark \" double quotation mark \\ backslash \? question mark [/code] These are usually used inside the quotation marks of cout, such as cout << "This will make a new line. \n"; cout << "This will make TWO new lines.\n\n"; The \n, according to the list above, will make a new line after the output to the screen. If you do not use this, then any other output following will be on the same line - really messy. ~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~ ~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~ 4.0--The Infamous "Hello World": 4.0.1 -- Short Introduction: This will show you the source code to the most basic, common, beginner's program of all programming languages--"Hello World!"--followed by an explanation, line-by-line. 4.1 -- Example Source Code: [code] 0: #include 1: using namespace std; 2: 3: int main() 4: { 5: cout << "Hello World! \n"; 6: return 0; 7: } [/code] 4.2 -- Explanation of the Source: Line 0: - calls iostream header file; - # (hash, pound) symbol signals to the preprocessor; - include is a preprocessor instruction that says "What follows is a filename. Find that file and read it right here". The angle brackets < > around the filename tell the preprocessor to "Look in all the usual places for this file" - in this case, in the directory that holds all the header files for the compiler. Line 1: - basically allows you to replace every instance of std::cout with just cout - as can be seen in the code. Something else to note, as said by Slarty on B-H.org forums, is: [quote="Slarty"] Ok, let me explain what's going on with the "using namespace std;" NEW c++ compilers, which are respecting the standard, will provide a header called simply iostream. These newer libraries put all their objects, classes, etc into namespace "std". This avoids namespace pollution. Older c++ compilers, provide a header "iostream.h" (note the .h, it *is* important). In this header file, they declare things like cin, cout, in the global namespace, hence you don't need "using namespace std" NEWER c++ compilers may also provide iostream.h (which g++ current does), but if you include it, you get a warning telling you that this is a deprecated (old-fashioned) way of doing things. Therefore the correct, modern way, is to include iostream and use namespace std. The old, but correct way, is to include iostream.h and not use namespace std. An incorrect way is to include iostream.h and then use namespace std ("using namespace std; does no harm in this case, but is useless). You may find that if you explicitly refer to cout as std::cout, it won't work if you only include iostream.h. A *really* incorrect way is to include then try to access cout from the global namespace, this probably won't work at all. Slarty [/quote] Line 3: - is the beginning of the actual program. Every C++ program has a main() function. Generally, a function is a block of code that performs one or more actions - in this case, printing the text Hello World! to the screen. - Like all functions, main() must state what value it will return; however, main() is special - it will always return int. Go main()! Line 4: - all functions begin with an opening and closing brace { }, with the main code block inbetween the two. Everything between them is considered part of the function. In this case, the code is all part of the main() function. Line 5: - is the meat and bones of the program. The object cout is used to print text to the screen Hello World! The object cout outputs to the screen, while it's partner, cin, inputs from the keyboard (but not shown in this program). - the ( << ) are standardredirection operators - they show the direction of the input (>>) or output (<<). Whatever follows the output redirection operators in inverted commas is written to the screen. In this case, it's the words Hello World =]. - the last two characters, \n, represent a new line Line 6: - we "return" the value 0 to the operating system. On some systems, this is used to show success or failure; usually 0 is for success. Line 7: - end the function main() with a closing brace, to signify the end of the main() code block. ~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~ ~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~ 5.0--C and C++ Style Comments (in-source): 5.0.1 -- Short Introduction: When writing a program, it's very important to document what the source code does. The best place to do this, obviously, is in the source code itself. This makes it easy for a user to take a quick glance and understand what each step of the program does. At first, the simple programs you make won't require many, if any at all, comments. But, as they grow in complexity, so should the use of comments proportionally. Even if you understand the code today, high chances are that in six months time you'll have forgotten. After all, it's best to assume that the user(s) can read the C++ source code, but can't read your mind - let the source code tell what is going on, and the comments why you decided to do it. C++ supports two styles of comments. The first style was inherited from the C programming language, which is still fully contained as a subset in C++. The second style was introduced specifically for C++. 5.1 -- Comment Styles: 5.1.1 -- C Style Comments: C Style comments uses a matched pair of slashes and stars, with the slashes on the outside and stars on the inside. Always. It looks something like this /* OMG Jo0 Haxx()r w1th Comm3nTs */ This is a very useful comment because it can span many lines of code. The C-Style comments work like bbCode tags, or HTML tags - you mst always close it for it to be finished. That is why it can span several lines, or until you close it with a */. The above code can also look like this /* OMG Jo0 Pr0gr4mm3R w1th ComM3nTs */ See - must always close to finish the comments. Anything inbetween the /* and */ will be counted as a comment, uncluding entire functions and code blocks. 5.1.2 -- C++ Style Comments: Unlike C Style, C++ Style comments can only exist on single line in a source file. It uses a double-slash to mark the beginning of the comment, and does not require a closing slash. Yay. // Hrm, this C++ Style comment looks really easy? And that's all there is to it. The compiler completely ignores all these lines. Comments not only tell the read of the code what's going on, but also allow for important notes to be entered, such as who wrote the code, when, why, etc. // // This still does the same job as above. Fun eh? // 5.3 -- Example Source Code: [code] 0: #include 1:usingname space std; 2: int main() 3: { 4: /* This is a C-Style comment, 5: and it extends all the way until you 6: close it, similar to BBCode tags and HTML */ 7: cout << "This shows C/C++ commentary styles\n"; 8: // This is C++-Style comment, and it ends at the end of the line 9: // with no "closing tags" required. 10: cout << "Ta-da, end of the commentaries\n"; 11: // This is also called "double-slash" 12: /* While this is called "star-slash" */ 13: return 0; 14: } [/code] 5.4 -- Explanation Of: The source code above simply uses the cout object to output This shows C/C++ commentary styles Ta-da, end of commentaries to the screen. Notice how nothing else shows up? That's why comments are good - they're invisible until you want them. Everything else in the source code should be self-explanatory, and if not, referrable from other sections already discussed. ~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~ ~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~ 6a.0 -- Fundamental C++ Data Types, Including Variables and Constants (simplified, skimmed-over-but-not-too-basic version): 6a.1 -- Definitions: o variables: In C++, a variable is a location in your computer's memory in which you can store a value and from which you can retreive that value. o constants: Like variables, constants are data storgage locations. Variables can vary; constants cannot (just like in Algebra/Pre-calc). Other forms of constants used are Literal Constants, Enumerated Constants, and Symbolic Constants. 6a.2 -- Tables of Variable Information, and Explanation: Several variable types are built into C++. [code] Type Size Values ---------------------------------------------------------------------------------------------------------------------------- - unsigned short int 2 bytes 0 [to] 65,535 short int 2 bytes -32,768 [to] 32,767 unsigned long int 4 bytes 0 [to] 4,294,967,295 long int 4 bytes -2,147,483,648 [to] 2,147,483,647 int 4 bytes -2,147,483,648 [to] 2,147,483,647 unsigned int 4 bytes 0 [to] 4,294,967,295 char 1 byte 256 character values bool 1 byte true [or] false float 1 byte 1.2e-38 [to] 3.4e38 double 1 byte 2.2e-308 [to] 1.8e308 [/code] Notes: signed/unsigned - sometimes you need negative numbers, sometimes you don't. Integers (short and long) without the word "unsigned" are assumed (automatically) to be "signed" - hence, no need to write "signed" before the variable. "signed" integers are either negative or positive values, as can be seen above; "unsigned" are always positive values. ie: 'long int' is "signed", and may be positive or negative values; 'unsigned long int' is "unsigned", and may be positive values only. char - these are used to hold a single byte and are used for holding the 256 characters and symbols of the ASCII and extended-ASCII character sets. ie: char[25] would hold up to 25 characters, ie: abcdefghijklmnopqrstuvwxy, but no z. ASCII - stands for American Standard Code for Information Interchange. It is a system of encodig the characters, numerals and punctuation used by computers. Floating-point - these variables have values that can be expressed in fractions, as in, real numbers. 6a.3 -- Example Source Code: Determining the size of variables on your computer: [code] 0: #include 1: using namespace std; 2: 3: int main() 4: { 5: cout << "The size of an 'int' is:\t\t"; 6: cout << sizeof(int) << " bytes.\n"; 7: cout << "The size of a 'short int' is:\t"; 8: cout << sizeof(short) << " bytes.\n"; 9: cout << "The size of a 'long int' is:\t"; 10: cout << sizeof(long) << " bytes.\n"; 11: cout << "The size of a 'char' is:\t\t"; 12: cout << sizeof(char) << " bytes.\n"; 13: cout << "The size of a 'float' is:\t\t"; 14: cout << sizeof(float) << " bytes.\n"; 15: cout << "The size of an 'double' is:\t"; 16: cout << sizeof(double) << " bytes.\n"; 17: 18: return 0; 19: } [/code] The output of the code, when run, will be the sizes of each integer variable on your system. The bytes shown may be different from the table above. ~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~ ~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~ 6.0b--Fundamental C++ Data Types, Including Variables and Constants (confusing, in-depth version): 6b.0.1 -- (Short Introduction) What Are Data Types?: At it's most basic level, a computer program performs an operation on some data and thus produces a result (cause and effect :)). Because C++ is a typed language, each bit of data--that is, each data element--must have a specific data type. To speak technically, a data type describes the set of values and the operations that can be applied to that set. To speak in layman's, a data type determines how the data is represented in the computer and the kinds of data processing that can be performed on it. Make any more sense that time? There are two types of data types in the C++ language: 1) Fundamental data types 2) Derived data types Because variables and data types are quite interrelated, you really need to understand one concept to understand the other - chicken and egg situation. For now though, just know that variables hold values, and are declared with this syntax [DataType] [VariableName]; This statement tells the compiler that the variable, VariableName, has the data type DataType. You can also assign a value to initialise a variable when you declare it, something like this [DataType] [VariableName] = [SomeValue]; And on with the show we go, yay! 6b.1 -- Explanation of Fundamental Data Types: The fundamental data types used in C++ fall into the following groups, or categories: o Integer types o Character types o Bollean types o Floating-point types o Double-precision types Each of these data types above has it's own characteristics, and each one uses a predetermined (determined before hand) amount of memory. However, do be aware that different systems (16-/32-/64-bit) use different numbers of bytes (as pointed out by Slarty - thanks). 6b.1.1 -- The Integer Type: Integers include positive and negative whole numbers (no decimals), and the number 0 (zero). Integers have no fractional parts at all. As you can probably tell already, this is a basic mathematical concept. +134 -48 -349 +4838 0 31847 These are all integer types. However, if an integer has no sign in front of it (ie: a positive or negative mathematical sign), it is always assumed to be positive, just like in mathematics. Notice that the last integer, 31847, does not use a comma to properly format the numbers. This is because commas aren't allowed in integers because they simply are not a number. C++ makes things a little more interesting, and at times perplexing, by defining many types of integers. Specifically, there are signed integers (both positive and negative numbers), and unsigned integers (only positive numbers). The following two tables are of the different integer data types and their respective byte sizes (grabbed this from a photocopy at the library): Integral Data Types and Common Byte Sizes: [code] Integer Byte Size ----------------------------------------- char 1 bool 1 short 2 int 2 or 4 long 4 or 8 [/code] C++ integers are further more made up of several different categories based on the number of bytes each type uses. The following table (again, yoinked from a photocopy at the library) gives the range of values permissable for each integer of a particular byte size (mentioned above ^^): [code] Byte Size Sign Value Range --------------------------------------------------------------------------------------------------------------------- 1 signed -128 [to] 127 1 unsigned 0 [to] 255 2 signed -32,768 [to] 32,767 2 unsigned 0 [to] 65,535 4 signed -2,147,483,648 [to] 2,147,483,647 4 unsigned 0 [to] 4,294,967,295 [/code] Just because this table is here (and available in books and articles online), don't assume that the byte size if always correct - as mentioned earlier, different systems have different sizes. Make use of the sizeof operator to let the compiler get accurate data type byte sizes for you. This operator, sizeof, returns the number of bytes used by a data type. An example of the usage of sizeof in source code: [code] 0: #include 1: using namespace std; 2: 3: int main() 4: { 5: cout << "The size of an 'int' is:\t\t"; 6: cout << sizeof(int) << " bytes.\n"; 7: cout << "The size of a 'short int' is:\t"; 8: cout << sizeof(short) << " bytes.\n"; 9: cout << "The size of a 'long int' is:\t"; 10: cout << sizeof(long) << " bytes.\n"; 11: cout << "The size of a 'char' is:\t\t"; 12: cout << sizeof(char) << " bytes.\n"; 13: cout << "The size of a 'float' is:\t\t"; 14: cout << sizeof(float) << " bytes.\n"; 15: cout << "The size of an 'double' is:\t"; 16: cout << sizeof(double) << " bytes.\n"; 17: 18: return 0; 19: } [/code] 6b.1.2 -- The Character Type: Characters are user everyday, and are therefore quite important. This entire article is made up of strings of characters (mostly).The data type I'll discuss now, char, is used to represent one single chracter of data. A char can use single quotes to specify a value: char ch = 'A'; A char is an integral daata type (integer). This means that you can also define the same character value 'A' using ASCII code as a value. For a full table of these, look up "ASCII table" on http://www.google.com, because I have no internet at the moment and won't feel like looking it up later. Anyway, A is 65 in ASCII code, so you could also do the above as: char ch = '65'; I am contemplating writing in a section on char data types that includes an in-depth look at arrays and the different number notations--binary, decimal, hexadecimal, octal--but I'll leave that for anyone to request that in an update. Moving along then, to Boolean :) 6b.1.3 -- The Boolean Type: [Thanks go to Rich0 for helping with this part.] A relative newcomer to the built-in data types provided by C++ is the boolean data type, secified by the bool keyword. A bool can store only two possible values - True and False. We use Boolean variable to test conditions in a program to help the computer make decisions. Booleanrs are quite useful for keeping track of the various states a code block might be in at any given time during program execution. For an example, a Boolean variable could be declared like this: bool LamersPleaseDie = true; Elsewhere in the program code, this variable could be used to see whether the programshouldsave some files.By checking the value of LamersPleaseDie to see whether it's true or false, the program will know what to do (hopefully killing all lamers with a sudden EMP through their computers, blowing up their monitors sending glass shards into their faces). 6b.1.4 -- Floating-Point and Double-Precision Type: Floating-point numbers consist of an integer part and a fractional part, with a decimal point in between. Floating-point numbers are important for a large amount of computer apps, from checkbook balancings to rocket science (fun?). Double-precision numbers are floating-point numbers with a higher degree of decimal accuracy. There are three different types of floating-point, and you can use each depending on the level of decimal place precision you want/need: - float - double - long double The Typcial Byte Size of Each: [code] Data Type Byte Size ----------------------------------------- float 4 couble 8 double long 12 [or] 16 [/code] NOTE: When you use floating-point in a program, you must always provide at least one digit to the right of the decimal point, even if that digit is a 0. Also, if the data type is float, you should specify this by appending an f to the end of that number. For example, if the number is 42, and you want it to be a float, it would be written as 42.0f, but as a double, the number is just 42.0. NOTE: For a long double, you specify the number as being long similar as when specifying it as a float. Instead of 42.0 or 42.0f, you would do 42.0L, for long. For example, long double SkiddiesThatAreCaught = 42.0L Floating-point types are most commonly used for mathematical related problems or operations, and to store the results of such things. For example, float Lamers = 12.0f, Skiddies = 2.3f; float Lamers * Skiddies; This basically multiplies Lamers and Skiddies. The end result is 27.6 (12 * 2.3), but the 0.6 in 27.6 can be stored because it was a floating-point type. Normally, C++ will disregard all fracionts and/or decimals. Remember that, otherwise it will be a pain in the arse later. 6b.2 -- Derived Data Types: A derived data type is simply any data type constructed (or derived) from a fundamental data type. Pretty simple explanation :). Some common derived data types are o arrays o pointers o references o structures o unions o classes Although these sound alot like a school or work environment, they're not. However, because these types are so flexible and can be compiled in many ways, there are really an infinite number of posssibile derived data types. Wowies. I won't go into the above mentioned data types in much detail, but a simple skim will suffice for now. Might be in-depth later. Who knows? 6b.2.1 -- Arrays: An array is simply a collection of similar object, such as a a grouping of like data types that rseide in a contiguous block of bytes in memory. You use an index number to access each element of the array, similar to the way you find a certain post office box by the number in a post office. Make sense? Arrays can be built in different "shapes". There are three general shapes of arrays, and each one corresponds to the structure of an individual array: o One-dimensional arrays o Two-dimensional arrays o Multi-dimensional arrays Each element in a character array takes up one byte of storage space for one character. Other types of arrays can be made up of floats, doubles, or some other fundamental data type, so each array element would take up a much larger amount of byte storage space. 6b.2.2 -- Pointers and References: A pointer is a variable that points to a specific location in memory. A pointer gives you the actual memory address of a specific object. A reference is an alias for an object. References are similar to pointers, but have stricter usage rules and less usability (functionality?). 6b.2.3 -- Strucutres, Unions, and Classes: A structure is a data typethat groups other data types together into a single, compound data type. Structures make it easy to keep variables in logical check with source code by making each logically related variable a member of a structure. A union is similar to a structure, however, unlike a structure, only one of the members of a union can be used at any one point in time. Unions are supposed to give a powerful and compact way to present data, but I'm neutral. A class is an enhanced structure that gives the OOP features of C++. Quite long and extensive explanations required - I might do these another time. For now, you can know about them, but you won't need them. 6b.3 -- Variables (...Introduction to): Variables are, in the simplest terms possible, aliases for memory locations; that is, they are named storage locations in the memory. Each specific data type stores information using a specific number of bytes in memory. For example, on a 32-bit OS running Windows XP Pro (such as mine), the size of an integer is four (4) bytes. The computer needs only a starting address for a variable, from there it can read as many bytes from memory as required for a given data type. As long as it starts at this location. A computer program operates on data, which is stored in memory. During the execution of the program (most commonly, double-clicking the .exe file), the same memory location may contain different values at different times. This is why this type of memory is called variable; because it is a variable. These are a few variables that might be used for test results in mathematics: char name; // 1 byte short score; // 2 bytes float average; // 4 bytes Using variables makes programming much easier than if you had to use memory addresses directly. One of the fundamental aspects of most modern programming languages is the ability to create variables for use within your programs (also hiding the ugliness of raw memory addresses from public - yay!). But of course, C++ gives you the ability to declare and define as many type-specific variables as you need. And on to the next section to discuss these more... ~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~ ~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~ 7.0--Declaring and Defining Variables and Constants: I'll try to keep this section simple, otherwise it could get way too deep and no one would understand it. I *might* write an in-depth version another time about the many different constants and variables. Enjoy this for now. 7.0.1 -- Short Introduction: Before you can use a variable, functions, class, or any other piece of programming data (object), you must always declare and define it. o declaration - introduces a name of some sort into a program, letting the compiler know that a function or some data type exists. A declaration also lays out the exact format by specifying storage classes, data types, and linkages for an object or function. o defintion - this actually allocates (reserves) new storage, creating a function or an identifier for use within the program. Declarations can also be deifnitions. Confused yet? You can define, or "create", a variable by stating it's type,followed by one or more space (white-space), followed by the variable name and a semicolon [;]. The variable name can be any combination of letters, but cannot contain spaces. Good variable names tell you what the variables are for; using good names make it easier to understand and follow the program. ie: myAge, myHeight - these would be good variable names to define your age/height. NOTE: C++ is case sensitive, just like UNIX. THIS =/= this =/= tHiS You can create more than one variable at a time, too. For example: unsigned int myAge, myHeight; will make the variables "myAge" and "myHeight" both unsigned integer variables. 7.1 --Assigning and Initialising: Assigning values to the variables: Using the assignment operator [=], you can assign a vaule to your variable. This can be done two ways - 1) assignment, and 2) initialisation. 1) [code] unsigned short Width; Width = 5; [/code] or 2) [code] unsigned short Width = 5; [/code] Initialisation looks like assignment, and with integer variables, the difference is very minute. However, as C++ progresses, you will find that some values must be initialised because they cannot be assigned a value. 7.1.1 -- Example Source Code: [code] 0: #include 1: using namespace std; 2: 3: int main() 4: { 5: unsigned short int Width = 5, Length; //Notice we use two of the assigning methods: initialisation of Width 5: Length = 10; //and assignment of Length 6: 7: unsigned short int Area = Width * Length; // Initialisation of Area 8: // Outputting the variables to screen... 9: cout << "Width: " << Width << "\n"; 10: cout << "Length: " << Length << endl; 11: cout << "Therefore, the area of Width (multiplied by) Length = " << Area << endl; 12: return 0; 13: } [/code] Output to screen: Width: 5 Length: 10 Therefore, the area of Width (multiplied by) Length = 50 7.1.2 -- Explanation of: Line 5: is where we define the variable Width as an "unsigned short integer", and it is initialised to 5. The other variable, Length, is also defined on Line 5: as an "unsigned short integer", but not given a value _yet_. NOTE: Line5: contains multiple actions - defining of variables, Length and Width as "unsigned short integers", and the and the initialisation of the variable Width to 5. Line 6: the "unsigned short integer" Length from line 5: is now assigned a value of 10. Line 10: the integer, Area, is defined. It is also initialised to the value obtained by multiplying Width and Length. Line 12-14: Output to the screen, NOTE: "<< endl;" means ENDLine, and is said end-ell. It is another way of doing \n, nothing major. 7.2 -- typedef: Typing out "unsigned short int" every time you need to assign a value to a variable can be tedious, repititious, and error-prone. You can, however, create a synonym for an existing type by using the C++ keyword "typedef", which stands for 'type definition'. typedef is used by writing the keyword "typedef", followed by the existing type and the new name. Example: typedef unsigned short int UNSHINT This creates the name UNSHINT (UNsigned SHort INTeger - aren't I clever :)), which can be used anywhere you would normally type out "unsigned short int". 7.2.1 -- Example Source Code: [code] 0: #include 1: using namespace std; 2: typedef unsigned short int UNSHINT; //typedef defined in the same source as above 3: int main() 4: { 5: UNSHINT Width = 5; 6: UNSHINT Length; 7: Length = 10; 8: UNSHINT Area = Width * Length; // Initialisation of Area 9: // Outputting the variables to screen... 10: cout << "Width: " << Width << "\n"; 11: cout << "Length: " << Length << endl; 12: cout << "Therefore, the area of Width (multiplied by) Length = " << Area << endl; 13: return 0; 14: } [/code] Output to screen is the same as before. Follow so far? 7.3 -- Constant Types (And Using): As mentioned earlier, constants are like variables and store data in locations. While variables can vary, constants cannot, much like algebra (yay). You must initialise a constant when you create it, and thereafter you cannot assign it a new value; it is called a constant for a reason. Literal Constants - this is a value typed directly into your program wherever it is needed. For example: int myAge = 39; myAge is a variable, of type integer. 39 is a literal constant - you can't assign a value to 39, and it's value cannot be changed. Symbolic Constants - this is a constant represented by a name, just as a variable is. Unlike a variable, however, after a constant is initialised it's value cannot be changed. For example: students = classes * 35 In the example, 20 is the literal constant. If you substitute a symbolic constant for this, it will make reading and mainting the code easier. For example: students = classes * studentsPerClass This is "healthier" to use because if at any time you need to change the numer of students in a class, you can simply redefine the constant studentsPerClass, instead of going through the code and changing it manually. Enumerated Constants - this creates a set of constants. You can declare anything to be an enumeration. Every enumerated constant has an integer value, and if you don't specify otherwise in the code, thge first constant will always be the value 0, and the rest will count up from there. For example: [code] enum COLOUR { RED, BLUE, GREEN, WHITE, BLACK }; [/code] This statement makes COLOUR the name of an enumeration, als known as, a new type. In this case, RED = 0, BLUE = 1, GREEN = 2, and so on. But, any constant can be initialised with a value, but those that aren't initialised, will go back to counting up from the initialised. For example: [code] enum COLOUR { RED=100, BLUE, GREEN=500, WHITE=800, BLACk }; [/code] This statement gives RED = 100, BLUE = 101; GREEN = 500; WHITE = 800, BLACK = 801. 7.3.1 -- Defining Constants: The following are the easiest ways to define costants: #define - also known as "the evil way" [code] #define studentsPerClass 15 [/code] Note: sudentsPerClass contain no particular type - int, char, etc. #define simply does a text substitution, so the preprocessor just substitutes the words studentsPerClass for 15. This is why it is also known as "the evil way". const - also known as "the good way" [code] const unsigned short int studentsPerClass = 15; [/code] This is "the good way" because it gives a definite type to the constant - "unsigned short int". This may take longer to type out, but because it has a type and the compiler can enforce this, it makes it all worth it. Unless you're lazy. Like me. Sometimes. */me is a bad_panda =(* ~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~ ~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~ 8.0--References, and Such Shit: I added this last minute, figured it would be respectful, mature, etc. Also might help others with further information. So, read ad explore. o "Beginning C++", from my college library, tattered and torn that one is, and o Some book Enjoi recommended me, but, I don't even know where that is anymore so I can't tell you the name - ask him yourself. o Governmentsecurity.org o Brain-hack.org o HackersCenter.com o LameIndustries.org o http://www.astalavista.com//data/c_torn_apart_1.html o CProgramming.com o CPlusPlus.com o WarIndustries.com o ValhallaLegends.com o Anomalous-Security.org/phpBB2 and my favourite source of whitebooks o 194.85.35.67/BOOKS/ (it's an FTP, for all you numbutz that can't figure it out) ~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~ ~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~ The two most famous words in movies: The End. Hope this helps. Email any questions, comments, etc to the contacts at the start. ~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~ ~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~ If you need a download version, you can go here: [url="http://www.tgs-security.com/tutorials.html]C++Begin[/url]. I know the posted format is bad, that's because going from WordPad to forum post destroys the layout of Wordwrap. Any comments, complaints, and/or corrections would be greatly appreciated, as this has been what's keeping me busy aswell as school.