Thursday, August 27, 2020

Function in C

Function play an important role in programming.It is basically a collection of statements which perform specific task.Each function have some specific name which is used for calling the purpose in any program.We can say that it is a type of sub-program.

Function is a self contain program that can perform some specific and well define task. A large program is broken down into number of smaller program in the form of function which can perform specific task. we can achieve re-usability by creating functions.Any function can define once and call many times in a program.

In C programming their are two type of functions such as:

 Predefined functions

✔ User define functions.

BUILT-IN FUNCTION

PREDEFINED FUNCTION

Predefined functions are built-in functions or library functions which is used by user.User can not define those functions.These functions are created at the time of creation of C language.We cannot change their definition.These functions are stored in library files and these library files are known as header files. We can identify these file by ( .h ) extension.There are so many header files are present in C.If we want to use any built-in function in our program than we must include these header files in C program by using INCLUDE keyword. 

like: # include <conio.h> , # include <stdio.h> , # include <math.h> , # include <string.h> etc.

Now we can simply say that all built-in function are available in header files. So without including these header file we can not use any built-in functions in any program.

Some built-in functions are printf() , scanf() , sum() , pow() etc

USER DEFINE FUNCTION:

User define function are those function which is define by user for their specific task.Let we understand Why user define function needed ?

We know that we write our program inside main() function. If our program is small than no need to create any functions but if our program goes long in size line of code than we must break our program in small modules called function.

This process will improve :

✔ Clarity  [ it is very clear to see every line of code ]

✔ Readability  [ things are readable so that we can understand ]

✔ Re-usability  [ if we need any function again and again in same program ]


For creating any function we must do following:

  1. Function Declaration
  2. Function Definition
  3. Arguments of a function.
  4. Return statement.

1. Function Declaration

Every programmer must define function before it use in program.We must define this function return which type of value like int, float, char etc. and list of argument used in program.

Syntax: 

Data_Type function_name (Parameter_List);

Example:

float area (float a ,float b);

int volume (int a, float b,  float c);

char function_name(arg 1, arg 2..);

void show ( );

All above cases show , Data type may be (char , int , float etc.) which show return type of any function.

2. Function Definition:

For using function in any program than we must define function.Function definition contain name of function , number of arguments (0,1,2...) and body of function.

Syntax:

Type Function_name (arguments....)

{

    body of function

    return statement.

}

Example:

float cube (float x)

{

    return (x * x * x);

}

3. Arguments of a function:

In any function argument are placed inside the parenthesis  ( ). Inside parenthesis number of argument may be [ 0 , 1 , 2 , 3 ,.....,n ]. This argument list contain valid datatype and variable name such as  [ int a , float b , char c..] . variables are medium through which calling function communicate to called function in any program.

Let we explain with the help of example:

# include <stdio.h>

main( )

{

    int a;

    for ( a =1; a <= 5; a ++ )

        {

            printf( " /n cube of %d" is %d ", a , cube (a)) ;    Calling Function

        }

    }

    cube ( int x )        Called Function

    {

        int y ;

        y = ( x * x * x ) ;

        return (y) ;

        }

Explanation :

Here in the above example we create cube() function with argument (a) so here one argument is used.We can use more than one arguments according to function requirement.In calling function argument (a) is called actual argument and in called function (int x) is called formal argument. Actual argument in calling function and formal argument in called function must be same datatype .Calling function and called function must have same number of arguments.

4. Return Statement :

Called function return one value to calling function.There are number of arguments in called function but it return single value.Return statement is responsible to return value from called function to calling function.Our main purpose of create any function is to produce some value after processing statements inside the function.

Like above example called function produce cube of x , process it and result is return to calling function.

Example:

 type function_name ( data_type variable_name )        

    {

       ----------

        ---------

        return (value) ;

        }

Complete Program:

#include < conio.h >

#include < stdio.h >

void main ( )

{

int i;

clrscr ( );

int cube ( int n );     // Function declaration

ptintf ( "Enter any number:" );

scanf ( "%d",&n);

printf ( "Cube of %d is %d",n,cube ( n ) );   // Calling function

getch ( );

}

cube ( int a )          //Called function

{

int y;

y=a*a*a;

return ( y );

}

I hope you all understand the basic of function,how we can create a simple function in any C program.

Friday, July 17, 2020

Loop Control Structures

Looping is a repetitive structure.In looping a group of structures are executed until some condition has been satisfied.

In C following loop structure are available:
  1. for loop
  2. while loop
  3. do-while loop
1. for loop


Syntax:

for ( initialisation expressions;
test expression;
update expression;)
body-of-the-loop;

Example:

int i; //declare variable
for(i=1;i<=10;i++)
printf("\n%d",i);
getch();
Note:
case 1.:

for(initialise exp1 initialise exp2;
test expression 1 test expression 2;
update expression 1 update expression 2)

case 2.:

for( ;test expression;update expression)

case 3.:

for(initialise exp; ;update expression)--infinite
for( ; ;)--infinite

case 4.:

for(initialise exp;test expression;update expression); -empty no body

case 5.:

for(int i=1;i<10;i++) --deceleration of i

2. while Loop
while loop is an entry control loop. In a while loop control variable should be initialised before the loop begins as an uninitialised variable can not be used in an expression.The loop variable should be updated inside the body of the while.
Syntax:

while(expression)
loop-body

Example:

int i=0;
while(i<=10)
{
printf("%d",i);
i++;
}

Note:

repeat expression.
syntax: while (boolean) statement 1;
example while(1);
while(0);

3. do-while Loop
do-while loop is an exit-control loop. do-while loop execute statement at least once. because test condition evaluates at the bottom of loop.
Syntax:

do
{
statements;
}while(test-condition);

Example:

int i=1;
do
{
printf("%d\n",i);
}while(i<10);

Friday, July 10, 2020

Decision Control Statements, Break, Continue, Exit, Goto

Decision control structures (also called selection statements)allow to choose the set-of-instruction for execution depending upon an expression truth value.

The main decision control statements are:
  1. if-statement
  2. if-else Statement
  3. Conditional Operator
  4. Switch Case

1. if-statement:

  Syntax:
   if(expression i.e condition)
     statement i.e action;
2. if-else Statement:
  Syntax:
   if(expression)
      statement1;
   else
      statement2;
2.1 Nested if-else Statement:
  Type 1:
  Syntax:

   if(expression 1)
   {
   if(expression 2)
      statement 1;
   [else
      statement 2;]
   }
   else
      body-of-else;
   }
  Type 2:
  Syntax:

   if(expression 1)
      body-of-if;
   else
   {
   if(expression 2)
      statement 1;
   [else
      statement 2;]
   }
   }
  Type 3:
  Syntax:

   if(expression 1)
   {
   if(expression 2)
      statement 1;
   [else
      statement 2;]
   }
   else
   {
   if(expression 2)
      statement 1;
   [else
      statement 2;]
   }
   }
2.2 if-else-if ladder or if-else-if or Nested if:
  Syntax:
   if(expression)
      statement 1;
   else if(expression 2)
      statement 2;
   else if(expression 3)
      statement 3;
     .......
   else
      statement n;
3. Conditional operator:
The conditional operators ? and : are called ternary operators since they take three arguments.
      Syntax:

        expression 1 ? expression 2 : expression 3

      Example:

        y=(x<3 ? 1 : 7);
4. Switch case statement:
C provide a multiple-branch selection statement known as switch.
      Syntax:

switch(expression)
{
case constant 1:statement sequence 1;
break:
case constant 2:statement sequence 2;
break:
.......
.......
case constant n-1:statement sequence n-1;
break;
[default: statement sequence n];
}
Break statement:
Braek statment is used to terminate the execution of a statement block.The next statement block will be executed.
Keyword: break;
When a break ststement is executed in a loop, the repetition of the loop will be terminated.
Continue Statement:
A continue statement is used to transfer the control to the begnning of a statement block.
Keyword: continue;
When a continue statement is executed in loop, the execution is transfer to the beginning of the loop.
exit() :
An exit() function is used to terminate the execution of a C program permanently.
Keyword: exit();
It is a built-in function and is used/called with necessary argument as exit(0);
goto and label:
A goto statement can transfer the program control anywhere in the program.The target point of a goto statement is marked by a label.
Syntax:
goto label;
.....
label:

Tuesday, June 30, 2020

Creating and Compiling C Programs

When we create source code for any c program than we use any c editor and write program. C program contain two type of file

1) Header file:
2) Source file.


Extension of Header file is .h and extension of c program is c. Write program in C editor than after compiling the program source code is converted into object code/machine code. Now link all the object files by the help of linker program. After that resulting file will be executable file (.exe) which we can run and generate output on screen.


Operators in C

In C language operators are pre define. It is represented by symbols. An expression is created by using one or more operand with operator.

In C language there are four basic type of operator.
  1. Unary Operator
  2. Binary Operator
  3. Conditional Operator
  4. Bit wise Operator

1. Unary Operator : Unary operator are those operator in which one operator is used to create one expression.
There are three type of unary operator:
  1. Increment Operator
  2. Decrement Operator
  3. Size of Operator
a. Increment Operator :This operator is used to increment operand by 1. Such as : ++i or i++ or i=i+1
b. Decrement Operator :This operator is used to decrement operand by 1. Such as : --i or i-- or i=i-1
c. Size of Operator :This operator is used to specify the size of memory sued by operand. Such as : sizeof(i);
2. Binary Operator :Binary operator are those operator in which two operator are used to create one expression.
There are four type of binary operators:
  1. Mathematical Operator
  2. Relational Operator
  3. Logical Operator
  4. Assignment Operator
a. Mathematical Operator :This operator is used to do all type of arithmetic calculations. Such as:
  • Addition (+)
  • Subtraction (-)
  • Multiplication (*)
  • Division (/)
  • Modulus (%) it return remainder After integer division.
b. Relational Operator :It is used to check the relation between two variables. Such as:
  • Less then (<)
  • Greater then (>)
  • Less then or equal (<=)
  • Greater then or equal (>=)
  • Equal to (==)
  • Not Equal to (!=)
c. Logical Operator :It is used to perform logical operations. It is generally used in if condition such as:
  • AND (&&) It must contain two conditions
  • OR (||) It must contain two conditions
  • NOT (!)
d. Assignment Operator :It is used to assign any value or any expression such as : a=b+c; , a=6;
3. Conditional Operator (?:) : Conditional operator is used for decision making. It is also known as single line if-else statement.
4. Bit-wise operator :It is used to perform bit-wise operation. Binary bits are used to perform operation. That involve the manipulation of individual binary bit. It is three type :
  • Bit-wise or conferment operator (~)
  • Bit-wise logical operator (&)(^)
  • Bit-wise shift operator (<< and >>)
Hierarchy (Precedence) of operators
Precedence of operators are as follow:



Saturday, June 20, 2020

Library Functions in C

Library functions are those functions which are pre-defined by compiler of C language. Some of the library functions are given below:
  1. Input / Output function (define in standard input/output stdio.h)
    Input/Output function is define in stdio.h header file. In C language 3 Input and 3 Output function are available for input and output of any data.
    Input Function
    • getchar()
      getchar() function is used to take single input charater data.
      Syntax: variable name=getchar();
      Example: x=getchar();
    • gets()
      gets() function is used to take multiple input charater data.
      Syntax: gets(variable name);
      Example: gets(name);
    • scanf()
      scanf() function is used to take all type of input data such sas (character data , string data , numric data).
      Syntax: scanf("Control string",&variable1,&variable2.....);
      Example: scanf("%d%f",&x,&y);

    Output Functions
    • putchar()
      putchar() function is used to print single charater data. It is output function.
      Syntax:putchar(variable name);
      Example: putchar(x);
    • puts()
      puts() function is used to print multiple character data (string).
      Syntax: puts(variable name);
      Example: puts(name);
    • printf()
      printf() function is used to print all type of data such as (character data , string data , numric data) on the screen.
      Syntax: printf("Control string",variable1,variable2.....);
      Example: printf("sum=%d",sum);
                       printf("sum of number",sum);
                       printf("%d",sum);
  1. Mathematical Function (define in math.h)
    These functions are used for mathematical operation.Include "math.h" header file before main() function for using these functions. These functions are:
    • sqrt():This function is used to find out square root of any number.
      Syntax: sqrt(argument);
      Example: sqrt(x);
    • pow(): pow() function is to find power of any number.
      Syntax: pow(argument1 , argument2);
      Example: pow(x,y); (i.e xy)
    • abs(x): This function return absolute number (positive number) of any numeric number.
      Syntax: abs(argument);
      Example: abs(-10); (i.e return 10)
  2. String function
    String function is used to manipulate or format the sting. String is a group of characters. Include "string.h" header file before main() function for using these functions.These functions are :
    • strlen(): This function is used to return the length of string.
      Syntax: strlen(argument);
      Example: x=strlen("Hello");
    • strcat(): This function is used to concate two string.
      Syntax: strcat(s1 , s2));
      Example: x=strlen("Hello" , "world");
    • strcpy(): This function is used to copy one string into second string.
      Syntax: strcpy(s1 , s2));
      Example: x="hello"; y="world"; strcpy(x , y);
    • strcmp(): This function is used to compair the length between two string.
      Syntax: strcmp(s1 , s2)); (i.e return 0 for s1=s2 , -1 for s1s2)
      Example: x=strcmp("hello" , "Helloworld"); return -1

Structure of Program in C

C program is a combination of one or more functions. C program contain one main function called main().Compiler start execution of program from main() function. Each C program contain following section such as:
  1. Preprocessor
    Programmer specify name of header file in this section. It provide instruction to C compiler.
    Header files such as: #include <conio.h> , #include<stdio.h> , #include<math.h> etc.
  2. Function Heading
    This section contain function name with arguments (if any) such as main().
  3. Variable Declaration
    This section contain variable declaration , definition , initialisation with their data type. Such as int x=1; , cahr a; , float p=3.14; etc. This section end with semi colon.
  4. Compound Statement
    This section contain group of statement. Statements contain printf , scanf , if..else , switch case , getch() , calculation (a=b+c) etc statements in this section. We can say that this section contain Input , Output , calculation part , condition statements.