Saturday 15 December 2018

Function


Function
function is a group of statements that together perform a specific task. Every C program has at least one function, which is main().
 Why use function ?
Function are used for divide a large code into module, due to this we can easily debug and maintain the code. For example if we write a calculator programs at that time we can write every logic in a separate function (For addition sum(), for subtraction sub()). Any function can be called many times.
Advantage of Function
·         Code Re-usability
·         Develop an application in module format.
·         Easily to debug the program.
·         Code optimization: No need to write lot of code.
Type of Function
There are two type of function in C Language. They are;
·         Library function or pre-define function.
·         User defined function.
Library function
Library functions are those which are predefined in C compiler. The implementation part of pre-defined functions is available in library files that are .lib/.obj files. .lib or .obj files are contained pre-compiled code. printf(), scanf(), clrscr(), pow() etc. are pre-defined functions.
Limitations of Library function
·         All predefined function are contained limited task only that is for what purpose function is designed for same purpose it should be used.
·         As a programmer we do not having any controls on predefined function implementation part is there in machine readable format.
·         In implementation whenever a predefined function is not supporting user requirement then go for user defined function.
User defined function
These functions are created by programmer according to their requirement for example suppose you want to create a function for add two number then you create a function with name sum() this type of function is called user defined function.
Defining a function.
Defining of function is nothing but give body of function that means write logic inside function body.
Syntax
               
return_type  function_name(parameter)
{
function body;
}
·         Return type: A function may return a value. The return_type is the data type of the value the function returns.Return type parameters and returns statement are optional.
·         Function name: Function name is the name of function it is decided by programmer or you.
·         Parameters: This is a value which is pass in function at the time of calling of function A parameter is like a placeholder. It is optional.
·         Function body: Function body is the collection of statements.
Function Declarations
A function declaration is the process of tells the compiler about a function name. The actual body of the function can be defined separately.
Syntax
return_type  function_name(parameter);
Note: At the time of function declaration function must be terminated with ;.
calling a function.
When we call any function control goes to function body and execute entire code. For call any function just write name of function and if any parameter is required then pass parameter.
Syntax
function_name();
    or 
variable=function_name(argument); 
Note: At the time of function calling function must be terminated with ';'.
Example of Function
               
#include<stdio.h>
#include<conio.h>

void sum(); // declaring a function
clrsct();
int a=10,b=20, c;

void sum()  // defining function
{
c=a+b;
printf("Sum: %d", c);
}
void main()
{
sum();  // calling function
}
Output
               
Sum: 30



Storage Classes in C

Storage Classes in C

Storage class specifiers in C language tells to the compiler where to store a variable (Storage area of variable), how to store the variable, Scope of variable, Default value of a variable (if it is not initialized it), what is the initial value of the variable and life time of the variable.
Storage classes of C will provides following information to compiler.
  • Storage area of variable
  • Scope of variable that is in which block the variable is visible.
  • Life time of a variable that is how long the variable will be there in active mode.
  • Default value of a variable if it is not initialized it.

Type of Storage Class

Storage classes in mainly divided into four types,
  • auto
  • extern
  • static
  • register

Properties of All storage class

TypeStorage placeScopeLifeDefault Value
autoCPU MemorybodyWithin the FunctionGarbage value
staticCPU Memoryfunctionprogram0 (zero)
externCPU MemoryprogramTill the end of the main program.0 (zero)
registerRegister memorybodyWithin the FunctionGarbage value

auto Storage Class

The auto storage class is the default storage class for all local variables. The scope auto variable is within the function. It is equivalent to local variable.

Syntax

{
   int roll;
   auto int roll;
}
In above example define two variable with same storage class auto and their scope is within the function.

Example of auto storage class

#include<stdio.h>
#include<conio.h>

void increment();
void main()
{
increment();
increment();
increment();
increment();
getch();
}
void increment()
{
auto int i = 0 ;
printf ( "%d", i ) ;
i++;
}

Output

Output:
0 0 0 0

static Storage Class

The static storage class instructs the compiler to keep a local variable in existence during the life-time of the program instead of creating and destroying it each time it comes into and goes out of scope.

Example of static storage class

#include<stdio.h>
#include<conio.h>

void increment();
void main()
{
increment();
increment();
increment();
increment();
getch();
}
void increment()
{
static int i = 0 ;
printf ("%d", i ) ;
i++;
}

Output

Output:
0 1 2 3

extern Storage Class

The extern storage class is used to give a reference of a global variable that is visible to ALL the program files. It is equivalent to global variable.

Example of extern storage class

Example

#include<stdio.h>
#include<conio.h>

int x = 20 ;
void main( )
{
extern int y;
printf("The value of x is %d \n",x);
printf("The value of y is %d",y);
getch();
}
int y=30;

Output

The value of x is 20
The value of y is 30

Register variable

Register variables are also local variables, but stored in register memory. Whereas, auto variables are stored in main CPU memory.
Advantages: The register variables are faster than remaining variables, because register variable are stored in register memory not in main memory..
Limitation: But, only limited variables can be used as register since register size is very low. (16 bits, 32 bits or 64 bits).
  • In TC-3.0 we can't access the address of register variables.
  • Pointer are ptr related concepts are can't applied to register variable.

Example

void main()
{
register int a=10;
++a;
printf("\n value of a: %d",a);
printf("Enter a value:");
scanf("%d",&a);
--a;
printf("\n value of a: %d",a);
getch();
}

Output

Input data is 50.
Error, must take address of a memory location.

Explanation

  • In scanf() function if address is provided for the register variable then it will give error, if addition is not provided it normally work.
  • Register storage class specifier just recommended to the compiler to hold the variable in CPU register if the memory is available or else stored in stack area of data segment.

Expression evaluation

Expression evaluation

In c language expression evaluation is mainly depends on priority and associativity.

Priority

This represents the evaluation of expression starts from "what" operator.

Associativity

It represents which operator should be evaluated first if an expression is containing more than one operator with same priority.
OperatorPriorityAssociativity
{}, (), []1Left to right
++, --, !2Right to left
*, /, %3Left to right
+, -4Left to right
<, <=, >, >=, ==, !=5Left to right
&&6Left to right
||7Left to right
?:8Right to left
=, +=, -=, *=, /=, %=9Right to left

Example 1:

images

Example 2:

images

sizeof operator

sizeof operator

The sizeof operator is used to calculate the size of data type or variables. This operator returns the size of its variable in bytes.
For example: sizeof(a), where a is interger, will return 4.

Syntax

sizeof(variable)

Example

#include<stdio.h>
#include<conio.h>

void main()
{
int a;
float b;
double c;
char d;
printf("Size of Integer: %d bytes\n",sizeof(a));
printf("Size of float: %d bytes\n",sizeof(b));
printf("Size of double: %d bytes\n",sizeof(c));
printf("Size of character: %d byte\n",sizeof(d));
getch();
}

Output

Size of Integer: 2
Size of float: 4
Size of double: 8
Size of character: 1

Ternary Operator in C


Ternary Operator in C

If any operator is used on three operands or variable is known as Ternary Operator. It can be represented with ? : . It is also called as conditional operator

 

Advantage of Ternary Operator

Using ?: reduce the number of line codes and improve the performance of application.

Syntax

expression-1 ? expression-2 : expression-3
In the above symbol expression-1 is condition and expression-2 and expression-3 will be either value or variable or statement or any mathematical expression. If condition will be true expression-2 will be execute otherwise expression-3 will be executed.

Syntax

a<b ? printf("a is less") : printf("a is greater");

Flow Diagram





































Find largest number among 3 numbers using ternary operator

Example

#include<stdio.h>
#include<conio.h>
 
void main()
{
int a, b, c, large;
clrscr();
printf("Enter any three number: ");
scanf("%d%d%d",&a,&b,&c);
large=a>b ? (a>c?a:c) : (b>c?b:c);
printf("Largest Number is: %d",large);
getch();
}

Output

Enter any three number: 5 7 2
Largest number is 7