> October 2011 ~ Online tutorial

nested if else statement in c

Nested if else statement
syntax
If(test condition1)

{

If(test condition2);

{

Statement1;

}

Else

{

Statement2

}

Else

{

Statement 3

}

Statement –x



If the test expression can be executed first if it is true then true block can be executed immediately

following if statement are executed otherwise false statement are executed

if neither case they statement x can be executed

Eg:

If(sex is female)

{

If(bal<500O)

Bonus=.05*bal;

Else

Bonus=.02*bal;

Bal=bal+bonus;

Program:

Main()

{

Int a,b,c;

Printf(“enter the value”);

Scanf(“%d%d%d”,&a,&b,&c);

If(a>b)

{

If(a>c)

Printf(“%d”,a);

Else

Printf(“%d”,c);

}

Else

{

If(c>b)

Printf(“%d”,c);

Else

Printf(“%d”,b);

}

}

arguments with no return type

Function with argument and no return type:

#include

#include

void print();

void line(int x,int y);

void main()

{

int a,b;

clrscr();

print();

printf("Enter thevalues");

scanf("%d%d",&a,&b);

line(a,b);

print();

getch();

}

void print()

{

int a;

for(a=0;a<=10;a++)

printf("-");

}

void line(int a,int b)

{

int c;

c=a+b;

printf("%d",c);

}

function types in c

No argument and no return type:

#include

#include

void printline();

void main()

{

int a;

clrscr();

printline();

printf("\nthis is world\n");

printline();

getch();

}

void printline()

{

int i;

for(i=1;i<=40;i++)

printf("-");

}

getch();

Declaration of function in c

Declaration of function:

1.function name

2.function type

3.list of parameter

syntax
Function type function_type(parameter list)

Example


int
mul(int m,int n);

function definition in c

Element of user defined function:

1.function definition

2.function call

3.function declaration


Defintion of function

A function definition ,also known as function implememtation shall include the following elements

1.function name

2.function type


3.list of parameter

4.local variable declaration

5.function statement

6.a return type

Syntax

Function type function_type(parameter list)

{

Local variable declaration;

Executable statement1;

Executable statement2;

Return statement;

}

Example:

void add(int a,int b)
{
int c;
return 0;
}


for above the statement

void          - return type
add          -function name
int a,int b  -parameter

recursive function in c

A multi fuction program
A function is a self contained block of code that perform a particular task.once a function has been designed

and packed .it can treated as a black box that takes some data from main program and return the values

Program:b

#include

Void printline();

Main()

{

printline();

printf(“this world”);

printline();

{

Void printline()

{

int i;

For(i=0;i<40;i++) Printf(“-“); } Getch(); } Output:

---------------------------------------------------------------------

This world

---------------------------------------------------------------------

Explanation:

The above pgm contains two user defined function

Main()function

Printline()function

As we know that the pgm execution can start with the main()during execution of main,the first statement can be encountered as
Printline()
Which indicates that it will be executed first and the main()returen function and again printline()

Can be executed .When this pgm main() can be call the user defined function prinline()

function two times and library function once

function in c

user defined function
Need for user defined function:
As pointed out that main is a specially recongized fuction in c.ever y fuction program must have main function to

indicate where the functionhas to execution has to begin its execution.



copying structures in c






#include<stdio.h>

#include<conio.h>

struct detail

{

char name[10];

int page;

};

void main()

{

int x;

struct detail m1={"anbu",10};

struct detail m2={"kan",20};

x=((m1.name==m2.name)&&(m1.page==m2.page))?1:0;

if(x==1)

{
printf("correct");

}

else

{

printf("bad");

}

getch();

}

Output:

bad

initializing structure variables in c

initializing structure variables in c
Like any other datatype ,a structure variable can be initializes at compil time

Syntax:
main()

{

struct
{
datatype variable;

datatype variable1;

}

student ={60,70,90};

}

This assigns values 60 to student.variabel and 70,90 to student.variable2 .

there is a one to one correspondence between the member and their initializing values

initializing Variabel
struct st_record

{

int wt;

float ht;

}student1={60,70,90};

main()
{
structst_record sstudent2={53,170.60};

}

Explanation
1.1.the keyword struct

2.the structure tag name

3.List terminated semicolon

4.The assignment operator=

Rule of initializing variable
1.we cannot initialize individual member inside the structure template

2.The order of values enclosed in braces must matchthe order of member in

the structure definition

3.zero for integer and floating point number

4.'\0' for character and string




declaring structure variables



Declaring structure variable

after defining a structure format we can declare vraiable of data type.

A structure variabel declaration is similar to the of variable of any other data

type.it includes the following elements

1.the keyword struct
2.the structure tag name

3.List of names separated of commas

For example ,the statement

struct book_bank,book1,book2,book3

declares book1,book2,book3 as variable of type struct book_bank

Declaration:
struct book_bank

{

char title[20];

char author[10;

int pages;

};

struct book_bank,book1,book2,book3;

remember that the member of a structure themselves are not variable .they

don't occupy any memory until they are associated with structure variable as book1

Type Defined structure:
we can use the keyword typedef to define a structure as follow

typedef struct

{


type member1;

type member2;

}type_name;

the type_name represent structure definition assoicated with it and therefore can beused to declare structure variable as shown
type_namevar1,var2;





stucture in c



unlike arrays ,structure must be defined first for their format that may be used later to declare structure variable.

syntax
struct book_bank

{

char title[20];

char author[10];

int pages;

};






Explanation
the keyword struct keyword declares a structure to hold the details of three data fields,namely title,author,pages.

these fields are called structure element or member

general form of structure

struct tag_line

{

data type member1;

data type member2;

};