Saturday, August 1, 2015

The Conditional/Ternary Operators.

Sometimes conditional operators" ? and : "  are called as ternary operators as they take three arguments.In the form of if then else.The syntax is

expression 1 ? expression 2 : expression 3
the following expression can be read as if expression 1 is true then it will return expression 2 else it will return expression 3.
Let's understand with the help of example:

//example of if-else 
int a,x;
scanf("%d",&a);
if(a%2= =0)
x=1;
else
x=0;

from above example it is clear that if condition is true it will give the value of x =1 otherwise it will give x=0.
Now if we want to write the above case in terms of conditional operator then it will be written as


//example using conditional operator

int a,x;
scanf("%d",&a);
x=(a%2==0 ? 1 : 0);

Here 1 would be assigned to x if a%2= =0 is true otherwise 0 will be assigned to x.

Few points that we should know about conditional operator is that :
1) It's not necessary that these operators can be used only in arithmetic statements.
2) It can be used as nested format.

Note :
if expression will be given in format like
x<y?z=1:z=0;

this statement will give the error this can be overcome by placing the expression 3 in the parentheses i.e.(z=0)

 x<y?z=1:(z=0);

If we do not put the parentheses the compiler believes that 0 is being assigned to the result to the left of second=,and thus gives the error.

Limitation of conditional operator:The limitation of this operator is that after the " ? " or after the " " only one C statement can occur .

Thursday, July 16, 2015

Decision Control Structure:

There are some circumstances where we need different sets of action to be performed at a particular instant of time.A C language has three major decision making instructions------
1->The if statement.
2->The if else statement.
3->switch statement.
Apart from these three we have fourth type also i.e. conditional operators.
In this post I will show you about the if ,if else method, rest I will describe in another post.

The if Statement:

C uses a keyword if to implement the decision control instructions,it's syntax is
                               if(condition true)                                                                                                                                                  {    execute these set of statement;.....
                                                __________________}    
Flow Chart of" if "Structure

The if statement  tells the compiler that to check the condition is true or not enclosed within a pair of parentheses. If true then it will undergo the execution block of "if".otherwise proceed to other statements.
NOTE: In C a non zero value is considered to be true and 0 is considered to be false. i.e.
if(5)
//true statements are executed
if(-5)
//here too statements gets executed true
if(0)
//false it will terminate the if block

there are operators which can be used for particular conditions like:
RELATIONAL OPERATORS
1)
"= =" :Equal to
example:
 a==b means a is equal to b
2)
"!=" : Not equal to
example:
a!=b means a is not equal to b.

3)
"<" :Less then
example:
a<b means a is less then b.

4)
">":Greater then
example
a>b means a is greater then b.

5)
"<=" :Less then or equal to
example
a<=b means a is less then or equal to b
6)
">=" :Greater then or equal to
example
a>=b means a is greater then or equal to b.

LOGICAL OPERATORS
7) The logical And is represented as " && "

8) The logical OR is represented as "  || "

9) The logical NOT is represented as " ! "

The if -else Statement.

The main drawback of  if statement is that the following set of statements within "if block " executes only when the condition is true .It does nothing when expression evaluates to false.This is what exactly the purpose of  else statement  is being used.

syntax:


          if(condition)
         {........//statements; }
        else
    {,,,,,,,,,,,,,,,,,,,,,,,,,,//statements;}
Flow chart for " if-else"structure.
NOTE:The else block should be written exactly below the "if "block.

Nested if-else :
We can also write an entire if-else construct within either the body of if statement  or the body of else statement. This is called nesting of ifs
The example of nested if-else is shown below:
Nested if-else.









Tuesday, July 7, 2015

Types of C Instructions

As we all know that a Program is a set of Instructions.Different Instructions helps us  to achieve different task in the program.In previous post i described you about the simple C program,Now lets discuss over C instructions.
Generally there are three types of instructions:
1) Arithmetic Instructions:Such instructions are used to perform arithmetic operations on constants      and variables.
Example:

                     a=i.j.k(x+y)                   // a normal arithmetic expression                                                                                                  a=i*j*k*(x+y)                // a C statement.

2)Type Declaration: This is used to declare the type of the variable.These statements are usually    written at the beginning of main( ) function.
 NOTE: The order in which we define the variables is sometimes important sometimes not.
Example:
                   int i=5,j=2;
                   float a=5.3,b=a-2.6;    //these above two examples are acceptable however the below  declaration is not

                  (float b=a-2.6,a=5.3; ) is incorrect as here we are trying to use variable a ,even before defining it.

NOTE:In order to use any variable in instruction it must be defined in memory first . 
3)Control Instructions: These set of instructions are used to control the sequence of execution of  various statements.
There are four types of  control instructions in  C.
1) Sequence Control
2) Selection and Decision Control.
3)Loop or Repetition Control.
3)Case Control.

All these types you can see in programming part

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.