![]() |
|
Learning C with Psycho_Coder | Part:1 - Printable Version +- Sinisterly (https://sinister.ly) +-- Forum: Coding (https://sinister.ly/Forum-Coding) +--- Forum: C, C++, & Obj-C (https://sinister.ly/Forum-C-C-Obj-C) +--- Thread: Learning C with Psycho_Coder | Part:1 (/Thread-Learning-C-with-Psycho-Coder-Part-1) |
Learning C with Psycho_Coder | Part:1 - Psycho_Coder - 04-06-2013 Hello , [username]
Before starting the tutorial I want to mention that please be patient while you read the whole article . Read till the end to get interest in C programming . This tutorial is specially for the Noobs but I think other programmers who are well versed with C can enjoy the tutorial as well . If I have missed something then please tell me . So lets start , What is a Programming Language ? Before , proceeding I think its better to tell you what do you mean my programming language . A programming language are a set of instruction that are given to a computer to perform certain user-defined tasks.A programming language is always accompanied with a Compiler , which is nothing but a type of converter , which converts the programming instructions into machine readable format i.e. Binary format . Example of Programming languages :- C , C++ , Java , Python etc. Interpreter and Compiler (Source Here and Here) Compilers : A compiler is a program that translates source code of a programming language into any kind of computer language . When a user writes a code in a high level language such as Java and wants it to execute , a specific compiler which is designed for Java is used before it will be executed . The compiler scans the entire program first and then translates it into machine code which will be executed by the computer processor and the corresponding tasks will be performed . C Programming language is compiled and not interpreted. Shown in the figure is basic outline of the compilation process , here program written in higher level language is known as source program and the converted one is called object program . Interpreter : Interpreters are not much different than compilers . They also convert the high level language into machine readable binary equivalents . Each time when an interpreter gets a high level language code to be executed , it converts the code into an intermediate code before converting it into the machine code . Each part of the code is interpreted and then execute separately in a sequence and an error is found in a part of the code it will stop the interpretation of the code without translating the next set of the codes . The main differences between compiler and interpreter are listed below:
Need Something More Click Here! Machine code files are self-contained modules of machine code that require linking together to build the final program . The reason for having separate machine code files is efficiency ; compilers only have to recompile source code that have changed . The machine code files from the unchanged modules are reused . This is known as Making the application . If you wish to recompile and rebuild all source code then that is known as a Build . Linking is a technically complicated process where all the function calls between different modules are hooked together , memory locations are allocated for variables and all the code is laid out in memory , then written to disk as a complete program . This is often a slower step than compiling as all the machine code files must be read into memory and linked together . The C Programming Language The C language was invented at Bell Labs by Dennis Ritchie in 1972 to allow the writing of the UNIX operating system , then developed by Ken Thompson and Dennis Ritchie . Initially C was developed for writing to UNIX operating system . UNIX was developed in the late 1960s or 1970s and was written in Assembly Language for the machines it was intended to , but it was not architecture neutral , So , there was a need for a programming language that could solve this problem . C is basically a middle-level , procedural programming language that uses the Top-Down Approach . History of C programming Language (Source Here) Among the languages available at the time , BCPL (Basic Combined Programming Language for , which is a simplification of CPL) , created by Martin Richards in 1966 , was interesting . Without going into detailed descriptions , BCPL is a simple language , procedural , and not typed . Its simplicity makes it easy to create BCPL compilers on machines of the time , when resources were very limited (the first computer used by Keith Thompson was to launch a Unix PDP-7 , which had a memory of 4000 words 18 bits , or less than 9 KB) . Ken Thompson has evolved to design the B language , which he implemented the first UNIX machines . However , some limitations of language B were UNIX could be rewritten in this language . From 1971 , Dennis Ritchie B did evolve to address these issues . Like the programmers increment versions of their programs , Ritchie "Nudge" the letter B to call the new language C . This evolution is "stabilized" to 1973 , from which UNIX and UNIX system utilities have been rewritten successfully in C . Subsequently in 1978 , Brian W. Kernighan documentation very active language, and finally publish the book with reference Ritchie "The C Programming Language" . Often called K & R C language as specified in the first edition of this book . In the years that followed , the C language was carried on many other machines . These ports were often made at the beginning , from the pcc compiler Steve Johnson , but then the original compilers were developed independently . During these years , every C compiler was written in accordance with the specifications of K & R , but some added extensions , such as data types or additional features , or different interpretations of certain parts of the book (not necessarily very precise) . Because of this , it was less easy to write C programs that can run unchanged on many architectures . Timeline of Development of C programming Language ![]() Timeline of Inventors ![]() Features of C C has the following features :-
Why C can be called as a Middle-Level programming Language ? Well there has been debates about C being a High Level or Low Level Language . Now Let's look closely to some features of C that's makes our doubt clear . First of all , since C supports inline Assembly programs and using this we can access registers . Hence C can be called as a Low-Level Programming , also C supports pointers and Memory addressing , which further supports the previous statement . Now C uses Keywords like switch , if-else , for etc. which is a feature of High Level Programming.Also , C does not supports nesting of functions at the hardware level , and this supports the previous statement as well . Also , C is used for both system programming and Application programming hence its is better to call C a Middle-Level Programming Language . Applications of C Programming Language (Source here . Note:- Only Some points has been taken from the source mentioned .) C has various application in the computing and engineering field as well . Here are some of the applications :-
Now that we have covered the basics of C programming . So we are to get started with really learning the Syntax and other related things necessary for programming . But before that you need a compiler in which you can compile the programs and run them to see the output . Tools(Any one of the following)
Note:- I am leaving the job on you on how to compile the C programs . As I will explain only a bit of it.As everything cannot be spoon feed . Search Google or better see YouTube Videos on how to use the above tools to compile your C program . I will give you some helpful links regarding this at the end of this article . C Coding Specifications A C program follows a definite pattern . That is nothing but a set of rules that are important to be followed for successful compilation of a C program . A C program starts with the declaration of Header files then global variable(if used) , main() function and { } , blocks within which the main code will be written . You can use comments in your C program to help yourself understand what you have done . Comments are simple statements but they are never executed by the compiler . There are two types of comments in C 1. Single Line Comment . Single line comments are preceded by a pair of front slash like this //, what ever you write after give this first and as long as your statement stays in the single line it will be a comment . Example:- Quote:// Hello my name is [username] , and I love Hack Community. The above is the correct example of a single line comment.But you write it like below then it is wrong . Quote://Hello my name is [username] , and I love The above will give you an error . 2. Multi-Line Comment Some times you may have to write comments that might not fit in a single line.So we have to use a Multi-line Comment . The the Multiline comment you need to write your text within this /* write your comment here */ . As long as your comment is within /* .. */ it will be a comment . Quote:Example - Here is the complete format of a C program . Code: #include <stdio.h>
//other Header files can also be declared here
int main()
{
...........
/*Here you have to write
your code*/
...........
return 0;
}Explanation of the Above . 1. Header Files Code files (with a .cpp extension) are not the only files commonly seen in programs . The other type of file is called a header file , sometimes known as an include file . Header files almost always have a .h extension. The purpose of a header file is to hold declarations for other files to use . Any in-built C function you use in your code has been defined in any of the header files . There are many header files . It is important to note that that header files typically only contain declarations . They do not define how something is implemented , To list some useful and commonly used header files are :-
Well you can create your own header files , also if you think that we can only include header files in a program then you're wrong , we can even include a .c file like this - #include "factorial.c" . I will later post on how to make your own Header files . 2. main() Function Just As the name suggests its main , that is this the most important part in any program as the main method or function is executed first . If you have made functions then also the main() function will be executed first as this kind of instruction has been provided in the compiler . We can pass arguments in the main function but we will discuss them in the later tutorials . 3. Miscellaneous a.) Blocks: Any function you create you need to create a block for it.Within which you have to write your instructions . The group of statements defining the main () enclosed in a pair of braces ({}) are executed sequentially Code: #include "stdio.h"
int main()
{//Beginning of block
// Your codes and instructions here
return 0;
}//Block ended here.b.) Semicolons : Semicolons are important as they are called termination operator . Every statement ends with a ';' c.) Return : Since we have given a return type for the main function that is int (Integer) so we need to return a value else we will get an error . If you don't want main to return a value then you need to use "void" keywords . d.)Keywords: "Keywords" are words that have special meaning to the C compiler . These are reserved words and you cannot use them as variable name . Here you will get the list of all Keywords In C Dissecting a Header File We have already seen header files so what are they and do they do and what does those symbols mean , # the <> or "" . Okay So , lets discuss it now . Let's consider a header file #include<stdio.h>
Difference Between < > and " " ? Well I hope that you must have noticed that I have used #include<stdio.h> as well as #include "stdio.h" which might have arose a question in your mind that 'what to use <> or " " ?' . So here is the explanation . The <> characters around the name tells C to look in the system area for the file stdio.h . If I had given the name "psycho.h" instead it would tell the compiler to look in the current directory where the program has been saved . This means that I can arrange libraries of my own routines and use them in my programs . What you learnt ?
So , I am ending this tutorial now as it is getting very big.In the next tutorial of this series . You will learn how to do a basic C program and we will see how to compile them and also do error handling . I hope you enjoyed the tutorial just as I enjoyed while making this tutorial . Leave comments and if there is any error , then please tell me I will correct it . Thank you , Psycho_Coder . My Other Tutorials :- RSA Encryption Explained MySQL with C API RE: Learning C with Psycho_Coder | Part:1 - Deque - 04-09-2013 Hi @Psycho_Coder. First of all, this is a very good tutorial. I am impressed by the work and detail you put into it. Nevertheless I have some comments that may help you to improve it. Your target group is not clear. On the one hand you explain what a programming language is, but you expect the reader to know what a compiler is or what a preprocessor, inline assembly etc is. I suggest you either remove the programming language explanation, or you put in far more explanations for the other terms that might be unknown to newbies. Your use of spaces before and after punctuation marks is inconsistent and wrong sometimes. You should do: previoussentence nospace punctuationmark space nextsentence You recommend Dev C++, although it is outdated. It hasn't been updated since 2005 and has a lot of known bugs that aren't fixed anymore, because no one maintains it. I would remove that one. For Windows you can include Visual Studio Express as IDE recommendation. Are you ok if I include your tutorial here: http://www.hackcommunity.com/Thread-Programming-language-introductions I really would like to see an example explanation how to compile and run a simple C program. And if it is just by using a text editor and gcc in a terminal. RE: Learning C with Psycho_Coder | Part:1 - Psycho_Coder - 04-09-2013 (04-09-2013, 07:39 PM)Deque Wrote: Hi @Psycho_Coder. Yes sure you can include this in the thread.That will be an honor . I will edit the thread with a bit more details regarding Compilers, interpreter. I wanted to put preprocessors but I thought to make whole tutorial regarding pre-processors as that is a huge part , a lot of thing is there to explain.I will use Visual Studio for compiling the programs and all punctuation will be corrected. About that example explanation of a C program , should I post it here in this thread or make a separate thread (I recommend this) regarding this. Thanks for your feedback. RE: Learning C with Psycho_Coder | Part:1 - Psycho_Coder - 04-10-2013 @Deque I have updated the thread , correcting the punctuation , gave some details on Compiler and Interpreter . More suggestion's are welcomed , any error or if I have made any mistakes please point it out . Also I will be using Visual Studio 2010 as compiler for explaining codes . Thank You, RE: Learning C with Psycho_Coder | Part:1 - H4R0015K - 04-10-2013 just a question in the sample code above why not use void main() has the main function doesn't return anything. RE: Learning C with Psycho_Coder | Part:1 - Psycho_Coder - 04-10-2013 (04-10-2013, 07:00 AM)H4R0015K Wrote: just a question in the sample code above why not use void main() has the main function doesn't return anything. Thank you , for pointing out the mistake . It has been edited RE: Learning C with Psycho_Coder | Part:1 - Deque - 04-10-2013 (04-10-2013, 07:04 AM)Psycho_Coder Wrote:(04-10-2013, 07:00 AM)H4R0015K Wrote: just a question in the sample code above why not use void main() has the main function doesn't return anything. int main is not a mistake, it is a standard. A lot of compilers do not allow void main, which is correct. int main returns the code that indicates whether the program ran correctly. 0 means that everything is fine. So leave it in there. Everything else would be bad practice. (04-10-2013, 06:46 AM)Psycho_Coder Wrote: @Deque Visual Studio is an IDE, not a compiler. I will read over your tutorial and post again if I see something. Edit: Knowing the term Interpreter is not necessary for a C introduction. C is compiled language, whereas Java is compiled into an intermediate language (Bytecode), which is interpreted by the virtual machine. The examples and pictures show how Java works, but it has nothing to do with C. Maybe you should use other sources. RE: Learning C with Psycho_Coder | Part:1 - Deque - 04-10-2013 (04-10-2013, 07:04 AM)Psycho_Coder Wrote:(04-10-2013, 07:00 AM)H4R0015K Wrote: just a question in the sample code above why not use void main() has the main function doesn't return anything. int main is not a mistake, it is a standard. A lot of compilers do not allow void main, which is correct. int main returns the code that indicates whether the program ran correctly. 0 means that everything is fine. So leave it in there. Everything else would be bad practice. (04-10-2013, 06:46 AM)Psycho_Coder Wrote: @Deque Visual Studio is an IDE, not a compiler. I will read over your tutorial and post again if I see something. Edit: Knowing the term Interpreter is not necessary for a C introduction. C is compiled language, whereas Java is compiled into an intermediate language (Bytecode), which is interpreted by the virtual machine. The examples and pictures show how Java works, but it has nothing to do with C. Maybe you should use other sources. RE: Learning C with Psycho_Coder | Part:1 - Psycho_Coder - 04-10-2013 (04-10-2013, 08:00 AM)Deque Wrote:(04-10-2013, 07:04 AM)Psycho_Coder Wrote:(04-10-2013, 07:00 AM)H4R0015K Wrote: just a question in the sample code above why not use void main() has the main function doesn't return anything. Sorry for that Visual studio compiler , I should have mentioned Microsoft Visual C++ Okay I will remove the image of the working of interpreter but I think I should leave the description. Reason : Users need to know what is an Interpreter and compiler . Since C is compiled it will be easier for them to understand what happens. Also , they should know the difference between Interpreter and Compiler. If you Still think that its not correct then I will edit and remove that part for sure no problem ,For the further tutorials in this series I will be using Visual Studio 2010 Command Prompt for compiling the source code , Codes will be written in any text editor. RE: Learning C with Psycho_Coder | Part:1 - Deque - 04-10-2013 Quote:A compiler is a piece of code that translates the high level language into machine language This statement is wrong. A compiler is a program that translates source code of a programming language into any kind of computer language. It doesn't have to be machine code. In case of the Java compiler it is bytecode. In case of C it is assembler. |