C Arrays Function
C Arrays Function
locations.
1000
Example
/* Program using arrays */
#include<stdio.h> #include<conio.h>
Variable for indexing
void main() { int arr[5],i; //declaring array variable of size 5 clrscr(); for(i=0;i<5;i++) { arr[i]=i+1; printf(%d,arr[i]); } getch(); Accessing array elements }
Memory mapping
1
arr[0]
2
arr[1]
3
arr[2]
4
arr[3]
5
arr[4]
is
<data type> <variable name>[size1][size2][size3][sizen];
Left Index
Right Index
Example
/* Program for two dimensional array */
#include<stdio.h> #include<conio.h> void main() { int t, i, num[3][4]; for(t=0; t<3; ++t) //Assigning values to the array for(i=0; i<4; ++i) num[t][i] = (t*4)+i+1; for(t=0; t<3; ++t) /* now print them out */ { for(i=0; i<4; ++i) printf(%d,num[t][i]); printf("\n"); } }
Memory mapping
num[t][i]
0 0 1 2 1 2 3
1 5 9
2 6 10
3 7 11
4 8 12
Example
/* Matrix Addition */ #include<stdio.h> #include<conio.h> void main() { int mat1[3][3]; int mat2[3][3]; int mat[3][3]; int i,j,k,l; printf("\nGive the values for 1st Matrix: \n); for(i=0;i<3;i++) { for(j=0;j<3;j++) scanf(%d,&mat1[i][j]); }
printf("\nGive the values for 2nd Matrix:\n); for(i=0;i<3;i++) { for(j=0;j<3;j++) scanf(%d,&mat2[i][j]); } printf("\nResultant: \n); for(k=0;k<3;k++) { for(l=0;l<3;l++) { mat[k][l]=(mat1[k][l]+mat2[k][l]); printf(%d\t,mat[k][l]); } printf(\n); } }
Array Initialization
C allows the initialization of arrays at the time of their
declaration.
Syntax
<data type> <array_name>[size1]. . .[sizeN]={value_list};
Eg:
int i[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
Array Initialization
We can initialize the multi dimensional arrays also.
Eg:
int sqr[3][2] = {1,1,2,4,3,9};
Functions
Function
Function is basic requirement of modular
programming.
The process of dividing a large program into small sub
statements that perform well defined, specific task and may return a value.
Example
/* Program for simple function call */ #include<stdio.h> void main() { message(); //Function Call printf("\nCry, and you stop the monotony!"); } message() //Function { printf("\nSmile,and the world smiles with you..."); }
Types of Function
Pre-defined functions User defined functions
Pre-defined functions
The functions, which has special type of meaning which is
already defined and stored, is called built-in functions or pre-defined functions. Eg: int a=4; x=pow(a,2);
specific task.
Any C program contains at least one function. If a program contains only one function, it must be main( ). If a C program contains more than one function, then one (and
only one) of these functions must be main( ), because program execution always begins with main( ). present in a C program.
After each function has done its thing, control returns to main().
Structure of Function
<return type> <function name>(arg 1, arg 2,,arg n) { declaration part; statements; return; }
Example
int sum(int x,int y) { int result; result = x + y; return (result); }
Properties
Every function has a unique name. This name is used to
Example
#include<stdio.h> void main() { printf("\nI am in main"); italy(); printf("\nI am finally back in main"); } italy() { printf("\nI am in italy"); brazil(); printf("\nI am back in italy"); }
Rules
A function gets called when the function name is followed
by a semicolon.
main() {
func(); }
Rules
A function can be called any number of times.
Rules
The order in which the functions are defined in a program and the order in which they get called need not necessarily be same.
main() { display1() ; display2() ; } display2() { printf("\n }
Rules
A function can call itself. This is known as Recursion. int fact(int n) { if(n==0) return(1); return(n*fact(n-1)); }
Ex:
declarator is called as function definition. The declarator and function prototype declaration should match each other.
Function Call:
A function is called by its name followed by
called actual arguments. The arguments declared in function declarator are called formal arguments. The return statement: The return statement is used to return the value to caller function. The return statement returns only one value at a time.
Syntax : return (variable name);
Function definition
Return statement
Types of Function
A function may belong to any one of the following categories:
1. Functions with no arguments and no return values. 2. Functions with arguments and no return values. 3. Functions with arguments and return values. 4. Function with no arguments and return values.
Types of Function
Functions with no arguments and no return values This is the simplest function ,it does not receive any arguments from the called function and does not return any value from calling function.
Example
#include<stdio.h> void main() { void show(); show(); }
Types of Function
Functions with arguments and no return values This function receives arguments from calling function and does not return value to the calling function.
Example
/* Example for with argument no return value*/ #include<stdio.h> #include<conio.h> void main() { void line(char,int); line(*,20); //function call printf(\n Welcome to KGISL - iTech); line(&,20); //function call getch(); }
void line(char ch,int n) { This function int i; called twice from for(i=0;i<n;i++) main() { printf(%c,ch); } }
Example
#include<stdio.h> #include<conio.h> void add(int x,int y) { int result; result = x+y; printf("Sum of %d and %d is %d.\n\n",x,y,result); } void main() { There is no clrscr(); function prototype add(17,95); //call 1 add(59, 344); //call 2 add(198,7895); //call 3 getch(); }
Types of Function
Functions with arguments and return values This function receives arguments from calling function and returns the computed value back to the calling function.
Example
#include<stdio.h> #include<conio.h> int sum(int,int); //function prototype
void main() { int i,j,k; clrscr(); printf(Enter value of i and j:); scanf(%d%d,&i,&j); k=sum(i,j); //function call
/* function definition for adding two numbers */ int sum(int a,int b) { int c; c = a+b; return c; }
Types of Function
Functions with no arguments and return values
Function Call
There are two ways to call a function.
1.
Call by Value
2. Call by Reference
return 0;
}
return 0;
}
Advantages of Function
Support for modular programming.
Pointers
Pointers
Pointers provide the means by which functions can
Declaration of Pointer
Syntax:
Pointer Operators
There are two special pointer operators: * and &.
1002
2000
Example
/* Program for pointers */ #include<stdio.h> #include<conio.h> void main() { int a,*p; clrscr(); printf("Enter a:); scanf(%d,&a); p=&a; printf(\na: %d \np: %d \nvalue: %d,a,p,*p); getch(); }
Void Pointers
The void type of pointer is a special type of pointer.
In C, void represents the absence of type. So void pointers are pointers that point to a value that has no type.
Example-1
int a,v1; char b,v2; void *ptr; a=100; ptr=&a; v1=*(int*)ptr; //now it s pointing to integer printf(" %d \n ",v1);
Example-2
#include<conio.h> #include<stdio.h> void display(int x,void *y) { int a; char b; if(x==1) { a=*(int*)y; printf("Integer Value %d \n ",a); } if(x==2) { b=*(char*)y; printf("Character Value %c \n ",b); } }
void main() { int s1; char s2; void *ptr1; clrscr(); s1=300; ptr1=&s1; display(1,ptr1); //function call with int value s2='J'; ptr1=&s2; display(2,ptr1); //function call with char value getch(); }
Pointer Expression
Assignment
int var,*p1,*p2; p1=&var; p2=p1; In this pointers p1 and p2 points to the same variable var. Arithmetic int var,*p; p=&var; p++; // or p--; In this pointer moves to the next memory location
int *ptr; int sample[5]; ptr=sample; It can also use & operator to specify the address of 1st element. ptr=&sample;
Note: Pointers can be arrayed like any other data type.
Example-1
#include<stdio.h> void main() { int *p,i=0; int x[5]={5,9,6,3,7}; p=x; //initializing with base address printf(Element\tValue\tAddress\n\n); while(i<5) { printf(x[%d]\t%d\t%u\n,i,*p,p); i++,p++; } }
#include<stdio.h> void main() { int a[4],v1,v2,*ptr; a[0]=100; a[1]=200; a[2]=300; a[3]=400; ptr=a; printf(" \n %d ",*ptr); ptr++; printf(" \n %d ",*ptr); v1=*(ptr+1); printf(" \n %d ",v1); ptr++; v2=*(ptr-1); printf(" \n %d ",v2); }
Example-2
Multiple Indirection
Multiple indirection or pointers to pointers is a pointer