Monday, June 29, 2015

What is a Program?

A program is a set of instructions  or we can say a sequence of instructions which are written to perform a specific task on computer.

Formation of a C program:

Lets take an example we have to print "WELCOME TO THE WORLD OF C PROGRAMMING"
the source code for the following is shown in the below



And the output for the corresponding source code is shown on black screen.




While Forming a C program we must have to keep the following points in mind:

1) Each of the instruction in C is written as a separate statement.
2) The instructions in a program must appear in the same sequence in which we wish to execute              them.
3) All instructions must be written in small letters .
4) C has no specific rules for the positioning at which statement has to be written. That's why it is            often called free-form language.
5) Each statement of the program must end with symbol ;
    As ;(semi colon) acts as a instruction terminator.


Now as you can see in above source code the body contains different types of parameters and each of them have specific functions so starting with the very first line i.e. Comments

1- Comments are basically used as the users point of view to decode the program
they are non executable instructions,the only use of these is to clarify the purpose of program or a particular statement.Though it is not necessary to write comments for each instruction.

Comments can be of two types 
a)Single line Comment : Comment can be written in single line as:
                                   
                                        //Hello reader welcome to c programming.
                                       //This is just a demo for you .
                                       //you can write anything here.

b)Multi line Comment: The comments written above can be split into a single multi line comment as:
                                   
                                   
                                     /* Hello reader welcome to c programming.
                                          This is just a demo for you .
                                          you can write anything here.  */

2-(#include<stdio.h>)
    This line is compulsory for including the library files of standard input /output functions.
    I will describe these later.

3-What is main().
    The main() function is the important part of the body of C program,All instructions should be              written under this function enclosed  within a pair of braces { }
     as shown in above source code

4-printf() and it's usage

    All output to screen is achieved using  readymade library functions.one such function is printf().
    syntax for using this function is:

                                                   printf("<Formatted string>",<list of variables>);


<Formatted string> can contain Message for user along with that format specifiers such as:
%f -for printing real values.
%d -for printing Integer values.
%c -for printing character values.

you can best understand this while writing further programs.

Note: printf() can not only print values of variables,it can also print the result of the valid expression.
         As expression is nothing but a valid combination of constants,variables and operators.
         Thus,2,8+2,a and a+b*c-d all are valid expressions.
         The result of these expression as shown below:
         printf("%d %d %d %d",2,8+2,a,a+b*c-d);

         COMPILATION AND EXECUTION:
Once program is framed you need to type it and instruct the computer to execute it .To type your C program you need editor.Once the program is typed it needs to be converted to a machine language(0s and 1s)i.e. binary language before machine can execute it.To carry out this we need Compiler.
There are several vendors who provides us Integrated Development Environment (IDE) which consist of an Editor as well as the Compiler.
One such IDE is Turbo C/C++
you can download the software by clicking here. 

Wednesday, June 24, 2015

C Variables

In the previous post I described you about the Types of C constant.Now moving on to variables.
Variables are the identity  which can only hold a particular type of data. For Example: an Integer type variable can hold only integer type of value or constant,character variable  can only hold character constant and same with other types also.

Rules for constructing any type of variable:
1)-Any variable name can be formed with the help of alphabets,digits and underscore.Do not create unnecessarily long names as it will create a problem later as it adds to your typing effort when you needs to use a variable again and again.

2)-No commas or blanks are allowed.

3)-No special Character other than underscore are allowed in variable name.

4)-The first letter or character of the variable name should start with alphabet.

examples: nxt_35_e
                 wan_pro
                 amt_per

Now the important point which differentiate these variables in C are there data types.
So to declare any variable in C we must have to follow the format as

                               (Data type) (variable name);

                           eg: int nxt_35_e;
Note:- there should be a gap/space in between the Data type and Variable name.
 For good programming it is advisable to use the small length variable names.

Keywords:

Keywords are the reserved words which are predefined in the C compiler. They cannot be used as variable names. There are only 32 Keywords available in C these are listed below:

1)-auto: It Defines a local variable as having a local lifetime.
              syntax:
                          [auto] data definition;

2)-break: It passes the control out of the compound statement block.      
                syntax:
                            break;

3)-switch
4)-case
5)-default
           :these three are the part of the branch control statements
           syntax:
                      for switch:
                                        switch(expression) statement
                      for case:  
                                         case constant expression:
                      for default:
                                         default:

6)-char : character data type.
7)-int :   integer data type.
8)-const: Makes the variable value or pointer parameter unmodifiable.
               syntax:
                           const variable_name=value;
9)-continue: It passes the control to the beginning of the loop.
                   syntax:
                              continue;
10)-do: it is used in do while loop
           syntax:
                       do
                       {
                         //statements
                         }while(expression);
11)-enum: Defines the set of constants of type int.
                syntax:
                            enum [tag] {name [=value], ..... }
12)- extern: It indicates that an identifier is defined elsewhere.It may be applied to data definitions                         and function prototype             
                    syntax:
                               extern data_definition;
                               extern function-prototype;

13)-float:
                    ----------------------------Floating point data types.
14)-double:

15)-for:      for loop
                   syntax:
                               for(initialization;condition;update)
                               {
                                  //statements;
                                  }

16)-goto: unconditional transfer control
                syntax:
                           goto identifier;

17)-if
              ---------------------------------------Conditional statement
18)-else
              :syntax:
                          if(expression)
                          statement 1;
                          else
                           statement 2;

19)-register: It tells the compiler to store the variable which is being declared in the CPU register.
                    syntax:
                               register data-definition;

20)-return: Exits the function.
                  syntax:
                             return(expression);

Type Modifiers:21 to 24
21)-short
22)-long
23)-signed
24)-unsigned

25)-sizeof: It returns the size of expression or type
                 syntax:
                             sizeof(expression);
                             sizeof(type);

26)-static: It preserves variable value to survive after it's scope ends.It is applied to both data-                               definition or function definition.
                  syntax:
                             static data_definition;
                             static function-definition;

27)-struct: It groups different data type variables into a single record.

28)-typedef: It creates a new type.
                    syntax:
                               typedef type-definition identifier;

29)-union: It groups the variable which shares the same storage space
                 syntax:
                             union [union-type-name]
                             {
                               type variable-names;
                                .  .   .   .
                               } [union-variables];

30)-void: It's an Empty data type.

31)-volatile: It indicates that a variable can be changed by it's background routine
                   syntax:
                               volatile data-definition;

32)-while: while loop ,it repeats execution if while condition is true
                 syntax:
                             initialization;
                             while(condition)
                             { 
                                //statements;
                                update;
                              }

Note that some vendors like Microsoft design there own compiler with some extra keywords apart from the ones mentioned above.

Monday, June 22, 2015

Getting Started With C

Learning C is just like learning any other language and is easier.Instead of straight away learning how to write programs,we must first know the Alphabets,numbers and special symbols which have been used in C,and then how with the help of these constants,variables and keywords are constructed and how are these combined to form an instructions.
You can best understand this with the help of below flow chart example:

Steps in learning English Language:

Alphabets->Words->Sentences->Paragraph.

Steps in learning C Language:

Alphabets                   Constants              
Digits                     -> Variables           -> Instructions  ->Program.
Special Symbols         Keywords

In C we have three types of Character Set:

Alphabets:                 A,B,C,............,Y,Z.
                                  a,b,c,................,y,z.
Digits:                       0,1,2,3,4,5,6,7,8,9

Special symbols:       ~ ` ! @ $ % # ^ & * ( )_-+={}[] | \ : ; " ' < , > . ? /

The alphabets,digits and special symbols when properly combined form constants,variables and keywords.

Constant: A constant is an entity that doesn't change.
Variable: A variable is an entity that may change.
Keyword:A keyword is a word that carries special meaning.


Constants are further classified into two categories:
 (i)-Primary Constants.
(ii)-Secondary Constants.


Primary Constants->a:Integer Constant.
                                 b:Real Constant.
                                 c:Character Constant.
Secondary Constant->a:Array
                                    b:Pointer
                                    c:Structure
                                    d:Union
                                    e:Enum. etc.


In this  discussion we discuss only Primary Constants and rest later.

Rules For Constructing Integer Constants:

1- An Integer constant must have at least one digit.
2- It can be positive or negative.
3-If no sign precedes an integer constant,it is assumed to be positive.
4-No Commas or blanks are allowed with in it.
5-It must not have a decimal point.
6-The allowable range for integer type constant is:
        -2147483648 to +2147483647. for compilers like gcc,Visual Studio etc.
    and -32768 to +32767 for compilers like Turbo C++ etc.

eg:
     895
     -9645
    +789
      12

Rules For Constructing Real Constants:

These constants are also called as Floating Point Constants
The real constants can be written in two forms
Fractional Form and
Exponential Form.

Rules which we have to keep in mind while constructing these as in Fractional form:
1-It must have at least one digit and have a decimal point
2-It could either be positive or negative.
3-Default sign is positive.
4-No commas and blanks are allowed
Eg::
        +596.58
         36.25
         -0.28

In exponential form the real constant is represented as in two parts :The part appearing before 'e' is called mantissa where as part after 'e' is  called as exponent.Thus 0.000896 can be written in exponential Form as 8.96e-4.

Rules for exponential form is:
1-The two parts(Mantissa and Exponential)  must be separated by letter 'e' or'E'.
2-Default sign of Mantissa part and Exponent part is positive but it may have positive or negative           sign.    
3-The exponent must have at least one digit.
4-Range of real constants is -3.4e38 to +3.4e38 

Rules For Constructing Character Constants:

1-It is a single alphabet,single special symbol or a single digit enclosed within single inverted                commas.
2-Both commas should point to left i.e.'B' where as 'B` is invalid.

Eg:
      'S'
      'd'



--------------------------------------------------------------------------------------------------------------------------
In next post i will describe you about C variables and C keywords after that i will show you the programming portion of C.






Sunday, June 21, 2015


Introduction To C

Before I began to show you how to write serious programs in C language,it would be interesting to make you find out what really C is,How it came into programming world and how you can differentiate it with other languages .
In this blog we would briefly outline these things.
Apart from C in any language we have four important aspects on which whole body of programming language exist they are :
->The way it stores the data
->the way it operates upon this data.
->How it traverses the input and output.
->And How it lets you to control the Sequence of execution of instructions in a program.

What is C ?

C is a programming Language developed at AT & T's Bell Laboratories of USA in 1972.
It was designed and written by Dennis Ritchie .

Possibly why C seems so popular is because it is reliable,simple and easy to understand and use.Moreover ,in present industry where newer languages and technology emerges and vanishes day in day out ,a language that has survived for more than three decades has to be really good.
An opinion that is often heard in present is that "C has been already Superseded by languages like-C++,C# and JAVA  so why bother to learn C ?"

There are several reasons regarding this question:

1-Though Languages like C++,C# and JAVA make use of principles of Object Oriented programming(OOP) to write a program.Though this technique have lots of advantages .But even while using this organizing principle you still need to hold a good knowledge of programming skills which can better obtained by learning C. Though it's a long way ,but end it will definitely find it worth trouble.

2-Though many languages came into existence but still major  parts of popular operating system like Linux,Windows e.t.c.  are still written in C.This is because when it comes to performance level no other language beats the C.

3-Sometimes one is required to interact with the hardware device very closely.Since C provides Several  Language Elements that make the interaction easier without comprising the Computer Performance.

There are lot more Areas in Programming World where the performance matters programmers prefer to use C.I hope you get it  why C is essential for Beginners.
  In Next post we will more Discuss about Getting Started with C.