---------------------------------------------- "Introduction To C: Chapter 1: Program Structure & Techniques" ---------------------------------------------- C/O :: Dr4g of DynamicHell Development Team ---------------------------------------------- http://dynamichell.org | irc.dynamichell.org ---------------------------------------------- The History of C ---------------- The C programming language was created in the early 1970's. It was a system implementation language, for the UNIX operating system. C was derived from the language B, and before that, we had the BCPL language. C is one of the most dominent languages today, due to many reasons, one being its flexibility. When UNIX and C were in fashion, appeared "The C Programming Language", created by Kernighan and Ritchie (K&R), often called the "white book". In the middle of the 1980's C was oficially standardized by the ANSI commitee. --------------------------- The Contents of a C Program --------------------------- Header Files ------------- A C program starts off with declaring header files (libraries), which will be used throughout your program. Every C header file has the standard file extension ".h". The most necessary header file would have to be "stdio.h Meaning the standard input output library. How Do We Include These Files? ------------------------------- The syntax: #include For extra functions we include more libraries as we go along. Variable Decleration --------------------- A variable is a storage location in memory which hold some kind of data. All variables are referred to by an addresses. However, instead of referring to an address, every time we want to access a variable. We give that address a name so its easy to remember, these are called variables. Every variable holds a certain type of data for example: Charaters, or Numeric values. This is called the data type of the variable. And it describes which type of data it stores. Integers are whole numbers like 0,1,2,3. (abbreviation: int) Floating Point values are like 1.4, 100.6..etc (abbreviation: float) Chars are, well charaters, mianly from the ASCII chart. (abbreviation: char) How do we create our variables? ------------------------------- The Syntax ----------- int myNum; or int myNum=10; float myFloat; or float myFloat=5.5; char myChar; or myChar="a"; Note:: C is a case-sensitive language, so MYFLOAT is different from myFloat. Notice how we use a semi-colon ";" after every line. This tells the computer that its "end of command", it lets the compiler know when the statement has ended. Overview: So far we have. ------------------------- #include int myNumber; A function ---------- A function is a group of variables and statements together, that perform a task. We reffer to this function by its name, just like a variable each function has its own data type too. The Main() Function. -------------------- The "main" function is the starting point and ending point of your program. Your program starts executing at the start of main() and ends at the end of main(). Everything is done inside main(). You can design your own functions as discussed above, and call them within main to execute them. As all functions have data types too, 'int' is a very common type to use for the main() function. eg: "int main()". You could use void too like so, however its not recommended. eg: "void main(void)". The compiler may let you, but main always returns 'int' At the end of every function, we have a 'return' value. It means after all code in this function has been executed, we have to return some kind of value. For this context in main. The following is most suited: "return 0;" Returning 0 means execution was successful without errors. The Void Data Type. ------------------- If we declare main() as type void as discussed above. We need not have a return value. All void functions have no return value. Braces ------- Braces when relating to functions, are there to mark the start and ending points of your function (its lifetime so to speak). We use braces { and } to mark the start and end. Overview: So far we have. ------------------------- #include int myNumber; int main() { // Program code goes in here. return 0; } The above program doesnt do much, but it does compile and execure fully with 0 errors. Comments --------- Did you notice the use of the "//" in the program above. This is called a "comment" and everything else after that, one the same line, Is disregarded by the compiler. We use this to put in comments, for us or other people to understand the code more easily. // is a single line comment. We can have multi line comments like the following. /* this comment spans over 2 lines long */ "/*" signifying the start and "*/" signifying the end of the comments Note: // is not C ANSI standard. /* This is C */ // This is C++ However your C compiler will still let you use them. How do i run this program? -------------------------- You will need a form of a compiler. Linux ----- The most famous compiler for Linux is 'gcc'. Your source file ".c" will be compiled with the following command. "gcc program.c -o program". This has created an executable with the name "program". And we can execute this executable file using the following. "./program". Windows ------- Windows has the great advantage of an IDE (Integrated development environment). A common IDE for windows is "Dev C++" Which runs C and C++ programs. Microsoft VC++ Also provides the same task, but has much more functionallity. Dev C++ is free, MSVC++ is not. So i suggest you google "Dev C++" and start downloading. Code Indentation ----------------- Indenting our code using the "Tab" key. Notice in our previous C program, the 'return 0;' statement was tabbed in? This is a coding technique used by all experienced programmers. It makes the code much more readable and in a sctructured manner. Naming Conventions ------------------ Naming conventions is a style of coding, for giving descriptive names to your variables. For example, by ussing the following, we can see which data type our variable is, just by looking at its name. eg: int myAge --> int intMyAge Clearly we can see here that this variable is an Integer data type and holds the persons age. The compiler does not know what data type 'intMyAge' is just by its name. Remember a variable name is just a name to refer to a location(address) in memory, where this value is held. This is a naming technique used so the programming can see whats happening more clearly and keep track of variables, and their typed. Recap ------ So far you should be familiar with the following. 1) What a header file is and how to #include it in our program. 2) What a variable is, how to declare it and give it a descriptive name. 3) What a function is, and understanding of the main() function. 4) What braces are and how we use them in functions. 5) Data types including the "void" data type. 6) Code undentation and the purpose of it. 7) How to run our program and which compilers to use for both Windows and Linux. Thankyou For Reading My First Chapter of my C Tutorials, If you feel something was not explained well or in too little detail, Contact me at dragoonis@gmail.com