Introduction To Programming in C: A Tutorial For Beginners, by - RMI, NIT Trichy
Introduction To Programming in C: A Tutorial For Beginners, by - RMI, NIT Trichy
What is C ?
Lame question ,yes agreed . But just to clear your basics , let me explain : C is a high level programming language . Just like how we have tamil , telugu, english,etc to communicate with humans , we use a HLP language to give 'commands' to a machine be it a supercomputer or a miniature microcontroller . A machine is a machine is a machine .
All the codes you type in a HLP language like C gets translated or technically 'compiled' into machine level language which is binary codes 1s and 0s that your machine can actually understand. So the next question is ,Who does this job ? A compiler !
Source program
libraries
compiler
Errors ???
Executable program
The C compiler
Now that you know about a compiler, where do I get those from ? Compilers are nothing but softwares . There are a lot of C or C++ compilers available online . Eg : TurboC , DevC++ , etc
With that level of basics, lets dive straight into basic programming in C
In this tutorial , we'll concentrate on all that basics necessary for microcontroller programming alone . Why ? You would'nt be needing other complicated stuff in C for simple programs for the Microcontroller .
If you wish to learn more, all you would need is interest, patience and a good reference manual . Some books to refer : Sumita Arora , Schaums series. etc
#include
You must have seen the above piece of code before in your first semester . Ever wondered what that was ?
It's called a preprocessor directive Its good you remember this piece of word before you plunge yourself into books on C .
Pre-processor commands are distinguished by the hash (number) symbol #. One example of this has already been encountered for the standard header file stdio.h.
#include <stdio.h>
is a command which tells the preprocessor to treat the file stdio.h as if it were the actually part of the program text, in other words to include it as part of the program to be compiled.
Consider this example . The cook in our mess. How does he make sambar ? Firstly, he knows the receipe and the procedure . The receipe list is analogous to your #include , the preprocessor directive , yes, it's that simple . What you code in C is the procedure to make sambar . The boiling , adding ingredients , etc is all done by our friend , the Compiler that asks the #include for the 'receipe' and our code for the 'procedure'. The product ? Tasty sambar / working Program .
You define #include<iostream.h> to include all the function and definitions under iostream
There is #include<math.h> to include all mathematical functions . Say sin(30) , it will compute the result , putting it simple , cook up your sambar . When you are into micocontroller programming , you define #include<avr/io.h>
LIBRARIES
Libraries are files of ready-compiled code which we can merge with a C program at compilation time. Each library comes with a number of associated header files which make the functions easier to use.
For example, there are libraries of mathematical functions, string handling functions and input/output functions and graphics libraries. It is up to every programmer to make sure that libraries are added at compilation time by typing an optional string to the compiler.
By declaring void main() { } after defining the headerfiles , you tell your compiler that yes, this is from where I start telling you the procedure to cook sambar or the main code of your program .
With all these basics in mind , Welcome to C programming
#include<iostream.h>
void main()
{
DECLARATIONS
Compiler languages require us to make a list of the names and types of all variables which are going to be used in a program and provide information about where they are going to be used. This is called declaring variables.
It serves two purposes: firstly, it provides the compiler with a definitive list of the variables, enabling it to cross check for errors,
and secondly, it informs the compiler how much space must be reserved for each variable when the program is run. C supports a variety of variable types (variables which hold different kinds of data) and allows one type to be converted into another. Consequently, the type of a variable is of great importance to the compiler. If you fail to declare a variable, or declare it to be the wrong type, you will see a compilation error.
A variable is nothing but a storage platform . A plastic box used to store turmeric in our mess sambar example is analogous to a variable ! . In C , there are different types of variables. A variable can store a number , or a character .
Integer variables are declared by writing this piece of code : int <variable name without these arrows> ; character variables are declared like this : char <variable name without these arrows> ;
Operation :The action which was carried out upon the operands by the operator!There are lots of operators in C. Some of them may already be familiar:
+ - * / = & ==
These operators would not be useful without a partner operator which could attach the values which they produce to variables. Perhaps the most important operator then is the assignment operator: = assignment operator This has been used extensively up to now. For example: double x,y; x = 2.356;
y = x;
x = x + 2 + 3/5;
The assignment operator takes the value of whatever is on the right hand side of the = symbol and puts it into the variable on the left hand side. The assignment operator can be summarized in the following way:
lvalue = expression;
An expression is simply the name for any string of operators, variables and numbers.
Is that all ?
Obviously NO . There are much more datatypes offered by C. Refer any standard C book to know more. We'll be dealing only with int, float , double , char .
Lets move onto conditional statements !.
As long as the condition is true , the while loop keeps executing the code it is given . This sample code illustrates how a variable's value is incremented until it reaches 100 . //assume headers and void main int rmi=1 ; while(rmi<101) { rmi=rmi+1; }
So, as soon as the value stored by rmi reaches 101, the condition goes false and the loop ends .
The if-else
Suppose your integer variable contains a value stored. For example 7 . How do you check that ? Examine this self explanatory sample code :
#include<iostream.h> void main() { int rmi=7; if(rmi==7) //check if it is equal to 7 { //do something } else if(rmi!=7) //check if it is not equal to 7 { //do nothing } }
For loop
I'm leaving it to you to findout why 'for loop' is used for . This program keeps incrementing a variable until the condition stays true and also runs a code you declare inside the for loop after the change each time . for(rmi=0 ;rmi<100; rmi=rmi+1)
//type your code you want to execute after the //value of rmi changes each time , here ! }
Switchcase
Sample program : switch(rmi)
{ case '1' : //execute code break;
Research on what would happen if the break; is not given ? We will get back to you on this question during the workshop ! ;)
End Of Tutorial
So , that was what necessarily you need to be knowing to not doze off during the coding lectures in genesis ;)
What extra will you be learning in genesis apart from this ? Using this knowledge in C to program your Atmega8 MCU .
Implementing the Wall Tracking algorithm in a C environment . Plus , we've planned to teach you the line follower algorithm as a bonus ! :)