Beginners Tutorial : The C-language.

Discussion in 'Computer Science | IT | Networking' started by simplycoder, Aug 19, 2011.

  1. simplycoder

    Engineering Discipline:
    Electronics & Communications
    Hello this is simplycoder, and welcome to the series of C programing tutorials.

    I request not to post questions or anything in this thread, as it will only disrupt the flow and co-ordination of the tutorials.


    OK everyone, this tutorial is basically designed for programmers, basically new to computing or new to "The C-Language".
    So now let us get started with the C.


    History of C


    • C language was developed by Dennis Ritchie in early 70s at the Bell Laboratories for Unix operating system.
    • C is a procedural language.
    • C was influenced by B, ALGOL, FORTRAN and Assembly.
    • C influenced most of the modern world programing languages which include the mighty C++ and Java as well.
    Above points are enough to answer the most common questions asked in vivas (oral-examinations) or objectives.

    As of now, this much is enough for more, google for C.
    Tutorial-1 will be posted in another dedicated post.
    Until then take care
    Ciao.

    PS: These tutorials will be based on DevC++ IDE and on Windows platform
    you can download DevC++ from here Bloodshed Software - Providing Free Software to the internet community
    -------------------------------------------------------------------------------------------------------------------------------------
    INDEX
    Tutorial 1
    Tutorial 2
    Tutorial 3
    Tutorial 4
    Tutorial 5
    Tutorial 6
    Tutorial 7
    Tutorial 8
    Tutorial 9
    Tutorial 10
    Tutorial 11
    Tutorial 12
    Tutorial 13
    Tutorial 14
    Tutorial 15
    Tutorial 16
    Tutorial 17
    Tutorial 18

    -------------------------------------------------------------------------------------------------------------------------------------

     
    • Like Like x 2
    #1
  2. simplycoder

    Engineering Discipline:
    Electronics & Communications
    Re: Beginners Tutorial : The C-language :: Tutorial-1

    Tutorial-1
    Hello this is simplycoder and welcome to the first tutorial on C.

    In this tutorial, we are going to learn the basic structure of a C-program.

    Any C program will contain a function called as: int main(int argc,char*argv[])
    Now let us break this in parts and analyze one by one.

    1)What is a function?
    Function is a small part of entire program,containing some set of instructions that is generally dedicated to perform a specific task.
    Advantages: Easy to debug,code resuability.
    Disadvantage: Lengthy code.

    2) int: it is a built in data type stands for Integer, we are going to understand the data-types in more detail in comming tutorials, as of now just understand that a data-type is something that signifies the type of data,which helps the compiler to allocate the memory. We are also going to learn about memory allocation and other stuffs in later tutorials,however as of now we can describe memory allocation similar to reserving space for something. Typing 'int' in front of the function,tells the compiler to return an Integer value once you finish the execution from the function.

    3)main: This is the name of the function. Every program,like in real life, needs a starting point. main exactly does that. Generally, any name followed by '()' signifies that it is a function. This is basically syntax. Whatever is typed inside the '()' are called as parameters.

    4) As of now, we can skip what is the meaning of (int argc,char*argv[]). We will require this when we read some file from the command line, but as of now its ok even if we skip it.

    Now that we know meaning of individual parts in the main function, there are few more things that are required to understand before we can start writing our first program.

    Let me explain the next concept with an example.

    Consider a large thermo-flask of coffee , similarly one that of milk and so on and these flask are never empty.
    In order to make one cup of coffee, I just have to pour some coffee and milk in my cup. That is it.
    Similarly in order to make 10 cups of coffee,I just have to pour some coffee and milk in 10 cups according to their needs,they can have black coffee aswell.
    The point is that I will never make coffee again by myself, all I will do is to use the coffee which is already available,use some milk which is already available,without bothering about it or how that coffee was brewed or any details on milk.

    In C, there are various inbuilt functions which can be used by programers. Say for example, there is a function which helps to print some thing on screen,then there is another which helps to read the input from the user, without even bothering about it. This is called as code reusability.
    In C, there are two such important files

    1)stdio.h
    2)stdlib.h

    stdio.h stands for 'standard input-output header file'
    stdlib.h stands for 'standard library functions header file'

    What is a header file?
    Header files allow programmers to separate certain elements of a code into reusable files. Header files commonly contain declarations and in this way, it helps to standardized a program.Header files donot contain implementations.

    Now the question arises: how do we use the material from the above two header files? How can we convey the compiler to include these two files in our program?There is our next syntax,

    5)#include:
    This is basically called as preprocessor directive, as from the name, we can easily guess that it is going to be accessed before the process starts.

    In C-style syntax, we tell compiler as follows:
    #include<stdio.h>
    #include<stdlib.h>

    In simple terms, compiler includes the files in the angle bracket along with your source code.Now we are in a position to write our first program based on our basic structure using some more things along with what we have already learnt.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    /*Author : simplycoder*/
    int main(int argc, char *argv[])
    {
      printf("Hello World!");//Print something.
      system("PAUSE");    
      return 0;
    }
    
    6)printf(): It is a built in function which is used to print the data on the screen.It stands for print formatted as it accepts a string,another data-type along with other format specifier, things will be cleared once we start to program regularly.

    7)system("PAUSE"): This is pretty much self explanatory, try running your code without this and check for the results.

    8)return 0: This is called as fullfilling the promise. We made a promise to the compiler that main function will return an integer once it about to exit.

    Minor details before I conclude with:

    9)'{..}' or use of curly brackets: This tells the compiler- "No matter what it appears, treat this block of code as a single statement"
    We shall see much more use of curly braces in control-structures.

    10) ';' or use of semi-colon: this conveys the compiler that it is the end of the statement syntactically, its similar to period in english.

    11)The comments: There are 2 types of comments in C.

    11.a) // or Single line comment : This tells the compiler,"Donot consider the data beyond the '//' on that particular line"

    11.b)/* ... Some comment ...*/ or multiline comment: This tells the compiler" Donot consider the block of data included between '/*' and '*/'

    Comments are really very useful in lengthy programs, they help to make other users quickly understand the block of code or nature of variables or in general anything that the author of the program wants to convey.

    Exercises:
    Modify the above program,check what errors you get if you remove ';'
    Modify the above program,check what errors you get if you remove '{' or '}'
    Modify the above program,check what errors you get if you remove 'system("PAUSE")'
    Play with it until you get bored of it.;)

    I know this has been a long tutorial hopefully it wasnt boring either, please send me a PM if you found some bug or something that I may have skipped or if you disliked the tutorial or if you liked it what ever it is, please give a feed-back so that I would know where to improve in my next tutorial.
    In next tutorial, we shall discuss concepts related to variables,data-types and more..
    Keep playing with C
    Until then take care
    Ciao.
     
    • Like Like x 3
    #2
  3. SagaRocks

    SagaRocks Certified CEan

    Engineering Discipline:
    IT
    tutorial is nice but add some colors to it so that it will looks better and more peoples will read it..!!! :)
     
    #3
  4. simplycoder

    Engineering Discipline:
    Electronics & Communications
    Re: Beginners Tutorial : The C-language:: Tutorial-2

    Tutorial-2

    Hello everyone again this is simplycoder and welcome to the 2[SUP]nd[/SUP] tutorial of C-programing.
    In this tutorial, we are going to learn about variables,different data-types and other related stuff.

    What are data-types?
    In C-programing, a data can be represented in different formats.
    an example might be as follows, in mathematics, 12 and 12.0 are same, however in computer science, the memory required by 12 and 12.0 is different.
    The number of bits required to store an integer is not the same as the number of bits stored by a floating point value(decimal point value).
    So the question arises how to tell the compiler to allocate the memory for different types of data?
    The answer to this is by telling the complier the data-type of the value or data which is the type of the data.
    C language data types can be classified as


    • Primary data type
    • Derived data type
    • User-defined data type
    Concept of : signed,unsigned,short and long.
    This is pretty simple as signed data-type means that it will cover the positive and the negative values as well.
    Unsigned data-type will cover only the positive values starting from 0.
    short datatype will allocate either same or less than the main type of the data and definitely lesser memory as compared to long-datatype of the same.
    short int <= int <= long int
    float <= double <= long double


    [TABLE="class: grid"]
    [TR]
    [TD]Type[/TD]
    [TD]Size (Bits)[/TD]
    [TD]Range [/TD]
    [/TR]
    [TR]
    [TD]Char or Signed Char[/TD]
    [TD]8[/TD]
    [TD]-128 to 127[/TD]
    [/TR]
    [TR]
    [TD]Unsigned Char[/TD]
    [TD]8[/TD]
    [TD]0 to 255[/TD]
    [/TR]
    [TR]
    [TD]Int or Signed int[/TD]
    [TD]16[/TD]
    [TD]-32768 to 32767[/TD]
    [/TR]
    [TR]
    [TD]Unsigned int[/TD]
    [TD]16[/TD]
    [TD]0 to 65535[/TD]
    [/TR]
    [TR]
    [TD]Short int or Signed short int[/TD]
    [TD]8[/TD]
    [TD]-128 to 127 [/TD]
    [/TR]
    [TR]
    [TD]Unsigned short int[/TD]
    [TD]8[/TD]
    [TD]0 to 255[/TD]
    [/TR]
    [TR]
    [TD]Long int or signed long int[/TD]
    [TD]32[/TD]
    [TD]-2147483648 to 2147483647[/TD]
    [/TR]
    [TR]
    [TD]Unsigned long int[/TD]
    [TD]32[/TD]
    [TD]0 to 4294967295[/TD]
    [/TR]
    [TR]
    [TD]Float[/TD]
    [TD]32[/TD]
    [TD]3.4 e-38 to 3.4 e+38[/TD]
    [/TR]
    [TR]
    [TD]Double[/TD]
    [TD]64[/TD]
    [TD]1.7e-308 to 1.7e+308[/TD]
    [/TR]
    [TR]
    [TD]Long Double[/TD]
    [TD]80[/TD]
    [TD]3.4 e-4932 to 3.4 e+4932[/TD]
    [/TR]
    [/TABLE]





    The above values are for a 16-bit machine.

    We shall discuss derived data-types and user-defined data-types in their dedicated tutorials.
    As of know, knowing just the simple data types is more than enough.

    What are variables?
    Variables in computer science is something that symbolizes the known or unknown data.
    Variable names are independent of the data-type of the variable.
    Variables can be changed in the course of execution under proper conditions.
    Variable names are nothing but temperory names given to some part of memory.

    Now we are in a position to write our first program which involves usage of variables.
    In this program, we are going to add two numbers and store them in a variable and display this variable.
    Code:
    /*Author : simplycoder*/
    #include<stdlib.h>
    #include<stdio.h>
    
    int main(int argc,char*argv[])
    {
        int number1=5;
        int number2=10;
        
        int answer=number1+number2;//Add the two numbers
        printf("The answer is %d\n",answer);
        system("PAUSE");
        return 0;
    }
    
    In the above code,number1,number2,answers are called as variables.
    int number1=5, this tells the compiler that the variable number1 is going to contain an integer,so allocate memory required to store an integer.
    same is with number 2 and answer.
    Take a look at the statement 'printf("The answer is %d\n",answer);'
    As stated in tutorial-1, printf() takes the format specifier, "%d" is used when we expect to print an integer value.

    Here is a list of format specifiers:
    %c The character format specifier.
    %d The integer format specifier.
    %i The integer format specifier (same as %d).
    %f The floating-point format specifier.
    %e The scientific notation format specifier.
    %E The scientific notation format specifier.
    %g Uses %f or %e, whichever result is shorter.
    %G Uses %f or %E, whichever result is shorter.
    %o The unsigned octal format specifier.
    %s The string format specifier.
    %u The unsigned integer format specifier.
    %x The unsigned hexadecimal format specifier.
    %X The unsigned hexadecimal format specifier.
    %p Displays the corresponding argument that is a pointer.
    %n Records the number of characters written so far.
    %% Outputs a percent sign.

    try it out yourself.

    Next to format specifier, you shall find "\n". This is called as an escape sequence.
    An escape sequence is combination of a backslash '\' and a letter. Its taken as one single character.

    The following is a list of commonly used escape sequences.
    \n : Newline
    \t : Horizontal Tab
    \v : Vertical Tab
    \b : Backspace
    \r :Carriage Return
    \f :Form feed
    \a :Audible Alert (bell)
    \\ :Backslash
    \? :Question mark
    \' : Single quote
    \" : Double quote

    ************************************************************************************************
    Exercise:
    Write program to subtract,multiply and divide(this is slightly tricky, grasp the knowledge of data-types first).
    Write program to find average of two numbers.
    Write many simple programs till the time,either you are bored or you get proper hang of the syntax.;)
    Play with different data-types and escape sequences.
    ************************************************************************************************

    OK. Here I conclude this tutorial.Do let me know about this tutorial via a PM. Your feed-back will help me improve and in turn make better tutorials.
    In next tutorial we shall learn how to take input from the users, something related to storage classes , constants and other related stuff.
    Until then take care.
    Ciao




     
    #4
  5. simplycoder

    Engineering Discipline:
    Electronics & Communications
    Re: Beginners Tutorial : The C-language:: Tutorial-3

    Tutorial-3

    Hello everyone again this is simplycoder and welcome to the 3[SUP]rd[/SUP] tutorial of C-programing.
    In this tutorial, we are going to learn about keywords,constants, reading input from user(scanf()).

    What are keywords?
    Keywords in C are the words which have special meaning.The meaning of these words is already explained to the compiler.
    So a programer cannot use keywords as variable names. Please note that main is not a keyword.
    Here is the list of keywords for C.
    [TABLE="class: grid"]
    [TR]
    [TD]double[/TD]
    [TD]int[/TD]
    [TD]struct[/TD]
    [/TR]
    [TR]
    [TD]else[/TD]
    [TD]long[/TD]
    [TD]switch[/TD]
    [/TR]
    [TR]
    [TD]enum[/TD]
    [TD]register[/TD]
    [TD]typedef[/TD]
    [/TR]
    [TR]
    [TD]extern[/TD]
    [TD]return[/TD]
    [TD]union[/TD]
    [/TR]
    [TR]
    [TD]float[/TD]
    [TD]short[/TD]
    [TD]unsigned[/TD]
    [/TR]
    [TR]
    [TD]for[/TD]
    [TD]signed[/TD]
    [TD]void[/TD]
    [/TR]
    [TR]
    [TD]goto[/TD]
    [TD]sizeof[/TD]
    [TD]volatile[/TD]
    [/TR]
    [TR]
    [TD]if[/TD]
    [TD]static[/TD]
    [TD]while[/TD]
    [/TR]
    [TR]
    [TD]_Bool[/TD]
    [TD]_Complex[/TD]
    [TD]_Imaginary[/TD]
    [/TR]
    [TR]
    [TD]restrict[/TD]
    [TD][/TD]
    [TD][/TD]
    [/TR]
    [/TABLE]







    What are constants?
    Constants, as the name suggests, are some values that never change. In the previous tutorial, you have seen how a variable can be declared. You can declare a constant by using keyword called 'const' or by directly entering its value in the source code. For example, in this code:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    /*Author : simplycoder*/
    int main(int argc, char *argv[])
    {
     const double pi=3.1416;
     float radius=10;
     double area=pi*radius*radius;//Calculate the area.
     printf("Area of the circle having radius %f is %lf\n",radius,area);
     
      system("PAUSE");    
      return 0;
    }
    


    Uptil now, we have seen how to carry out the operations by entering the values into the source.
    However, this is not feasible all the time and hence we should try to take the input from the user, this will make our program more flexible.
    Consider this situation of a post-man.
    If I tell postman, "Can you please deliver this parcel at Mr.XYZ's house?"
    Postman:"No Sir,I cannot."
    Me:"Why not?"
    Postman:"I cannot deliver this parcel for two reason, one I don't know the size of your parcel whether it will fit in my bag or not,second is I donot know the address of the destination."
    Me: Ok if I can provide both details, then can you deliver it?
    Postman: Sure.

    So how to tell compiler to read the input from the user?
    We can follow some simple steps like these:
    1)Allocate the memory for expected input.
    2)Pass on the address of the variable.

    We shall rewrite our code for finding area of the circle,this time by taking input from the user.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    /*Author : simplycoder
    Task:      Calculate Area of Circle by reading input from user.
    */
    
    int main(int argc, char *argv[])
    {
     const double pi=3.1416;
     float radius;
     printf("Enter the radius : \n");
     scanf("%f",&radius);
     double area=pi*radius*radius;//Calculate the area.
     printf("Area of the circle having radius %f is %lf\n",radius,area);
     
       system("PAUSE");    
      return 0;
    }
    
    
    So to read the input, we use an inbuilt function called as scanf.
    So the first argument is the expected size of the input and the second is the address of the variable.
    At first this might sound wierd but then we are going to learn much more in detail regarding addresses and references in our later tutorials,until then, all you need to know is the format of scanf.


    OK, I think this much is enough for tutorial-3, I shall start with storage classes in the next tutorial as it will be another lengthy, theoritical tutorial.
    *****************************************************************************************************************
    Exercises:
    1)Rewrite all the programs from previous tutorials by reading input from keyboard.
    2) Write a program(WAP) which will convert km/hr into m/s.
    3) WAP to convert given length from inches to a)cms b)feet c)meters.
    4)Practice as much as you can.
    *****************************************************************************************************************
    In tutorial-4 we shall see the concept of storage classes and other related stuff.
    Until then, take care.
    Ciao.
     
    #5
  6. TheV

    TheV Rookie

    Engineering Discipline:
    Computer Science
    I am confused....lol...

    this c code is going out of my mind...

    Code:
    float c = 0.7;
    if(c < 0.7)
        printf("C");
    else
        printf("C++");
    in my Turbo C++ compiler it is printing C ... I don't know how.. plz any one explain it....!
     
    #6
  7. Reya

    Reya Ace

    Engineering Discipline:
    Computer Science
    • Like Like x 2
    #7
  8. TheV

    TheV Rookie

    Engineering Discipline:
    Computer Science
    Thank you reya .. . ! Now I understood .. . .!
     
    #8
  9. simplycoder

    Engineering Discipline:
    Electronics & Communications
    Re: Beginners Tutorial : The C-language:: Tutorial-4

    Tutorial-4
    Hello everyone again this is simplycoder and welcome to the 4[SUP]th[/SUP] tutorial of C-programing.
    In this tutorial, we are going to learn about scope,life of a variable,storage classes.

    What is scope of a variable?
    A scope is the region of the program, where the variable is accessible.

    What is the life time of the variable?
    A life time of variable is the duration of program where the variable can retain its value.

    What are storage classes?
    A storage class is something which sets or defines the scope (visibility) and the life time of the variables or functions.

    This is an important concept in C-Programing.
    We know how to allocate memory to a variable, now we can ask "how long will the piece of memory will be assigned to particular variable?"
    The answer to this is give by storage class of the variable.
    Storage class defines the visibility and life time of the variable, this means, a storage class will decide how long a piece of memory will be allocated to a particular variable.

    There are four storage classes in C


    • auto
    • static
    • register
    • extern
    1)auto: auto stands for automatic-storage class. 'auto' is a keyword.
    auto is the default storage class for all the local-variables.(
    A variable defined inside a block and is only visable from within the block is called as local-variable.)
    Code:
      {
       int x;
       auto int y;
       }
    
    The example above defines two variables with the same storage class. auto can only be used within functions, i.e. local variables.
    Thus the scope and the visibility of the the variables with automatic storage class is within the same block where they are defined.

    2)static:static is the default storage class for global variables. The two variables below name and fame both have a static storage class.
    Code:
    static int name;
    int fame
    {
      printf("%d\n", name);
     }
    
    static variables are visible within all functions in this source file.static storage class is default storage class for all the global variables.

    3)register:register storage class is used to define local variables that should be stored in a register instead of RAM. This means that the variable has a maximum size equal to the register size (usually one word i.e. 16bits.) and cant have the memory access operator( unary '&') applied to it (as it does not have a memory location).
    Syntax:

    Code:
    [FONT=arial][SIZE=2]
    register int p;[/SIZE][/FONT][FONT=arial]
    [/FONT]
    4)extern: extern storage class is used for global variables when we want the global variables to be visible to all the files.
    for example we have two files one.c and two.c
    we want to make the global variable from one.c to be visible in two.c, then we can use the extern storage class.
    Syntax for using extern storage class is as follows.
    Code:
    extern int hit;
    
    Since this tutorial is going to be for begineers, I will stick more with auto and static storage classes.
    For begining C-Programing, its enough to know outline of register and extern storage classes.
    We would not use these storage classes as much as static and auto.
    If need arises, I shall describe more on these two storage classes in another dedicated tutorial.

    ***************************************************************************************************************
    Exercises:
    1)Find the output of the following and why does it print that way?
    Code:
    int c=9;
    int main(int argc, char *argv[])
    {
        int c=0;
        printf("%d",c);
        system("PAUSE");
        return 0;
    }
    2)Find the local and global variables in above code?
    3)Find the storage-classes of the above variables.
    4)Try to code using various storage classes as long as you get bored.
    5)Practice.
    ***************************************************************************************************************
    I shall conclude this tutorial here and hopefully we can start with the interesting part of the language.
    The Control-Structures.
     
    • Like Like x 1
    #9
  10. TheV

    TheV Rookie

    Engineering Discipline:
    Computer Science
    Thank you ... simplycoder... this tutorial are very goood.

    Keep posting.....
     
    • Like Like x 1
    #10
  11. simplycoder

    Engineering Discipline:
    Electronics & Communications
    Re: Beginners Tutorial : The C-language:: Tutorial-5

    Tutorial-5
    Hello everyone again this is simplycoder and welcome to the 5th tutorial of C-programing.
    In this tutorial, we are going to learn about the operators which essentially is pre requisite before learning control structures.

    What are operators in C?
    In our previous tutorials, we have seen how to declare and define the variables,their scope and life time, but then we can ask ourselves a question here:"What exactly I am going to do with these variables and constants? Some how I need to perform some operation on them in order to achieve my goal. So how can I perform operations on it?"
    The answer to this question is given by another great concept : The Operators and the Operands.
    The variables and contants in our programs are operands.
    Basically in C, there are only two types of operators:
    • Unary operators.
    • Binary operators.
    By unary operator, it means that it requires only one operand,whereas by binary operator,it requires two operands.(Very simple).
    Here is the list of commonly operators.
    [TABLE="class: grid"]
    [TR]
    [TD="colspan: 6"]Operators[/TD]
    [/TR]
    [TR]
    [TD]Sr.no[/TD]
    [TD]Syntax[/TD]
    [TD]Meaning[/TD]
    [TD]Example[/TD]
    [TD]Type[/TD]
    [TD]Classification[/TD]
    [/TR]
    [TR]
    [TD]1[/TD]
    [TD]++[/TD]
    [TD]Increment[/TD]
    [TD]x++;++x;[/TD]
    [TD]Unary[/TD]
    [TD]Arithmetic[/TD]
    [/TR]
    [TR]
    [TD]2[/TD]
    [TD]--[/TD]
    [TD]Decrement[/TD]
    [TD]x--; --x;[/TD]
    [TD]Unary[/TD]
    [TD]Arithmetic[/TD]
    [/TR]
    [TR]
    [TD]3[/TD]
    [TD]+[/TD]
    [TD]Positive[/TD]
    [TD]+x;[/TD]
    [TD]Unary[/TD]
    [TD]Arithmetic[/TD]
    [/TR]
    [TR]
    [TD]4[/TD]
    [TD]-[/TD]
    [TD]Negative[/TD]
    [TD]-x;[/TD]
    [TD]Unary[/TD]
    [TD]Arithmetic[/TD]
    [/TR]
    [TR]
    [TD][/TD]
    [TD][/TD]
    [TD][/TD]
    [TD][/TD]
    [TD][/TD]
    [TD][/TD]
    [/TR]
    [TR]
    [TD][/TD]
    [TD][/TD]
    [TD][/TD]
    [TD][/TD]
    [TD][/TD]
    [TD][/TD]
    [/TR]
    [TR]
    [TD]5[/TD]
    [TD]=[/TD]
    [TD]Basic Assignment[/TD]
    [TD]x=y[/TD]
    [TD]Binary[/TD]
    [TD]Arithmetic[/TD]
    [/TR]
    [TR]
    [TD]6[/TD]
    [TD]+[/TD]
    [TD]Addition[/TD]
    [TD]x+y[/TD]
    [TD]Binary[/TD]
    [TD]Arithmetic[/TD]
    [/TR]
    [TR]
    [TD]7[/TD]
    [TD]-[/TD]
    [TD]Subtraction[/TD]
    [TD]x-y[/TD]
    [TD]Binary[/TD]
    [TD]Arithmetic[/TD]
    [/TR]
    [TR]
    [TD]8[/TD]
    [TD]*[/TD]
    [TD]Multiplication[/TD]
    [TD]x*y[/TD]
    [TD]Binary[/TD]
    [TD]Arithmetic[/TD]
    [/TR]
    [TR]
    [TD]9[/TD]
    [TD]/[/TD]
    [TD]Divide(Quotient)[/TD]
    [TD]x/y[/TD]
    [TD]Binary[/TD]
    [TD]Arithmetic[/TD]
    [/TR]
    [TR]
    [TD]10[/TD]
    [TD]%[/TD]
    [TD]Modulo(Remainder)[/TD]
    [TD]x%y[/TD]
    [TD]Binary[/TD]
    [TD]Arithmetic[/TD]
    [/TR]
    [TR]
    [TD][/TD]
    [TD][/TD]
    [TD][/TD]
    [TD][/TD]
    [TD][/TD]
    [TD][/TD]
    [/TR]
    [TR]
    [TD][/TD]
    [TD][/TD]
    [TD][/TD]
    [TD][/TD]
    [TD][/TD]
    [TD][/TD]
    [/TR]
    [TR]
    [TD]11[/TD]
    [TD]==[/TD]
    [TD]Equal to[/TD]
    [TD]x==y[/TD]
    [TD]Binary[/TD]
    [TD]Relational[/TD]
    [/TR]
    [TR]
    [TD]12[/TD]
    [TD]!=[/TD]
    [TD]Not equal to[/TD]
    [TD]x!=y[/TD]
    [TD]Binary[/TD]
    [TD]Relational[/TD]
    [/TR]
    [TR]
    [TD]13[/TD]
    [TD]>[/TD]
    [TD]Greater than[/TD]
    [TD]x>y[/TD]
    [TD]Binary[/TD]
    [TD]Relational[/TD]
    [/TR]
    [TR]
    [TD]14[/TD]
    [TD]<[/TD]
    [TD]Less than[/TD]
    [TD]x<y[/TD]
    [TD]Binary[/TD]
    [TD]Relational[/TD]
    [/TR]
    [TR]
    [TD]15[/TD]
    [TD]>=[/TD]
    [TD]Greater than or equal to[/TD]
    [TD]x>=y[/TD]
    [TD]Binary[/TD]
    [TD]Relational[/TD]
    [/TR]
    [TR]
    [TD]16[/TD]
    [TD]<=[/TD]
    [TD]Less than or equal to[/TD]
    [TD]x<=y[/TD]
    [TD]Binary[/TD]
    [TD]Relational[/TD]
    [/TR]
    [TR]
    [TD][/TD]
    [TD][/TD]
    [TD][/TD]
    [TD][/TD]
    [TD][/TD]
    [TD][/TD]
    [/TR]
    [TR]
    [TD][/TD]
    [TD][/TD]
    [TD][/TD]
    [TD][/TD]
    [TD][/TD]
    [TD][/TD]
    [/TR]
    [TR]
    [TD]17[/TD]
    [TD]![/TD]
    [TD]Logical negation[/TD]
    [TD]!x;[/TD]
    [TD]Unary[/TD]
    [TD]Logical[/TD]
    [/TR]
    [TR]
    [TD]18[/TD]
    [TD]&&[/TD]
    [TD]Logical AND[/TD]
    [TD]x&&y[/TD]
    [TD]Binary[/TD]
    [TD]Logical[/TD]
    [/TR]
    [TR]
    [TD]19[/TD]
    [TD]||[/TD]
    [TD]Logical OR[/TD]
    [TD]x||y[/TD]
    [TD]Binary[/TD]
    [TD]Logical[/TD]
    [/TR]
    [TR]
    [TD][/TD]
    [TD][/TD]
    [TD][/TD]
    [TD][/TD]
    [TD][/TD]
    [TD][/TD]
    [/TR]
    [TR]
    [TD][/TD]
    [TD][/TD]
    [TD][/TD]
    [TD][/TD]
    [TD][/TD]
    [TD][/TD]
    [/TR]
    [TR]
    [TD]20[/TD]
    [TD]~[/TD]
    [TD]Bitwise NOT[/TD]
    [TD]~x;[/TD]
    [TD]Unary[/TD]
    [TD]Bitwise[/TD]
    [/TR]
    [TR]
    [TD]21[/TD]
    [TD]&[/TD]
    [TD]Bitwise AND[/TD]
    [TD]x&y[/TD]
    [TD]Binary[/TD]
    [TD]Bitwise[/TD]
    [/TR]
    [TR]
    [TD]22[/TD]
    [TD]|[/TD]
    [TD]Bitwise OR[/TD]
    [TD]x|y[/TD]
    [TD]Binary[/TD]
    [TD]Bitwise[/TD]
    [/TR]
    [TR]
    [TD]23[/TD]
    [TD]^[/TD]
    [TD]Bitwise XOR[/TD]
    [TD]x^y[/TD]
    [TD]Binary[/TD]
    [TD]Bitwise[/TD]
    [/TR]
    [TR]
    [TD]24[/TD]
    [TD]<<[/TD]
    [TD]Bitwise Left Shift[/TD]
    [TD]x<<y[/TD]
    [TD]Binary[/TD]
    [TD]Bitwise[/TD]
    [/TR]
    [TR]
    [TD]25[/TD]
    [TD]>>[/TD]
    [TD]Bitwise Right Shift[/TD]
    [TD]x>>y[/TD]
    [TD]Binary[/TD]
    [TD]Bitwise[/TD]
    [/TR]
    [/TABLE]




    There are more operators in C-Language, but we shall take a look at them in the comming tutorials.

    Arithmetic operators are self explanatory.
    We shall also see logical and relational operators in comming tutorials as they are easy to understand and wont take much of time.
    Generally teachers avoid or give less time for bit operators.

    So I think I shall give preference to write about operators.
    Bit-wise Operators:
    In computer science, everything boils down to binary.
    In binary system, all we have is 0's and 1's.(we call them as bits).

    Since the operators above are used to manipulate the bits, we call them as bitwise operators.
    These are slightly difficult to understand if you are unaware of the binary system.

    A binary system is a part of number system which uses only two values. 0 and 1.
    1 is called as logical-high and 0 as logical-low.
    In digital electronics, binary system is generally treated as a switch where 0 is OFF and 1 is ON.
    In boolean algebra,0 is treated as false and 1 as true.

    So this much is enough for basic understanding of binary systems.

    Now we shall see how to convert a decimal number into binary.
    The technique we use is called as successive division.
    What we do in this technique is divide the number successively by 2 and note down the remainders.
    Once we get the quotient as 0, we stop and reverse the order of remainders.
    This is the binary representation of the decimal number.
    for example lets take number (19)[SUB]10[/SUB] and convert it to binary.
    ************
    Step 1:
    Divide 19 by 2.
    Quotient : 9
    Remainder : 1
    ************
    Step 2:
    Divide 9 by 2.
    Quotient : 4
    Remainder : 1
    ************
    Step 3:
    Divide 4 by 2.
    Quotient : 2
    Remainder : 0
    ************
    Step 4:
    Divide 2 by 2.
    Quotient : 1
    Remainder : 0
    ************
    Step 5:
    Divide 1 by 2.
    Quotient : 0
    Remainder : 1
    ************
    Now we reverse the order of the remainders. i.e, we write the remainders from last step to first step.
    so the binary representation of (19)[SUB]10[/SUB] is (10011)[SUB]2[/SUB].

    You can try for as many numbers you like.

    Now we shall see how to convert a binary number to a decimal number:
    Let us try to convert the number from (10011)[SUB]2[/SUB] to decimal.
    Binary number is something which is always expressed in powers of 2.
    the right extreme of the number has place value 0(LSB:Least Significant Bit) and is incremented by 1 everytime as we proceed from right to left,with rightmost bit having highest place value(MSB : Most Significant Bit).

    We get the decimal number by adding all the bits,multiplied by 2[SUP]place value

    [/SUP]So the place value of 1 in 10011 is 0.

    so we start like this: 1*2[SUP]4[/SUP]+0*2[SUP]3[/SUP]+0*2[SUP]2[/SUP]+1*2[SUP]1[/SUP]+1*2[SUP]0
    [/SUP]=1*16 + 0*8 + 0*4 + 2*1 + 1*1
    =16+2+1
    =19
    Hence we have learnt a method to convert a number to binary and from binary back to decimal.
    Be thorough in this part, we are going to use it in our next tutorial, where we would actually learn how to manipulate bits.
    Its very interesting and I shall also try to explain, in computer science, why we would always prefer bit-operations on numbers where the numbers are large.
    *******************************************************************************************
    Exercises:
    1)Convert as many numbers as you can from decimal to binary.
    2)Repeat Q1 for binary to decimal.
    3)Findout the relation between number of bits and the maximum number that can be represented by those bits.
    4)How many bits will I require to represent (200000)[SUB]10[/SUB] in binary?
    5)Maximum value that can be represented in each data-type we have studied in previous tutorial (V.Important).
    *******************************************************************************************
    I will conclude this tutorial here,We shall see how to manipulate the bits and to carry simple operations using bitwise operators in our next tutorial.
    Until then take care and practice.
    Ciao.
     
    #11
  12. simplycoder

    Engineering Discipline:
    Electronics & Communications
    Re: Beginners Tutorial : The C-language:: Tutorial-6

    Hello everyone again this is simplycoder and welcome to the 6[SUP]th[/SUP] tutorial of C-programing.
    In this tutorial, we are going to learn further about the bitwise operators NOT,AND,OR.

    In the previous tutorial, we have seen how to convert a decimal number to binary and vice-versa.
    In this tutorial, we are going to learn how to operate on the bits using bit-wise operators.

    There is one more thing about binary system, which we havent seen in our previous tutorial and that is how to represent negative numbers in binary form.
    The answer to this is something called as 2's complement.
    Previously there was a method called as 1's complement,but I leave this exercise to readers as to why 1's complement was disapproved.

    Since this is a C-programing course, I think I should limit myself only to the introduction of 2's complement and how to calculate 2's complement of any number.

    We shall directly start with an example.
    let us assume that we are dealing with signed numbers. lets take (9)[SUB]10[/SUB] as our example and limit ourself till 8-bit representation.
    In signed numbers, the rightmost bit is used to indicate the sign of the number. (0 for positive number and 1 for negative)
    (9)[SUB]10[/SUB] in binary is written as 0000 1001. (See the rightmost bit is 0, hence the number is positive.)

    Step 1:
    To take one's complement, just invert all the bits.
    so 1's complement of (9)[SUB]10[/SUB] = 1111 0110.
    Since this is signed number and the rightmost bit 1, inorder to find the decimal representation, all we have to do is multiply the rightmost bit by its place value taking the sign into consideration.
    so 1*(-2[SUP]7[/SUP])+1*2[SUP]6[/SUP]+1*2[SUP]5[/SUP]+1*2[SUP]4[/SUP]+0*2[SUP]3[/SUP]+1*2[SUP]2[/SUP]+1*2[SUP]1[/SUP]+0*2[SUP]0[/SUP]
    = -128+64+32+16+4+2
    = -10

    Step 2:
    Now add one to this number to get 2's complement: -10+1=-9
    This is how we represent the negative numbers in binary.

    Before we can actually write programs, let us first know the advantages and disadvantages of bit-wise operation
    Advantages:
    Bit-wise operations are faster than the normal operations(By this, I mean they are really very very fast).
    It gives us an insight on how simple things get computed in a computer. eg. Addition of two numbers takes place using bit-wise operators and not by '+' in lower computing or assembly. Similar fashion is in C. The '+' operator is broken down finally to its bit-operators while the things are computed. In short a C-compiler, doesnot understand anything except for bit-wise operators and binary numbers.

    Disadvantages: They seem to be complicated,and a programmer must spend some time on bit-operators before he can master them.

    Applications:The main areas of applications are in embedded systems and in microprocessor-microcontrollers, typically when either the input is large or space is small.

    All the bitwise operators work using their respective truth-tables.

    What is a truth-table?
    A truth table is a form of mathematical table which is generally related to boolean alegra.
    We shall come to know about it more when we discuss it.

    Before going through our first bit-operator:: Bit-wise NOT, I encourage readers to read more on 1's and 2's complements and how to represent negative numbers in binary.


    1)Bit-wise NOT :: ~
    [TABLE="class: grid, width: 25%"]
    [TR]
    [TD]Truth-Table of Bit-wise NOT : : ~[/TD]
    [/TR]
    [TR]
    [TD]Input[/TD]
    [TD]Output[/TD]
    [/TR]
    [TR]
    [TD]0
    [/TD]
    [TD]1[/TD]
    [/TR]
    [TR]
    [TD]1[/TD]
    [TD]0[/TD]
    [/TR]
    [/TABLE]


    The bit-wise NOT is used when we have to reverse the bits of the number.
    Interestingly, we have ~x== (-x-1).
    Lets take the following program.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    /*Author : simplycoder*/
     
    int main(int argc, char *argv[])
    {
      int x=9;
      int y=~x;
      printf("Original number %d on bit negation is %d\n\n\n",x,y);
    
       system("PAUSE");    
       return 0;
    }
    
    The above explaination of 1's complement of 9 is enough to understand the above code.

    2)Bit-wise AND :: &
    Our second bit-wise operator is &.
    Truth-table for bitwise-AND is
    [TABLE="class: grid, width: 20%"]
    [TR]
    [TD="colspan: 3"]Truth-Table for bit-wise AND.
    [/TD]
    [/TR]
    [TR]
    [TD="colspan: 2"]Inputs
    [/TD]
    [TD]Output
    [/TD]
    [/TR]
    [TR]
    [TD]A
    [/TD]
    [TD]B
    [/TD]
    [TD]Z
    [/TD]
    [/TR]
    [TR]
    [TD]0
    [/TD]
    [TD]0
    [/TD]
    [TD]0
    [/TD]
    [/TR]
    [TR]
    [TD]0
    [/TD]
    [TD]1
    [/TD]
    [TD]0
    [/TD]
    [/TR]
    [TR]
    [TD]1
    [/TD]
    [TD]0
    [/TD]
    [TD]0
    [/TD]
    [/TR]
    [TR]
    [TD]1
    [/TD]
    [TD]1
    [/TD]
    [TD]1
    [/TD]
    [/TR]
    [/TABLE]



    In short whenever all the inputs are 1,the output is 1, else its 0 (Simple).
    Let us write a program for the same and then discuss the output.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    /*Author : simplycoder*/
     
    int main(int argc, char *argv[])
    {
       int a=2;
       int b=3;
       printf("a = %d b = %d a&b = %d\n\n\n",a,b,(a&b));
    
       system("PAUSE");    
       return 0;
    }
    
    
    OK. Now let us see how does the following program executes.
    a = 0000 0010
    b = 0000 0011

    Now compare the bits on their equal place values, if both of them are 1, then the result is 1 else its 0.
    so we get
    0000 0010
    & 0000 0011
    -------------
    0000 0010 =(2)[SUB]10[/SUB]

    Now we shall see another bitwise operator, its called as Bitwise-OR
    3)Bitwise-OR :: |

    [TABLE="width: 25%"]
    [TR]
    [TD="colspan: 3"]Truth-Table for bit-wise AND.
    [/TD]
    [/TR]
    [TR]
    [TD="colspan: 2"]Inputs
    [/TD]
    [TD]Output
    [/TD]
    [/TR]
    [TR]
    [TD]A
    [/TD]
    [TD]B
    [/TD]
    [TD]Z
    [/TD]
    [/TR]
    [TR]
    [TD]0
    [/TD]
    [TD]0
    [/TD]
    [TD]0
    [/TD]
    [/TR]
    [TR]
    [TD]0
    [/TD]
    [TD]1
    [/TD]
    [TD]1
    [/TD]
    [/TR]
    [TR]
    [TD]1
    [/TD]
    [TD]0
    [/TD]
    [TD]1
    [/TD]
    [/TR]
    [TR]
    [TD]1
    [/TD]
    [TD]1
    [/TD]
    [TD]1
    [/TD]
    [/TR]
    [/TABLE]



    This truth-table can be interpreted as: If anyone of the input is 1,the resulting bit is 1 else 0.


    Let us write a program for the same and then discuss the output.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    /*Author : simplycoder*/
     
    int main(int argc, char *argv[])
    {
       int a=2;
       int b=3;
       printf("a = %d b = %d a|b = %d\n\n\n",a,b,(a|b));
    
       system("PAUSE");    
       return 0;
    }
    
    
    OK. Now let us see how does the following program executes.
    a = 0000 0010
    b = 0000 0011

    Now compare the bits on their equal place values, if both of them are 1, then the result is 1 else its 0.
    so we get
    0000 0010
    | 0000 0011
    -------------
    0000 0011 =(3)[SUB]10[/SUB]

    Ok then since this tutorial is going on to be lengthy, I shall wrap up the first part of bit operators here now.
    ********************************************************************
    Exercises:
    Findout the maximum number that can be stored in your system using only the bit operators.
    Argue if only ones complement is enough to represent negative numbers.Reason it out.
    Play around with the new operators learnt. Ofcourse till you get bored of them.
    ********************************************************************
    In the next tutorial, I shall post on the remaining three bit operators and hopefully we shall be able to carryout our basic arithmetic operations using only bit operator.
    Kindly give your feedbacks on the tutorial through a PM or by a visitor message.
    Until the next time, take care
    Ciao.
     
    #12
  13. simplycoder

    Engineering Discipline:
    Electronics & Communications
    Re: Beginners Tutorial : The C-language:: Tutorial-7

    Tutorial-7


    Hello everyone again this is simplycoder and welcome to the 7[SUP]th[/SUP] tutorial of C-programing.
    In this tutorial, we are going to learn further about the bitwise operators Exclusive-OR, Right-Shift,Left-Shift.

    In the last tutorial, we had basic information on bit-wise operators.
    Now in this tutorial, we are going to learn about remaining 3 bit-wise operators listed above.

    We shall start with our next biwise operator

    4)Exclusive-OR(XOR):: ^
    This too follows truth table as in AND and OR.
    The truth table is given as follows.
    [TABLE="class: grid, width: 20%"]
    [TR]
    [TD="colspan: 3"]Truth-Table for XOR
    [/TD]
    [/TR]
    [TR]
    [TD="colspan: 2"]Input
    [/TD]
    [TD]Output
    [/TD]
    [/TR]
    [TR]
    [TD]A
    [/TD]
    [TD]B
    [/TD]
    [TD]Z
    [/TD]
    [/TR]
    [TR]
    [TD]0
    [/TD]
    [TD]0
    [/TD]
    [TD]0
    [/TD]
    [/TR]
    [TR]
    [TD]0
    [/TD]
    [TD]1
    [/TD]
    [TD]1
    [/TD]
    [/TR]
    [TR]
    [TD]1
    [/TD]
    [TD]0
    [/TD]
    [TD]1
    [/TD]
    [/TR]
    [TR]
    [TD]1
    [/TD]
    [TD]1
    [/TD]
    [TD]0
    [/TD]
    [/TR]
    [/TABLE]

    Truth-Table for X-OR follows the same way as that of OR.
    However, when all inputs are 1, the resulting output is 0
    .
    Lets now write a program and discuss how it works.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    /*Author : simplycoder*/
     
    int main(int argc, char *argv[])
    {
      int a=2;
      int b=3;
      printf("a = %d b = %d a^b = %d\n\n\n",a,b,(a^b));
    
       system("PAUSE");    
       return 0;
    }
    
    a = 2 and in binary, 2 is 0000 0010.
    b = 3 and in binary, 3 is 0000 0011.

    Now compare the bits on their equal place values,
    • if both of them are 1, then the result is 0
    • if both of them are 0 then the result is 0,else its 0.
    • else result is 1.
    so we get
    0000 0010
    ^ 0000 0011
    -------------
    0000 0001 =(1)[SUB]10[/SUB]

    Now we are going to see a bit operator, this is called as Left-Shift operator.

    5)Left shift operator:: <<
    This operator has no truth-table.
    All it does is, it shifts the bits to left, by padding zeroes on right.
    Syntax : <<.
    Lets write a program and check how it works.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    /*Author : simplycoder*/
     
    int main(int argc, char *argv[])
    {
       int a=2;
       int b=1;
       printf("a = %d b = %d a<<b = %d\n\n\n",a,b,(a<<b));
    
       system("PAUSE");    
       return 0;
    }
    
    
    a=2.
    therefore in binary : a = 0000 0010
    b= 1.(We need not know the bit details about this number in shifting.).
    We can say that in the above code, a is left-shifted by b bits.
    so 2 is left-shifted by 1 bit.
    After shifting, a = 0000 0100(note the position of 1 in both the cases.)
    so in decimal format, a=4.

    In similar cases, we have the right shift operator.

    6)The right-shift operator :: >>
    This operator has no truth-table.
    All it does is, it shifts the bits to right, by padding zeroes on left.
    Syntax : >>.
    Lets write a program and check how it works.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    /*Author : simplycoder*/
     
    int main(int argc, char *argv[])
    {
       int a=2;
       int b=1;
       printf("a = %d b = %d a>>b = %d\n\n\n",a,b,(a>>b));
    
       system("PAUSE");    
       return 0;
    }
    
    
    a=2.
    therefore in binary : a = 0000 0010
    b= 1.(We need not know the bit details about this number in shifting.).
    We can say that in the above code, a is right-shifted by b bits.
    so 2 is right-shifted by 1 bit.
    After shifting, a = 0000 0001(note the position of 1 in both the cases.)
    so in decimal format, a=1.

    Ok I think this much is enough for the basic introduction to Bit-Operators and Binary numbers.
    I shall conclude this tutorial here.
    ********************************************************************************
    Execrises:
    1) Assume a=5 and b=10. Swap them by using a bitwise operator and without use of third variable.
    2) Explain in laymans terms, what does right-shift and left-shift operators do?
    3) WAP to find whether the pattern (111)[SUB]2[/SUB] exists in all numbers that can be represented by 4 bits.
    4)Practice.
    ********************************************************************************
    So in this tutorial, we have learnt about bit-operators.
    In the next tutorial, we shall see relational operators.
    I tried to make this tutorial as clear as possible and hopefully you to have liked it.
    Your feedback is important so kindly let me know about your views.
    Until the next time, take care.
    Ciao.
     
    #13
  14. ukanth19

    ukanth19 Apprentice

    Engineering Discipline:
    Electrical
    Very useful....keep on going :)
     
    #14
  15. durga

    durga Star

    Engineering Discipline:
    Communications
    :),

    Simplycoder, are you alright if this thread is moved to tutorials section and made a 'sticky'?
     
    #15
  16. simplycoder

    Engineering Discipline:
    Electronics & Communications
    Re: Beginners Tutorial : The C-language:: Tutorial-8

    Tutorial-8
    Hello everyone again this is simplycoder and welcome to the 8[SUP]th[/SUP] tutorial of C-programing.
    In this tutorial, we are going to learn further about the relational operators.

    Relational operators are also called as Comparison Operators.
    There are six types of relational operators in C. Now we shall see them one by one.

    1)Equal to :: ==
    This operator check whether both the operands are equal or not.
    It will return true if the the operators are equal else it shall return false.
    In C all the conditions which are true, are returned with 1 and all the conditions which are false are returned by 0

    Lets check a program on the same.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    /*Author : simplycoder*/
     
    int main(int argc, char *argv[])
    {
       int a=5;
       int b=6;
       int c=5;
       
       printf("a == b , %d\n\n\n",a==b);
       printf("b == c , %d\n\n\n",b==c);
       printf("c == a , %d\n\n\n",c==a);
       
       system("PAUSE");    
       return 0;
    }
    
    This program, basically checks if the operators are equal.
    the first statement is a==b this is false, hence the output will be 0.
    the second statement is b==c this is also false, hence the output will be 0 in this case as well.
    in the third case, c==a this is true and hence the output will be 1.

    Now lets take a look at another relational operator called as not equal to.

    2) Not Equal to :: !=
    This operator works similar to that of '==' operator, however it returns true (1) if they are not equal and false (0) if they are equal.
    We shall write a program here.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    /*Author : simplycoder*/
     
    int main(int argc, char *argv[])
    {
       int a=5;
       int b=6;
       int c=5;
       
       printf("a != b , %d\n\n\n",a!=b);
       printf("b != c , %d\n\n\n",b!=c);
       printf("c != a , %d\n\n\n",c!=a);
       
       system("PAUSE");    
       return 0;
    }
    
    The code is now self explanatory and I leave the output as an exercise to the readers.

    3) Greater than :: >
    The above operator is which test whether the first operator is greater than second.
    I will return true (1) if the first operator is greater than second operator, else it will return false.
    A sample code for this will be
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    /*Author : simplycoder*/
     
    int main(int argc, char *argv[])
    {
       int a=5;
       int b=6;
       int c=5;
       
       printf("a > b , %d\n\n\n",a>b);
       printf("b > c , %d\n\n\n",b>c);
       printf("c > a , %d\n\n\n",c>a);
       
       system("PAUSE");    
       return 0;
    }
    The working is self explanatory.

    4) Less than operator :: <
    This is similar to Greater than operator, only difference is that it will return true if the first operand is less than second else it will return false.
    This is a good exercise for readers to write their own code or modify the above code to know more on this operator.

    5)Greater than or Equal to :: >=
    This operator will check for the condition where the first operator is either equal or greater than the first operator.
    It will return true if the above condition holds, else shall return false.
    Lets write a code for this.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    /*Author : simplycoder*/
     
    int main(int argc, char *argv[])
    {
       int a=5;
       int b=6;
       int c=5;
       
       printf("a >= b , %d\n\n\n",a>=b);
       printf("b >= c , %d\n\n\n",b>=c);
       printf("c >= a , %d\n\n\n",c>=a);
       
       system("PAUSE");    
       return 0;
    }
    In this case, the first statement will test for a to be greater than or equal to b, but this is false, hence it will return 0.
    In the second statement, will test for b to be greater than ore equal to c, this is true as b is greater than a.
    In the third statement, it will test c to be greater than or equal to a, this is also true as c is equal to a.

    6) Less than or equal to :: <=
    This operator is similar to above greater than or equal to operator.
    However it will return true if the first operand is less than or equal to the second operator.
    Working is self explanatory.

    This is it about relational operators.
    We shall see further use of these operators when we shall use control structures.
    I shall conculde my tutorial now.
    ***********************************************************************
    Exercise:
    1)Play with relational operators.
    2)Practice.
    ***********************************************************************
    In the next tutorial, I shall explain the use of logical operators.
    Until the next time, take care.
    Ciao.
     
    #16
  17. simplycoder

    Engineering Discipline:
    Electronics & Communications
    Re: Beginners Tutorial : The C-language:: Tutorial-9

    Tutorial-9

    Hello everyone again this is simplycoder and welcome to the 9[SUP]th[/SUP] tutorial of C-programing.
    In this tutorial, we are going to learn further about the logical operators.
    There are only three logical operators in C.

    1) Logical NOT :: !
    This operator is called as Logical NOT as it will return an opposite value.
    This means, it will return false if the statement is true and it will return true is the statement is false.
    Above statement is equivalent to :: It will return 0 if value is not 0 and it will return 1 if value is 0.(Simple).
    Please donot get confused with the bit-wise NOT, this operator has nothing to do with bits.

    Let us write a program to understand things more clearly.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    /*Author : simplycoder*/
     
    int main(int argc, char *argv[])
    {
       int a = 20;
       int b = 0;
       printf("%d\t%d\n\n\n\n",!a,!b);
       
       system("PAUSE");    
       return 0;
    }
    
    Here a=20, this means that a not equal to 0, so !a will return 0.
    In the next statement, b = 0, this means that b is equal to 0 and hence !b will return 1.

    2) Logical AND :: &&
    This operator test whether all conditions hold true.
    It can be read as: Logical-AND will return true(1) if and only if both the conditions are true, else will return false(0).

    Things will be clear now when we write our next program.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    /*Author : simplycoder*/
     
    int main(int argc, char *argv[])
    {
       int a = 20;
       int b = 0;
       printf("%d\n\n\n\n",((a==20)&&(b==0)));
       printf("%d\n\n\n\n",((a==21)&&(b==0)));
       system("PAUSE");    
       return 0;
    }
    
    The first statement is ((a==20)&&(b==0)), this can be split in two sub statements as:
    1...."a is equal to 20"..... TRUE.
    2...."b is equal to 0".....TRUE.

    Now check the stament which describes Logical AND.
    Since both the statements are true, it will return true(1).

    Check for the second statement here, in this case, a equal to 21 is false, hence no matter what the second statement is, the overall statement is going to return false(0).
    This is called as SHORT-CIRCUIT behaviour of the Logical AND.


    3)Logical OR :: ||
    This logical operator will return true if any one of the statement is true. If all statements are false, then and then only, it will return false.
    To make things clear, we shall write a small program now,
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    /*Author : simplycoder*/
     
    int main(int argc, char *argv[])
    {
       int a = 20;
       int b = 0;
       printf("%d\n\n\n\n",((a==20)||(b==0)));
       printf("%d\n\n\n\n",((a==20)||(b==1)));
       printf("%d\n\n\n\n",((a==21)||(b==1)));
       system("PAUSE");    
       return 0;
    }
    
    
    In the first stament, a equal to 20, so now the overall statment will return true(1).(This is called as the SHORT-CIRCUIT nature of the Logical OR).
    In the second statment, a equal to 20 true the overall statment will return true, it wouldnt even evaluate the other statements, in this case the second part of the statement is false, but by defination, entire statment will return true as it encounterd the first statement to be true(1).
    In the third statment, a equal to 21 is false, so it will proceed to check the next statement, b equal to 1 this is also false, hence the over all statment will be false(0).

    There is not much in logical operators on their own, however, a basic understanding is required as we will use them on regular basis when we go for control structures.
    I shall conclude my tutorial here.
    ********************************************************************************
    Exercises:
    1)Play with Logical operators,develop strong understanding.
    2)Practice.
    ********************************************************************************
    In the next tutorial we shall finally begin with control structures, where we will allow the computer to make decisions on our behalf..
    This is really important concept and very interesting indeed.
    Until the next time, take care.
    Ciao.
     
    #17
  18. simplycoder

    Engineering Discipline:
    Electronics & Communications
    Re: Beginners Tutorial : The C-language:: Tutorial-10

    Tutorial-10
    Hello everyone again this is simplycoder and welcome to the 10[SUP]th[/SUP] tutorial of C-programing.
    In this tutorial, we are going to learn further about the Control Structures.
    This tutorial can be treated as part-1 as here we shall only see the decision making control structures.

    Until now, we have seen programs which have linear execution of the statements, but then its not enough in day to day life programing.
    We might want to code a program which takes decisions, which can repeat set of instructions, which can bifurcate from the code and so on...
    For this purpose C offers us set of keywords which allows us to tell the compiler what we want from the code and how to behave.
    These set of keywords are called as Control-Structures.

    OK, enough of introduction here, now we shall see the first control structure:

    1) if :- This is the decision maker in C. It can interpreted in similar way as it is done in English.
    Basically what it does is checks whether the code within the if(...) is true,if its true, then it executes the statement below it, if it is false, then it won't execute. Very simple.
    Syntax
    Code:
    if (conditional)
        {
                block of statements executed if conditional is true;
        }
    ....
    rest of the code.
        
    consider this code.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    /*Author : simplycoder*/
     
    int main(int argc, char *argv[])
    {
       int a = 20;
       int b = 0;
       
       if(a==20)
       printf("Value of a = 20\n");
       
       if(a==21)
       printf("Value of a = 21\n");
      
      
       system("PAUSE");    
       return 0;
    }
    
    
    Check the first if statement, the condition holds true and hence the statement below it will be executed.
    Now take a look at the second if statement, the second if statement is false and hence the condition below it wouldnot be executed.

    2)if .... else
    This is similar to if , here the program has to execute the decision if the statement is true or it will execute the statement below else.
    This will be clear once we write a program.
    Code:
    Syntax
        if (conditional)
        {
                block of statements executed if conditional is true;
        }
        else
       {
                block of statements if condition false;
        }
    consider the following program.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    /*Author : simplycoder*/
     
    int main(int argc, char *argv[])
    {
       int a = 20;
       int b = 0;
       
       if(a==20)
       printf("Value of a = 20\n");
       else 
       printf("Value of a != 20\n");
       
       if(a==21)
       printf("Value of a = 21\n");
       else 
       printf("Value of a != 21\n");
      
       system("PAUSE");    
       return 0;
    }
    
    
    The first if statement if(a==20) holds true, hence it executes the statement below if and skips the statement below else.
    However the second if statement if(a==21) is false hence it skips the statement below if and executes the statement below else.

    3) switch(expression) :
    The switch and case statements help control complex conditional and branching operations.
    The switch statement transfers control to a statement within its body.
    They can be treated as a better option for conventional if or if...else

    Syntax:
    Code:
    switch (expression)
      {
         case item-number:
                                   statements;
         break;
         case item-number:
                                    statements;
         break;
         case item-number:
                                    statements;
         break;
         default: 
                                    statement;
        break;
      }
    
    the item-numbers should be compatible with an integer or a character, however cannot be a variable.
    The execution is simple: Consider this statement,switch (expression), the term expression here denotes the case item-number.
    If the expression is a number A, then the case item-number equal to A will be executed(Simple).

    Now at first look, this will look a bit complicated, but need not worry, things will get clear as we write our simple program.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    /*Author : simplycoder*/
     /*Task :  To find if the number is ODD or EVEN*/
    int main(int argc, char *argv[])
    {
     
     int a;
     printf("Enter the value of number :: ");
     scanf("%d",&a);
     int check_for_even=2;
     switch(a%check_for_even)
      {
         case 0: printf("\nEVEN\n");
         break;
      
         case 1:printf("\nODD\n"); 
         break;
         
         default:printf("\nNEITHER ODD NOR EVEN\n");
         break;                       
      }  
     
      
       system("PAUSE");    
       return 0;
    }
    
    
    Assume that in the above program, the user entered 10.
    10%2==0 hence the expression will contain 0, the program will execute the case which has case-number 0.
    Similarly,if the number entered is 11, then 11%2==1, the program will execute the case which has cas-number 1.
    The case number is very important as the program will match the expression with the case number.
    The default here is redundant as any number is either odd or even, so default here will never be executed.
    The general understanding of default is that it will execute if none of the cases match the expression.

    This is it for this tutorial, we now know how about the decision making control-structures in C.
    Please understand any statements included in '{}', then the compiler will treat all of them as one combined statement.
    I shall conclude my tutorial here.
    *****************************************************************************
    Exercises:
    1)WAP to find whether the number is ODD or EVEN using if,if...else statements.
    2)WAP to find if the enterd character was 'a' using switch, if, if...else statement.
    3)WAP to print the maximum and minimum number of any three numbers.
    4)WAP to print the 4[SUP]th[/SUP] bit of the decimal 10 is 1 or 0.
    5)Practice a lot, now we are entering the heart of programing, this is valid for any language.
    A good grasp now, will help your entire programing life.
    *****************************************************************************
    In the next tutorial, we shall see the looping-control-structures.
    Until the next time,take care
    Caio.
     
    #18
  19. simplycoder

    Engineering Discipline:
    Electronics & Communications
    Re: Beginners Tutorial : The C-language:: Tutorial-11

    Hello everyone again this is simplycoder and welcome to the 11[SUP]th[/SUP] tutorial of C-programing.
    In this tutorial, we are going to learn further about the remaining Control Structures.
    This tutorial can be treated as part-2 as here we shall see the iterative control structures or looping control strucutres.

    1)for

    Syntax: for(initialization ; condition of termination ; increment or decrement)
    Donot forget those semi-colons :: ;

    for loop consists of three parts
    • initialization.
    • condition of termination.
    • increment or decrement.
    Initialization:This is the first part in writing the for-loop structure.Here the loop is initialized to a desired value, the for-loop begins its execution from this value.

    Condition of termination: This is the condition of termination, in most of the times, terminating condition is a relational operator. The for loop exits or terminates once the condition returns a false value.

    increment or decrement: Based on the need of for loop, increment or decrement is applied.This puts a limit on number of interations and helps the loop to terminate the loop after those many number of iterations.

    Basically, a for loop will repeat the same thing for the specified number of iterations. (and hence it is called as LOOP).
    Things will be simpler when we write our first program using for loop.
    Take a look at following code:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    /*Author : simplycoder*/
     
    int main(int argc, char *argv[])
    {
     int i;
     
     for(i=0;i<5;i++)
     printf("Hello\n");
     
      
       system("PAUSE");    
       return 0;
    }
    
    
    Here the variable i has been initialized to 0(i=0), the terminating condition is when x<5 returns false, and a post-increment operator is applied for increment in value of i.
    so what this loop does?
    The answer is, it prints Hello.
    for how many iterations does it print hello?
    This is what we are going to calculate now.Let us execute the program by hand.

    Interation 1: i=0, is i less than 5(true) execute statement below for loop. (Print hello), increment i(now the value of i is 1)
    Interation 2: i=1, is i less than 5(true) execute statement below for loop. (Print hello), increment i(now the value of i is 2)
    Interation 3: i=2, is i less than 5(true) execute statement below for loop. (Print hello), increment i(now the value of i is 3)
    Interation 4: i=3, is i less than 5(true) execute statement below for loop. (Print hello), increment i(now the value of i is 4)
    Interation 5: i=4, is i less than 5(true) execute statement below for loop. (Print hello), increment i(now the value of i is 5)
    Interation 6: i=5, is i less than 5(false) donot execute statement below for loop, terminate from the loop.

    Hence from the above analysis,the program will print Hello 5 times and when i<5 will return false, it will terminate.

    At first look things look difficult, so don't worry, later on eventually you will get hang of it, once we write programs using loops and other control structures.

    2)while:
    Syntax:
    while(condition terminates)

    This is called as while loop.
    It is simple and can be read as while the loop terminates, do something(this is obvious).

    Take a look at the program here,
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    /*Author : simplycoder*/
     
    int main(int argc, char *argv[])
    {
     int i=0;
     
     while(i<5)
     {
        printf("Hello\n");          
        i++;
     }
     
      
       system("PAUSE");    
       return 0;
    }
    
    
    Here i is initialized to 0 (int i=0), the terminating condition is when i<5 will return false.
    So let us execute the statements by hand:
    Interation 1: i=0, is i less than 5(true) execute statement below while loop. (Print hello) and increment i(now the value of i is 1).
    Interation 2: i=1, is i less than 5(true) execute statement below while loop. (Print hello) and increment i(now the value of i is 2).
    Interation 3: i=2, is i less than 5(true) execute statement below while loop. (Print hello) and increment i(now the value of i is 3).
    Interation 4: i=3, is i less than 5(true) execute statement below while loop. (Print hello) and increment i(now the value of i is 4).
    Interation 5: i=4, is i less than 5(true) execute statement below while loop. (Print hello) and increment i(now the value of i is 5).
    Interation 6: i=5, is i less than 5(false) donot execute statement below while loop, terminate the loop.

    Here it can be seen that '{}' are used to tell compiler, to interpret printf("Hello\n") and i++ as single block of statement.

    3) do... while(condition);
    Syntax:
    Code:
     do
    {
    //code here
    }
    while(condition is true);
    
    Donot forget the semi-colon after while.

    There is a subtle difference between do..while and while, which will be illustrated later as we see.
    Take a look at the code:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    /*Author : simplycoder*/
     
    int main(int argc, char *argv[])
    {
     int i=0;
     
     do
     {
        printf("Hello\n");          
        i++;
     }
     while(i<5);
      
       system("PAUSE");    
       return 0;
    }
    
    
    the 'do' part will print hello for the first time without taking the terminating condition into consideration.
    the while part will put restrictions on do from the second iteration.

    Iteration 1:
    do :: print Hello,increment i(value of i=1)
    while :: i<5(true)
    Iteration 2:
    do :: print Hello,increment i(value of i=2)
    while :: i<5(true)
    Iteration 3:
    do :: print Hello,increment i(value of i=3)
    while :: i<5(true)
    Iteration 4:
    do :: print Hello,increment i(value of i=4)
    while :: i<5(true)
    Iteration 5:
    do :: print Hello,increment i(value of i=5)
    while :: i<5(false), now terminate from the loop.

    So as you can see, a do...while loop executes first and then checks for the condition,whereas a while loop will check for the condition first and then execute.
    Thus it can be said that a do..while loop will execute atleast one time,whereas while maynot execute at all.

    Here I shall conclude with this part of my tutorial.
    ******************************************************************************
    Exercises:
    1)WAP to print numbers from 1 to n, where the input is take from user, using for,while and do...while looping structures.
    2)Repeat 1 to print numbers from n to 1.
    3)WAP to find sum of 1 to n integers where the input is take from user, using for,while and do...while looping structures(DONOT use formula, however verify using the formula).
    4)WAP to find the sum of series 1+4+9+16+...n[SUP]2[/SUP] where the input is take from user, using for,while and do...while looping structures(DONOT use formula, however verify using the formula).
    5)WAP to find the sum of series 1[SUP]3[/SUP]+2[SUP]3[/SUP]+3[SUP]3[/SUP]+4[SUP]3[/SUP]+...n[SUP]3[/SUP] where the input is take from user, using for,while and do...while looping structures(DONOT use formula, however verify using the formula).
    6)WAP to find the factorial of number given by user.
    7)WAP to find fibonacci series for n numbers where n is taken from user, refer this article for more information on fibonacci numbers Fibonacci Number -- from Wolfram MathWorld
    8)WAP to reverse a number.
    9)Program to print A for simple investment equation : A=P(1+r)[SUP]n[/SUP] where all inputs are given by user.
    10)Develop a condition where a do...while loop executes atleast once whereas while loop doesn't execute at all. WAP to support your answer.
    11)Practice.

    These exercises are very important and are there for a purpose, so if you are interested to be a good programer, the start writing the code on your own.
    If you are not able to get it correctly,you can do one of the following, ask in Discussions, PM me, VM me, I shall try to help.



    ******************************************************************************
    In the next tutorial, we shall something called as nested loops and how to use decision making control structures along with iterative control structures.
    Until the next time, take care.
    Caio.
     
    #19
  20. simplycoder

    Engineering Discipline:
    Electronics & Communications
    Re: Beginners Tutorial : The C-language:: Tutorial-12

    Hello everyone again this is simplycoder and welcome to the 12[SUP]th[/SUP] tutorial of C-programing.
    In this tutorial, we are going to learn further about the remaining Control Structures.
    This can be treated as part-3 and considered as extension of previous tutorial.

    In this tutorial, we will try to accomodate the decision making control-structures and the iterative control-structures.This is going to be the first tutorial in the series where we are not going to study any new thing, not even syntax, but we are going to apply whatever knowledge we have from our previous tutorial.

    This can be marked as a check point, you should be able to grasp the contents of all the previous tutorials before proceeding any further else it will be point less and it maynot make any sense or worse shall confuse you even more.

    Enough of introduction, now lets jump to the structure of the snippet.
    Code:
    control structure 1
    {
       control structure 2
         {
            control structure 3...
              {
                    ..............
               }
          }
    }
    
    As you can see control structure 3 is embedded in control structure 2. control structure 2 is embedded in control structure 1, this embedding is called as nesting.
    so if a for loop is embedded in another for loop, then it is called as nested-for loop, when if is embedded in another if, its called as nested ifs, and so on.
    It is very simple.

    Another important concept is how do nested control structures work, this can be explained with example of nested for loops as follows:
    Code:
    for(initialization of variable 1;terminating condition of this for-loop;increment/decrement)
    {
      for(initialization of variable 2;terminating condition of this for-loop;increment/decrement)
          { 
                  ....
          } 
     }
    Now in this case, the first for loop will initialize, test for the condition, if the condition is true, then it will execute the statement below it.(As regular for-loop).
    But in above pseudo-code, we find another for loop embedded here, this will also run as regular for loop, so there will be initialization of variable 2,terminating condition, it will execute the statements below it, and now increment the value of variable 2. This will run till the termination condition of second for loop returns false.
    Now is the most important part, it is now that the value of variable 1 will increment/decrement.
    Same steps will follow, again the variable 2 will be initialized and again the loop will be carried out as before.
    This will take place till the termination of first loop takes place.

    Saying this, I have tried to explain the most difficult part of the control structures, if you have understood this, then there is no stopping from writing some real cool programs.

    I think, now we are in a position to write some cool programs.

    The first program that we are going to write is to print the even numbers from 1-50.(I know this is not kind of cool, but then this will surely make you understand the power of for-loop.)

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    /*Author : simplycoder*/
    /*Task :    Print even numbers from 0 to 50*/ 
    int main(int argc, char *argv[])
    {
      //Variables.
      int index=0;//Index for the loop.
      int numbers=51;//Total numbers.
      int divide_by_two=2;// Divisor.
      int zero=0;//Value of zero.
      
      //Logic.
      for(index=zero;index<numbers;index++)
      {
         if(index%divide_by_two==zero)
          {
            printf("%d\n",index);
          }
      }
     
       
       system("PAUSE");    
       return 0;
    }
    
    Check the logic, its simple, in english if I were to describe the code, I would just say, Go through all the numbers from 0 to 50 and check if the number is divisble by 2, if it is, then print, if it is not divisible by 2, then go to the next number. This is exactly what the above code does.
    The for loop will help in browsing the numbers from 0 to 50, and the if statement will help to find whether the number given by for loop is divisible by 2 or not.
    So the exact working is summarized as follows:
    Step 1: Take a number from for-loop
    Step 2: Decide if it is divisible by two
    Step 3: If it is divisible by two, then print it.
    Step 4: Take the next number from the for-loop.

    Now as to understand the power for loop, this loop has minimized writing 51 numbers by hand, so instead of using 51 variables,checking and printing the variables 51 times(considering the numbers are even ), all we have to do is write a single for loop and i
    ********************************************************************************************
    Exercises:
    1)WAP to find the odd numbers from range, input is taken from the user.
    2)WAP to find the factors of any number, the input is taken from the user.
    3)WAP to find the sum of all numbers which are multiples of 3 or 5.(Don't count the common multiples twice).
    4)Practice a lot now.
    **************************************************************************************

    Ok then I shall conclude the tutorial here and in the next tutorial, We shall write more programs which would include printing *-patterns that can be created by control structures, finding GCDs, LCMs, trignometric values of a particular angles in radians, finding exponential value of any number, if time permits then we shall see even how to findout logarithms and other stuff.

    I will appreciate feedbacks from you,send a PM or VM anything you like, it will help me in return to make tutorials better.
    Until then, take care.
    Ciao.
     
    #20

Share This Page