Learning C with Psycho_Coder | Part:1 04-06-2013, 09:39 PM
#1
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:-
The above is the correct example of a single line comment.But you write it like below then it is wrong .
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 .
Here is the complete format of a C program .
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
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 .
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:
- The interpreter takes one statement then translates it and executes it and then takes another statement . While the compiler translates the entire program in one go and then executes it .
- Compiler generates the error report after the translation of the entire page while an interpreter will stop the translation after it gets the first error .
- Compiler takes a larger amount of time in analyzing and processing the high level language code comparatively interpreter takes lesser time in the same process .
- Besides the processing and analyzing time the overall execution time of a code is faster for compiler relative to the interpreter .
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
![[Image: History-of-C-Programming-Language.jpg]](http://img.c4learn.com/2012/02/History-of-C-Programming-Language.jpg)
Timeline of Inventors
![[Image: History-of-C-Programming-Language.jpg]](http://img.c4learn.com/2012/02/History-of-C-Programming-Language.jpg)
Features of C
C has the following features :-
- C Programs are portable i.e they can be run on any Compiler with Little or no Modification .
- C Compiler and Preprocessor make it Possible for C Program to run it on Different PC .
- C takes the concepts of both Low-Level and High Level Programming Language , hence it is also called Middle-Level Programming Language . It is more User friendly as compare to Previous languages . Previous languages such as BCPL , Pascal and other programming languages never provide such great features to manage data .
- C Programs can be manipulated using bits .
- C follows Modular Programming . C Program Consist of Different Modules that are integrated together to form complete program
- Pointers has direct access to memory . C makes efficient use of pointer .
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 :-
- C language was mainly developed for System programming .
- It can be used for creating computer application .
- C is used for writing "embedded softwares" .
- It is also used in developing verification software , test code , simulators etc. for various applications and hardware products .
- For Creating Compiles of different Languages which can take input from other language and convert it into lower level machine dependent language .
- C language has also find its application in the field of developing Network Drivers .
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)
- Turbo C++ IDE
- Borland Turbo C++
- Microsoft Visual C++ [Recommended]
- Emulated Turbo C++ (If you are using Windows 7)
- Dev C++ (Runs good In XP and Windows 7)
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
Hack Community .
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 -
/* Hello my name is [username], and I love Hack Community. */
or
/* Hello my name is [username], and
I love Hack Community. */
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 :-
- stdio.h :- Standard I/O. It contains functions for Input and output, like getc(), gets(),printf() etc.
- stdlib.h :- Standard Library. example :- exit()
- conio.h :- Console I/O. example :- getch()
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>
- # : This is called pre-processor directive . It's not a part of our program ; it's an instruction to the compiler to make it do something .
- include : It says the C compiler to include the contents of a file within the delimiters < > or " " , in this case it is stdio.h
- Delimiters : These contain the file name that is to be included into the program .
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 ?
- What is C ?
- History of C.
- Applications of C.
- Very Basic elements of C.
- Header files and comments in C.
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
![[Image: OilyCostlyEwe.gif]](http://fat.gfycat.com/OilyCostlyEwe.gif)
![[+]](https://sinister.ly/images/modern/collapse_collapsed.png)
![[Image: 2YpkRjy.png]](http://i.imgur.com/2YpkRjy.png)
,