FUNCTIONS
———
Function:
———
It is a self contained block of statements and is used several
times in a program but defined only once.
library function(or)predefined functions:-
—————————————–
The functions which are inbuilt with the compiler is called as
library function.
ex:- printf(),scanf(),getch(),…..etc
User defined functions:-
————————
User can defined functions to do a task relavent to their program.
such functions are called user defined functions.
Any function has 3 things.
1) Function declaration
2) Function definition
3) Function calling.
In case of library functions the function declaration is in headers,
definition is in C libraries and calling is in your source program.
In case of user defined functions all the three things are in your
source program.
Function declaration:-
———————
syn:- returntype func_name([arg_list]);
ex:- void sum(int,int);
Function definition:-
——————-
syn:- returntype func_name([arg_list])
{
body;
}
Function calling:-
—————-
syn:- func_name([arg_list]);
ex:- sum(a,b);
NOTE:- The arguments which are given at the time of function declaration and
function definion are called arguments (or) formal arguments.
The arguments which are given at the time of function calling
are known as parameters (or) actual arguments.
Void :- Empty data type.
RULES FOR CREATING AND ACCESSING USER DEFINED FUNCTIONS:
——————————————————-
1.A function can be called by any no of times.
2.A function may or may not return a value.
3.A function may or may not receive arguments
4.If a function does not return any value,the function will be specified as
void.
5.If a function returns a value only one value can be returned.
6.If a function returns a value the returning value must be returned with a
statement “return”.
7.If a function returns a value the execution of return statement should be
last.
8.A function returns an integer value by default;
9.If a function returns a value , the returning value should be match with
the function return data type.
10.A function is defined after (or) before main function.
11.Before calling a function , the function declaration (or) definition
is must and should.
12.If a function declaration is specified before the function call,The function
fn definition can be specified any where in the program.
13.If a function definition is specified before the function call,then the
function declaration is not necessary.
14.A function is executed when the function is call by its name.
15.The function defintion should not be terminated with semicolon.
RETURN:-
——-
Exits immediately from the corresponding executing function to the
calling. return Optionlly returning a value.
syntax:- return [<expression>];
Function prototypes (or) Function categories:-
———————————————-
A Function depending on whether arguments are present (or) not and whether
a value is returning or not .They belong to one of the following categories.
1)Function with no arguments and no return value
2)Function with arguments and no return value.
3)Function with arguments and return value.
4)Function with no orguements and return value.
Recursive function:-
——————-
Calling a function with in same function is known as recursion (or)
Recursive function.If we want to work with recursive function we must follow
the two conditions.
1. Termination condition.
2. Calling by itself.
gotoxy():-
———-
gotoxy moves the cursor to the given position in the current text window.
Declaration: void gotoxy(int x, int y);
here x=columns y=rows
IN textmode 25 rows and 80 columns are there.
_setcursortype():-
——————
Selects cursor appearance
Declaration: void _setcursortype(int cur_t);
Remarks:
Sets the cursor type to one of the following:
þ _NOCURSOR (turns off the cursor)
þ _SOLIDCURSOR (solid block cursor)
þ _NORMALCURSOR (normal underscore cursor)
kbhit():-
———–
Checks for currently available keystrokes
Declaration: int kbhit(void);
delay():- <dos.h>
——–
It suspends execution for an interval(milli seconds).
syn:- void delay(unsigned milliseconds);
gets:-
It gets a string from stdin.
declaration:- char * gets(char *s);
puts:-
It outputs a string to stdout and appends a new line character.
declaration:- int puts(const char *s);
getch():-
It gets a character from console but does not echoes to the screen.
declaration:- int getch(void);
getche():-
It gets a character from console but it echoes to the screen.
declaration:- int getche(void);
getchar():-
getchar is a macro that gets a character from stdin
Declaration:
int getchar(void);
putchar():-
It is a macro that outputs a character on stdout
Declaration:
int putchar(int c);
STRING HANDLING FUNCTIONS <string.h>
————————-
1) strlen():-
It calculates the length(no of characters) of the given string.
declaration:-
size_t strlen(const char *s);
2)strrev():-
It reverses the all characters in the given string(except terminating
null character ( ).
declaration:-
char *strrev(char *s);
3)strcpy():-
It copies all characters in the given string into destination string
declaration:-
char *strcpy(char *dest,const char *src);
4)strcat():-
Appends one string to another.
Declaration:
char *strcat(char *dest, const char *src);
5)strlwr():-
It converts all characters in given string into lowercase.
declaration:-
char *strlwr(char *s);
6)strupr():-
It converts all characters in given string into uppercase.
declaration:-
char *strupr(char *s);
7)strncpy():-
It is used to copy specified no of characters into destination string.
declaration:-
char *strncpy(char *dest, const char *src, size_t maxlen);
8)strcmp():-
It compares two strings with case sensitivity.
declaration:-
int strcmp(const char *s1, const char*s2);
9)stricmp():-
It is a function which is used to compare two strings without
case sensitivity.
declaration:-
int strcmpi(const char *s1, const char *s2);
10)strcmpi():-
It is a macro which is used to compare two strings without
case sensitivity.
declaration:
int stricmp(const char *s1, const char *s2);
Return Value:
These routines return an int value that is
< 0 if s1 < s2
== 0 if s1 == s2
> 0 if s1 > s2
Two dimensional character arrays:-
———————————
A list of names can be treated as a table of strings and two-dimensional
character array can be used to store the entire list.
For example:-
char st[20][20] may be used to store a list of 20 strings,each of
length mot more than 20 characters.
ARRAYS
——
Definition:-
A Group of data items of same data type stored in a continuous
Memory allocation and it can be represented by a single identifier.
That means a group of data items that share a common name. A
Perticular value is indicated by writing a number called index(or)subscript
in square braces( [] ) after the array name.
C supports 3 types of arrays. They are
1.single dimensional arrays (or) One dimensional arrays.
2.Two dimensional arrays (or) double dimensional arrays.
3.Multi dimensional arrays.
1.Single dimensional arrays:-
A list of items can be given one variable name using only one index.such
a variable is called single dimensional arrays.
In single dimensional array the elements are represented one after the
Other in edges of Memory bytes.
syn:- data type arr_name[size];
ex:- int a[5];
here elements are:- a[0],a[1],a[2],a[3],a[4];
In any array the array index is 0 to n-1.
Initialization:-
syn:- data type arr_name[]={val-1,val-2,val-3,……..val-n};
ex:- int a[]={1,2,3,4,5,6,……};
2.Two dymensional arrays :-
A two dimensional array can store a table of values which contains rows
and columns . In two dimensional arrays we use two index values.One for
Rows and another for columns.
Declaration:- data type arr_name[row_size][col_size];
ex:- int a[2][3];
here elements:- a[0][0] a[0][1] a[0][2]
a[1][0] a[1][1] a[1][2]
Initialization :-
Form:1:
datatype arr_name[rowsize][colsize]={val-1,val-2,………,val-n};
ex:- int a[2][3]={1,2,3,4,5,6};
Form:2:
datatype arr_name[rowsize][colsize]={ {val-1,val-2,……},
{val-1,val-2,…val-n},{val-1,val-2,….val-n}………..};
ex:- int a[2][3]={ {1,3,4},{2,3,4} };
3.Multi Dimensional arrays:-
C supports arrays of 3 or more dimensions.In multi dimensional
arrays we use more than two index values.
syn:- data type arr_name[s1][s2][s3]……[sn];
where s1,s2,s3,……sn are the sizes of the array.
ex:- int a[2][2][3];
here elements are:-
a[0][0][0] a[0][0][1] a[0][0][2]
a[0][1][0] a[0][1][1] a[0][1][2]
a[1][0][0] a[1][0][1] a[1][0][2]
a[1][1][0] a[1][1][1] a[1][1][2]
Program 1:
/* display */
#include<stdio.h>
#include<conio.h>
void main()
{
int i;
clrscr();
for(i=1;i<=100;i++)
display(i);
getch();
}
display(int n)
{
printf(“%d\t”,n);
}
Program2:
/* gets(),puts() */
#include<stdio.h>
#include<conio.h>
void main()
{
char name[20];
clrscr();
printf(“Enter your name:”);
gets(name);
clrscr();
puts(“Hello!…”);
puts(name);
getch();
}
Program 3:
/* order(),reverse() */
#include<stdio.h>
#include<conio.h>
order()
{
int i;
for(i=1;i<=10;i++)
printf(“%d\n”,i);
getch();
}
reverse()
{
int i;
for(i=10;i>=1;i–)
printf(“%d\n”,i);
getch();
}
void main()
{
clrscr();
printf(“The Numbers in Assending Order:\n”);
order();
printf(“The numbers in Desseding Order:\n”);
reverse();
getch();
}
Program 4:
/* square value */
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf(“\n enter any no:”);
scanf(“%d”,&a);
b=square(a);
printf(“\n square of %d is %d”,a,b);
getch();
}
square(int x)
{
int y;
y=x*x;
return(y);
}
Program 5:
/* passing values between functions */
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c,sum;
printf(“\n Enter any 3 no:”);
scanf(“%d %d %d”,&a,&b,&c);
sum=calsum(a,b,c);
printf(“\n sum=%d”,sum);
getch();
}
calsum(x,y,z)
int x,y,z;
{
int d;
d=x+y+z;
return(d);
}
Program 6:
/* string functions strcat() */
#include<stdio.h>
#include<conio.h>
void main()
{
int a;
char s1[20],s2[20];
clrscr();
printf(“Enter first string:”);
gets(s1);
printf(“Enter second string:”);
gets(s2);
a=strcat(s1,s2);
puts(a);
getch();
}
Program 7:
/*strcmp() */
#include<stdio.h>
#include<conio.h>
void main()
{
char string1[]=”ABC”;
char string2[]=”BBC”;
int i;
clrscr();
i=strcmp(“string1,string2″);
printf(“%d\n”,i);
getch();
}
Program 8:
/* strcpy() */
#include<stdio.h>
#include<conio.h>
void main()
{
char s1[]=”meher”;
char s2[20];
clrscr();
strcpy(s2,s1);
puts(s1);
puts(s2);
getch();
}
Program 8:
/* strlen() */
#include<stdio.h>
#include<conio.h>
void main()
{
int l;
char name[20];
clrscr();
printf(“Enter a string:”);
gets(name);
l=strlen(name);
puts(name);
printf(“The length of a string a is:%d”,l);
getch();
}
Program 9:
/* strrev() */
#include<stdio.h>
#include<conio.h>
void main()
{
int a;
char st[20];
clrscr();
printf(“Enter any string:”);
gets(st);
a=strrev(st);
puts(a);
getch();
}
Program 10:
/* strupr(),strlwr()*/
main()
{
int a,b;
char s1[20],s2[20];
clrscr();
printf(“Enter 1st string:”);
gets(s1);
printf(“Enter 2nd string:”);
gets(s2);
a=strupr(s1);
b=strlwr(s2);
puts(a);
puts(b);
getch();
}
Program 11:
/* USER DEFINED FUNCTIONS */
#include<stdio.h>
#include<conio.h>
meher()
{
int i;
for(i=0;i<=40;i++)
printf(“*”);
}
void main()
{
clrscr();
gotoxy(15,6);
meher();
gotoxy(26,7);
printf(“CH.MEHER BABU”);
gotoxy(26,8);
printf(“Door no:9-32-9″);
gotoxy(26,9);
printf(“kothapet”);
gotoxy(26,10);
printf(“Vijayawada-520001″);
gotoxy(15,11);
meher();
getch();
}
Program 12:
/* print 1 to 10 numbers using with single diamention array */
#include<stdio.h>
#include<conio.h>
void main()
{
int all[10];
int i;
clrscr();
/*string the values into array */
for(i=1;i<=10;i++)
{
printf(“Enter the cell value:”);
scanf(“%d”,&all[i]);
}
/* printf the values of an array */
for(i=1;i<=10;i++)
{
printf(“%d\t”,all[i]);
getch();
}
Program 13:
/* print small,capital,symbols using with single diamention array*/
#include<stdio.h>
#include<conio.h>
void main()
{
int i;
char st[256];
clrscr();
for(i=0;i<256;i++)
{
st[i]=1+i;
}
for(i=0;i<256;i++)
{
printf(“%c\t”,st[i]);
}
getch();
}
Program 14:
/* call by value using with single diamention array */
#include<stdio.h>
#include<conio.h>
void main()
{
int i;
int marks[]={56,65,75,56,78,90}
clrscr();
for(i=0;i<=6;i++)
{
display(marks[i]);
}
display(int m);
getch();
}
Program 15:
/*conversion of numbers to text */
#include<stdio.h>
#include<conio.h>
void main()
{
int a[3],i;
clrscr();
for(i=0;i<=3;i++)
{
printf(“Enter any no:”);
scanf(“%d\t”,&a[i]);
for(i=0;i<=3;i++)
printf(“%c\t”,a[i]);
}
getch();
}
Program 16:
#include<stdio.h>
#include<conio.h>
void main()
{
int arr[5][5];
int i,j;
clrscr();
for(i=1;i<=5;i++)
for(j=1;j<=5;j++)
{
if((i+j)==6)
arr[i][j]=9;
else
arr[i][j]=1;
for(i=1;i<=5;i++)
{
for(j=1;j<=5;j++)
printf(“%d\t”,arr[i][j]);
printf(“\n”);
} }
getch();
}
Program 17:
/* print lowercase letters using with single diamention array */
#include<stdio.h>
#include<conio.h>
void main()
{
int i;
char st[26];
clrscr();
for(i=0;i<26;i++)
{
st[i]=97+i;
}
for(i=0;i< 26;i++)
{
printf(“%c\t”,st[i]);
}
getch();
}
Program 18:
/* password program */
#include<stdio.h>
#include<conio.h>
void main()
{
char passwd[10]=”meher”;
char tpasswd[10];
clrscr();
printf(“\n Enter pass word:”);
fflush(stdin);
scanf(“%s”,tpasswd);
if(strcmp(tpasswd,passwd)!=0)
{
printf(“\n wrong pass word:”);
exit(0);
}
getch();
}
Program 19:
/*Sorting */
#include<stdio.h>
#include<conio.h>
void main()
{
int a[20],n,i,j,t;
clrscr();
printf(“Enter no of elements :”);
scanf(“%d”,&n);
printf(“Enter array elements :”);
for(i=0;i<n;i++)
{
scanf(“%d”,&a[i]);
}
printf(“\nGiven array elements :”);
for(i=0;i<n;i++)
{
printf(“\t%d”,a[i]);
}
/* sorting */
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
printf(“\nafter sorting :\n”);
for(i=0;i<n;i++)
{
printf(“\t%d”,a[i]);
}
getch();
}
Program 20:
/*searching*/
#include<stdio.h>
#include<conio.h>
void main()
{
int a[20],i,n,se,ck=0;
clrscr();
printf(“Enter no elements :”);
scanf(“%d”,&n);
printf(“Enter array elements :”);
for(i=0;i<n;i++)
{
scanf(“%d”,&a[i]);
}
printf(“Given array elements :\n”);
for(i=0;i<n;i++)
{
printf(“\t%d”,a[i]);
}
printf(“\nEnter searching element :”);
scanf(“%d”,&se);
/* searching */
for(i=0;i<n;i++)
{
if(a[i]==se)
{
ck=1;
printf(” %d is found at a[%d] “,se,i);
}
}
if(ck==0)
printf(” %d is not found “,se);
getch();
}