C
From DocDroppers
Contents |
The C Programming Language
Any hacker worth his salt knows C, so you should learn it as soon as you can. It will give you the valuable skills required to understand other hackers code, and will allow you to help the Open Source Software Community if you are a good enough hacker to choose that path. What follows is Feverdream's Always Being Updated Updated C Tutorial. After reading this you should be ready to learn the new step, C++. It is important for you to understand C before C++ because the standards that define C++ require that everything be backwards compatible with C. So by learning C, you are learning a subset of C++.
Introduction
- Tutorial Preamble
The goal of this tutorial is to break down the basic concepts and give you a solid foundation you can use to learn the rest of the C and C++ languages. Learning C first is good because it teaches you to otected spr deeper; C++ is good because it teaches you to look wider. A fundamental understanding the core C language is required for both to be learned correctly. Once you understand C you will be in a much better position to understand C++.
Tutorial Credits
This tutorial was originally started by Feverdream of the binrev.com forums, but may be added or added to by anybody who has a user account on DocDroppers and is willing to follow the rules spelled out below. Here is a full list of all the great people who have helped out:
- Feverdream
- greeniguana00
Rules for Editing
The rules for adding to or otherwise editing this tutorial, and why it help you to keep to them are as follows.
- If you edit or add to this tutorial, please reference your work in the Bibliography section below so that others can confirm this tutorial at all times as valid.
- If you edit or add to this tutorial, please add your name/handle to the credits list only once so the list does not cluttered. If multiple people use the same handle, use an email address to make it known you are two people (Or just pick a better handle.).
- Please respect an overall neutral point of view in your edits so that an un-hackerish bias is not translated to the person reading the tutorial. This tutorial is here to educate, not belittle.
- Do not remove valid information or the names of people who helped unless they are vandals, since you would not want the same done to you.
- Vandals forfeit an they props/credit/etc for there work on this document, because vandalizing tutorials is for fucktards.
- Try to make sure the pages sync well with each other. The int page is a great example of a clearly defined page.
Tutorial Question/Rants
If you have a question about this tutorial and you want your thoughts to be known so that we can use your feedback to help make this tutorial more understandable or just plain better, use the C Tutorial Soapbox to leave your questions,being careful to follow the rules at the top of that page while you do so.
A Simple Tutorial in Binary.
In binary the only possible values a digit can have is 1 or 0. That is right, One or Zero. But that does not mean you can not count to a higher number, it just means that you have to put more binary digits (The 1's and 0's) together.
For example:
- In binary '0000-1011' is equal to one 'byte' (see below) with a value of '11' in decimal.
- In binary '0111-1111' is equal to one 'byte' (see below) with a value of '127' in decimal.
Some terms you will need to learn:
- A single binary digit is called a 'bit'.
- A set of 4 bits is a nyble. (although you probably don't need to learn this one)
- A set of 8 bits is a 'byte'.
We calculate the number by doing base-2 math. For each one from the right, we give the bit the value of (I*2) where I is equal to the index of the bit from the right going to he left.
So if the byte is 0010-0010, we map it as follows..
128,64,32,16,8,4,2,1 | See the pattern? Every value is double the one to its right. 0 , 0, 1, 0,0,0,1,0 |
.. And simply add up all of the numbers that a '1' is under. The example only shows a single byte, but multiple bytes can be (and are often) used.
Test: Find the binary values of any 10 values from 1 through 255.
Elite Hacker Note: At the lowest level of hardware, a binary "1" means "on" or "high" and a binary "0" means "off" or "low".
The Core C Language
What follows is the a tutorial on the main core C language. It does not include API calls, common system functions, or little magic elves. The idea is once you have this foundation, you build on your knowledge however you so choose.
Variables
What do Variables Do?
Variables store "variable data", meaning that they store data that may or may not change while your code is being run. Think of them as magic P.O. boxes that you can put data into.
What are the different Variable Types?
Each "Type" of variable is stored inside your computers memory, but each has its own way of storing its data. Think of them each as a sign saying what to expect in the P.O. box (postcard? letter? package?)
The base Variable Types are as follows:
You assign a "type" to variables so the program knows what it can do with them.
All about 'signed' verses 'unsigned'
Any type of char, int or short can be 'signed' or 'unsigned'.
- A 'signed' variable can hold negative and positive values.
- A 'unsigned' variable can only hold positive values.
This means to declare an int that is unsigned (and can only store positive integers) in your code you do the following:
unsigned int myUInt;
This means to declare an int that is signed (and can store both positive and negative integers) in your code you do the following:
signed int mySInt;
And you can only use one of these modifiers per variable declaration, so any of the following 2 lines would not make sense and be a compiler error:
signed unsigned int myUInt; unsigned signed int myUInt;
Test: A variable can hold both negative and positive integers. Is it unsigned or is it signed?
Test: A variable can only hold positive integers. Is it unsigned or is it signed?
The object Variable Types
- Structure Types with 'struct'
- Union Types with 'union'
- Enumerations with 'enum'
- NULL
- Casting Types
- Arrays
- Pointers
- A word of warning.
- Simple rules to keep things simple.
- Pointers of normal data types.
- Pointers to functions. - http://www.newty.de/fpt/index.html
- Math in C.
- Incrementation and De-Incrementation
- Of non-pointer types
- Of pointer types.
- Pre verses Post
- Conditionals
- Defining True and False in C
- if
- if..else
- if..else if.. else
- switch
- Loops
- for
- while
- do..while
- Functions
- Why functions are good.
- How to define a function.
- How to use functions.
- Passing function input by value.
- Passing function input by reference.
- Recursion with Functions.
The C Preprocessor.
- The define directive.
- Using the 'define' directive to create globally defined constants.
- Using the 'define' directive to create Macro'c
UNIX Network programming
- Beej's Guide to Internet Socket and Network Programming - http://www.beej.us/guide/bgnet/
- Beej's Guide to Unix Interprocess Communication - http://beej.us/guide/bgipc/
Shared Library Creation
- How to create and use program libraries on Linux. - http://www.faqs.org/docs/Linux-HOWTO/Program-Library-HOWTO.html
Assorted Appendixes
- Appendix A: The different C keywords, the availability of each, and what they really mean to you.
- auto
- char
- int
- short
- long
- float
- double
- void
- sizeof
- typedef
- enum
- signed
- unsigned
- struct
- union
- switch
- case
- break
- default
- for
- do
- while
- continue
- const
- static
- extern
- register
- volatile
- if
- else
- goto
- return
- inline - C99 and Up Only!
- restrict - C99 and Up Only!
- _Bool - C99 and Up Only!
- _Complex - C99 and Up Only!
- _Imaginary - C99 and Up Only
- Appendix B: Security Considerations.
- For Linux and UNIX - http://www.dwheeler.com/secure-programs/Secure-Programs-HOWTO.html
- Appendix C: Common functions you should know.
- Appendix D: Common functions you should stay away from, and why.
- Appendix D: Common GCC error messages and what they mean. ===
- Add your own here!
- Appendix E: Bibliography
- The C Programming Language (Ansi C) By Brian W. Kernighan and Dennis M. Ritchie. ISBN: 0-13-110362-8 {PBK}, ISBN: 0-13-110370-9
- Appendix F: Credits
- Feverdream of the binrev.com forums
- Do You Have the skill to help? Could this be you?[[Category: Coding)
