Monday, April 29, 2019

C language : conditional statement

                      Conditional statement in C language

Conditional statement: The conditional statement help us to jump from one part of the program to another depending on whether a particular condition is satisfied or not. These decision control statement include:

1. if statement
2. if else statement
3. if else if statement
4. switch case statement

1. if statement-
The if statement is the simplest form of decision control statement that is frequently used in decision making. The syntax of if statement is shown in figure:

if (test expression)
{
Statement 1;
..........
............
Statement n;
}
Statement x;

The if statement may include one statement or more statements enclosed within curly braces. First the test expression is evaluated, if the test expression is it true then statement of if block, statement 1 to n are executed otherwise these statements will be skipped and execution will jump to the statement x . In control statement no semicolon is used after the test expression.

For example,
/* Program to find whether a person is eligible for vote or not. */

#include<stdio.h>
#include<conio.h>
void main ()
{
clrscr();
int age;
printf ("\n Enter the age ");
scanf ("%d",&age);
if(age>=18)
printf ("\n Person is eligible for vote.");
getch();
}

2 . if else statement
       The decision control statement in which, the test expression is first evaluated. If the expression is true, statement Block 1 is executed and the statement Block 2 is skipped otherwise if the expression is false statement 2 is executed and statement 1 is skipped or ignored. The syntax for simple if else statement is shown in figure:

if (test expression)
{
Statement 1;
}
else
{
Statement 2;
}
Statement x;

Now in any case after the statement Block 1 and 2 gets executed ,the control will pass to statement x. Therefore, statement x is executed in every case.

For example,

/* Program to find whether a given number is even or odd. */

#include<stdio.h>
#include<conio.h>
void main ()
{
clrscr();
int n;
printf ("\n Enter the value of n  ");
scanf ("%d",&n);
if(n%2==0)
printf ("\n Entered number is even. );
else
printf ("\n Entered number is odd.");
getch();
}

3 . If else if statement
      C language supports if else if statement to test an additional condition apart from the initial test expression. The if else if construct work in the same way as a normal if statement.
if else if statement is also known as nested if construct. It is not necessary that every if statement should have an else block as C language supports simple if statement. After the first expression or the first if branch the programmer can have many else if branches as he wants depending on the test expression that has to be tested . The syntax for if else if statement is shown in figure:

if (test expression)
{
statement 1;
}
else if (test expression 2)
{
statement 2;
}
.....................
......................
else

statement X;
}


For example,

/* Program to find whether a given number is greater or smaller or equal. Only for positive numbers */

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
printf("\n Enter the value of a and b ");
scanf("%d %d",&a,&b);
if(a>b)
printf("\n a is greater than b ");
else if(b>a)
printf("\n b is greater than a ");
else
printf("\n a is equal to b ");
getch();
}

4. Switch case statement
     A switch-case statement is a multiway decision statement that is simplified version of an if else block that evaluates only one variable. The syntax of switch case is shown in figure:

switch (variable)
{
case 1:
statement 1;
break;
case 2:
statement 2;
break;
................
...................
case n:
statement n;
break;
default:
statement y;
}

Switch case mostly used in two situation-
1. When there is only one variable to evaluate in the expression.
2 . When many condition are being tested for.

              When there are many condition to test, using if -else- if construct become a bit complicated and confusing. Therefore, switch case statement are often used as an alternative to long if statement that compares a variable to several integral values. The switch case statement compares the value of a variable given in the switch statement with the value of each case statement that follows. If the value of a switch and the case statement matches then the statement block of that particular case is executed.
In the switch case, the break statement must be used at the end of each case because if it were not used then all the cases from one met will be executed.

For example,
/* program for switch case statement */

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int main()
{
    int choice;
   while(1)
    {
  printf("\n 1. DAYS   ");
  printf("\n 2. MONTHS   ");
  printf("\n 3. Exit");
    printf("\n Enter your choice  ");
    scanf("%d",&choice);
    switch(choice)
    {
  case 1:
       printf(" Name of the days are ");
       printf("\n ");
       printf("\n Monday ");
       printf("\n Tuesday ");
       printf("\n Wednesday ");
       printf("\n Thrusday ");
       printf("\n Friday ");
       printf("\n Satday ");
       printf("\n Sunday ");
        break;
  case 2:
       printf(" Name of the months are ");
       printf("\n ");
       printf("\n January ");
       printf("\n Febuary ");
       printf("\n March ");
       printf("\n April ");
       printf("\n May ");
       printf("\n June ");
       printf("\n July ");
       printf("\n August ");
       printf("\n September ");
       printf("\n October ");
       printf("\n November ");
       printf("\n December ");
                break;
 
  default:
        printf("wrong choice");
         break;
case 3:
        exit(0);
     
   
    }
    getch();
}}

Tuesday, April 23, 2019

C language : Keywords and Identifiers

                   Keywords and Identifiers in C language

Keywords- 
Keywords are predefined reserved words used in programming that has a special meaning to compiler. Keywords are the  part of Syntax and they cannot be used as an identifier.
For eg. int cash;
Here int is a keyword that indicates cash, which is the variable of integer type.
As C is a case sensitive language all keywords must be written in lowercase.
There are 32 keywords in C programming language-
auto
double
 int
struct
break
if
 else
 long
switch
case
enum
register
typedef
 char
extern
return
union
continue
for
signed
void
do
 static
while
default
 goto
sizeof
volatile
const
float
short
unsigned

Identifier- 
 Identifiers refers to the name given to entities such as variables, functions, structures etc.. Identifier must be unique. They are created to give unique name to a entity to identify it during the execution of a program.
For eg. int cash;
              double accountbalance;
   Here cash and account balance are identifiers.
Also Identifier names must be different from keywords. We cannot use int as an identifier because int is a keyword.

C language : Variable

                                Variable in c language

Definition of variable-
C variable is named location in a memory where a program can manipulate the data. This location is used to hold the value of the variable.
The value of a c variable may get changed in the program. C variable might be belonging to any of the data type like int ,char, float or string etc.

Declaration of variable- 
Declaration of variable can be done in C by using following syntax:

data_type variable_name;
Or
data_type variable1,va variab2,...,variable n;


Where data_type is any valid c data type and variable_name is any valid identifier.
For eg.  int a,b,c;
               float a,b;

Variable declaration tells the compiler only two things-
1. Name of the variable.
2 . The type of data that variable will hold.

Initialization of variables- 
In C programming language variable can be initialized in the declaration statement of any block (either it may main's block or any other function block).
While declaring a variable we can provide a value to the variable with assignment operator.

Syntax of the initialization of the variable:

data_type variable_name=value;
For eg. int a=10;
              float=5.80
              char='m';
               int at[5]={1,2,3,4,5};

Scopes of variables-
Scope refers to the visibility of variables or we can say that scope is a region of a program. Variable scope is a region in a program where a variable is declared and used. So we can have three types of scopes depending on the region where these are declared and used-
1. Local variable-local variable are defined inside a function or a block.
2. Global variable-Global variable are defined outside all the functions.
3. Formal parameters- formal parameters are defined in function parameter.

Friday, April 19, 2019

C language : Operators

                            

                           OPERATORS IN C LANGUAGE

Operator-
An operator is defined as symbol that specifies mathematical, logical or relational operation to be used in expressions. These operation can be categorised into following terms:-
 
1. Arithmetic operator:    Arithmetic operator can  be applied to any integer or floating point operand.
for eg.    if a=5,b=4

Then,C=a+b=9
         C=a-b=1
         C=a*b=20
         C=a/b=1
         C=a%b=1

2. Logical operator:-    Logical operator are used to calculate expression containing the relational operators.
There are three types of logical operators--
a. Logical AND "&&"
b. Logical OR"||"
c. Logical NOT"!"

for eg. if a=5,b=4

then for AND 
if a>b&&a!=b
Then first condition is true and second condition is also true so, the whole condition is true.
then for OR
if a>b||a==b
Then first condition is true but here second condition is false so, the whole condition is also true.
then for NOT
if !(a>b)=False

3. Bitwise operator:-     Bitwise operator are those operator which perform operation in bit level. These operator include-
1. bitwise AND
2. bitwise OR
3. bitwise XOR
4. shift operator
5. bitwise NOT
1. bitwise AND:-    bitwise AND operator (&) is a small version of the boolean AND(&&) as it performs operation on bits instead of bytes, char ,integer etc.
for eg. 10101 & 01010=00000
2. bitwise OR:-   bitwise OR operator ( | ) is a small version of the boolean OR ( || ) as it performs operation on bits instead of bytes ,char,integer etc.
for eg 10101 | 01010=11111
3. bitwise XOR:-       bitwise XOR operator (^) performs operation on individual bits of the operands.
for eg. 0101 ^ 1010 = 1111
           0101 ^ 1111 = 1010
4. shift operator:-     C supports two bitwise shift operators.
They are shift left "<<" and shift right ">>".
       These operations are simple and responsible for shifting bits either to the left or to the right.

Left shift ( << ) - When we apply a left shift ,every bit in X is shifted to the left by one place. So, the MSB of X is lost , the LSB of X is set to 0.
for eg. if  X = 0001 1101
         then X<<1 = 0011 1010

if X=0001 1101
  then, X<<4 = 1010 0000

Right shift ( >> )- When we apply a right shift,every bit in X is shifted to the right by one place. So, the LSB of X is lost and the MSB of X is sets to 0.
for eg.- if X=0001 1101
then, X>>1 = 0000 1110
  
   if  X = 0001 1101
then  X>>4 = 0000 0001

5. bitwise NOT:-       The bitwise NOT (~), or complement is a unary operation that performs logical negotiations on each bit of the operand. By performing negotiation of each bit, it actually produces the 1's complement of the given binary value. BITWISE NOT operator sets the bit to 1 if it was initially 0 and sets it to 0 if it was initially 1.
for eg. ~10101011 = 01010100

4. Equality operator:-  C language supports two kind of equality operators to compare their operands for strict equility or inequalty. They are equal to  ( == ) operator and not equal to ( !- ) operator. 
Equal to ( ==) operator returns one(1), i.e., true if both the operands are , otherwise it returns zero.
Not equal to (!=) operator returns one(1), i.e.,true if both the operands do not have the same value.


5. Unary operator:-     Unary operator acts on single operands. C language support three unary operator:
       unary minus; increment and decrement operator.
unary minus:  Unary minus ( - ) operator is strikingly different from the binary arithmetic operator that operates on two operands and subtracts the second operand form the first operand.
when an operands is preceded by a minus sign , the Unary minus operator negates its value.
for eg  int a,b = 10;
a=-(b);
then, a=-10

increment operator: Increment operator is a unary operator that increases the value of its operand by 1(one). The increment operator  have two variants- prefix and postfix.
In prefix expression ( ++X ), the operator is applied before an operand is fetched for computation and in postfix expression ( X++ ) the operator is applied after an operand is fetched for computation. 
for eg.
int  x = 10,y,z;
     y = x++;
  z= ++y;

so, y=11 and z = 11

decrement operator :
 The decrement operator is a unary operartor that decreases the value by 1 . The decrement operator have two variants-  prefix and postfix.
In prefix expression ( --X ), the operator is applied before an operand is fetched for computation and in postfix expression ( X-- ), the operator is applied after an operand is fetched for computation.
for eg.  a= 7;
then,  a--  = 6
         --a  =  6
6. Conditional operator:    The conditional operator or the ternary ( ?: ) is just like an if  else statement that can be within expression. Such an operator is useful in situation in which there are two or more alternatives for an expression.
for eg.   large =  (a>b) ? a : b

The conditional operator is used to find largest of two given numbers. First expression  i.e.,  (a>b) is evaluated. if 'a' is greater than 'b' then large = a , else large = b.

7. Assignment operator:-     In C, the assignment operator is responsible for assigning values to the variables while the equal sign ( = ) is the fundamental assigning operator.
 for eg. int x;
           x = 10;
then, ( x+ = 2 ) = 12
          ( x/=5 ) = 2
           ( x*=3 ) = 30
          ( x-=4 ) = 6
8. Comma operator:-      The comma operator in C takes two operand. It works by evaluating the first and discarding its value, and then evaluates the second and returns the value as a result of the expression. Comma operator separates the operands when chained together are evaluated in left to right  sequence with the right most value yielding the result of the expression.
for eg. int a = 3, b = 4, c = 6;

9. sizeof operator:      The sizeof operator is a unary operator used to calculate the  size of datatypes. This operator can be applied to all datatypes. The sizeof operator is used to determine the amount of memory space that the variable/expression/datatypes will take.
      When a type name is used , it is enclosed in parenthesis, but in case of variable names and expression they can be specified with or without parenthesis.
for eg.        int a = 10; 
                  unsigned int result;
                  result = sizeof(a);
       then,    result = 2 

which is the space required to store the  variable 'a' in memory. Since 'a ' is an integer , it requires 2 bytes of storage space.

10. Relational operator:-    A relational operator, also known as a comparison operator, is an operator that compares two values . Relational operator return true or false value, depending on whether the conditional relationship between the two operands holds or not.

Operator                            Meaning                                 Example
   <                                         less than                                     3<5 gives 1
   >                                         greater than                                7>9 gives 0
  >=                                        less than or equal to                  100>= 100 gives 1
  <=                                        greater than or equal to              50>= 100 gives 0


Thursday, April 18, 2019

C language : Datatypes



                Data types in C programming language

  Datatypes:-Data types in C refer to an extensive system used for declaring variable or functions of different types. The type of variable determines how much space it occupies in storage and how the bit pattern stored is interpreted.
   There are two types of data types in C programming language.

1. Predefined data types
2. User defined data types

1. Predefined data types- pre defined data types are those data types which are already defined in C programming language. We cannot do any changes in this type of data types as it is fixed by the developers. There are five types of predefined data types which are described as follows:

1. int
2. char
3. float
4.  double
5.  void

 1.     int- int is used for integer value. It's size is two bytes which ranges from -32768 to 32767 for signed integer and 0 to 65535  for unsigned integer.
For eg.    int b;
                 int a=5;
2.    char- char is used for character value. It's size is one byte. It ranges from - 128 to 127 for signed characters and for unsigned character it is from 0 to 255.
 For eg. char a;
3.  float-float is used for floating point numbers or for fractional values. It's size is four bytes. It ranges from 3.4E-38 to 3.4E+38.
For eg.     float a=45.32;
4.  double-double is also used for floating point numbers but it's size is eight bytes. It ranges from 1.7E-308 to 1.7E+308.
For eg.    double b=3467.125478;
5.  void-void keyword has no value all we can say that it is valueless special purpose type.
    For eg.      void()

2.   User defined data types- user defined data types are those data types  which are defined by the user. Here we can make our own data types with the help of structure, Union, Enum, typedef.

Function

FUNCTION C enables programmer to break up a program into segments commonly known as functions, each of which can be written more or...