Array 0 Types of Functiongs
Array 0 Types of Functiongs
(run time)
#include<stdio.h>
int main()
{
int marks[6],i;
printf("\n enter the elements of the array:");
for(i=0;i<6;i++) //if we give scanf for 6 times it goes very big thats y we r using for
loop
{
scanf("%d",&marks[i]);
}
printf("the elements of the array are:");
for(i=0;i<6;i++)
{
printf("\n %d",marks[i]);
}
}
Two Dimensional Array
[row_size][column_size] =[3][3]
no of rows * no of columns=3*3=9
1.Declaration Of Array
syntax: datatype arrayname[row_size][column_size];
example: int A[3][3];
[3]--> 0 1 2
[3]--> 0 1 2
[0][0]-1
[0][1]-2
[0][2]-3
[1][0]-4
[1][1]-5
[1][2]-6
[2][0]-7
[2][1]-8
[2][2]-9
2.initialization of array
syntax: datatype arrayname[row_size][column_size]={{ }};
example: int A[3][3] ={{1,2,3},{4,5,6},{7,8,9}};
Functions
A function is a block of code that performs a specific task. You can use them to divide
large problems into smaller, more manageable pieces of code. You can declare and
define functions in C, and pass parameters either by value or by reference.
Uses:
1.reduces the complexibility
2.code reusability
3.easy debugging
Types Of Functions
1.pre defined function – built in
2.User-Defined Functions – user
PREFINED FUNCTIONS
1.Output function-printf
Syntax: Printf (“text to be displayed:”);
//if we r using a variable,then we must write it with a format specifier otherwise error exists
Syntax: Printf(“format specifier”,variable name);
Example: printf(“%d”,a);
2.Input Function-scanf
Syntax: Printf(“format specifier”,&variable name);
Example: printf(“%d”,&a);
#include <stdio.h>
void swap(int* ,int*);
main()
{
int a,b;
a=10;
b=20;
swap(&a,&b); //in main(),we r passing the address and to get the value of
address,we r using *
printf(" before swapping a & b is :%d %d",a,b);
}
void swap(int*a,int*b)
{
int temp;
temp=*a; // temp = a
*a=*b; // a=20
*b=temp; //b=10
printf("after swapping a & b is :%d %d",*a,*b);
}
0/P:
after swapping a & b is : 20 10
before swapping a & b is :20 10
NOTES:
since we are passing the address of the actual parameter to the formal parameter , changes
made in the formal parameter affects the actual parameter
POINTERS:
pointers hold the address of another variable
pointer datatype should be same as variable datatype
*--deferencing/value (refers to the value present in the address of the another variable)
&--address
#include<stdio.h>
int main()
{
int a=2;
int *p;
p=&a;
printf("a=%d\n",a); //a=2
printf("&a=%d\n",&a); //&a=6487572
printf("p=%d\n",p); //&a=6487572
printf("*p=%d\n",*p); //*p = *(&a) =2
}
O/P:
a=2
&a=6487572
p=6487572
*p=2
STRINGS
It is a combination/sequence of characters surrounded by double quotes (everything on
keyboard is called string). it is terminated by null character '\0' so 1 size should be given
more.string.strings are implemented by character arrays.
EG: char array[6]={"hello"};
A character constant is a single character and it wont terminate with null so no need to give
one more size
EG :char array[5]={'h','e','l','l','o'};
1.Declaration Of Char Array
Syntax :datatype arrayname [size];
Example :char array[6];
2. Initialization (compile time)
char array[6]={"hello"};
To access individual array elements, we use index numbers
STRUCTURES
structure is a userdefined datatype which has a combination of data items with different
datatype.it should terminate with semi colon.
structure members occcupy continuous memory location (next-next storage la values store
aagum for eg: now 20 means next 21 la store aagum).
syntax:
structure can be created anywhere even after the after the main() but generally it, is created
before main() i.e after header file
struct is a keyword for declaring a structure
#include<stdio.h>
struct structure_name{
body of structure(structure members)
};
Int main()
{
struct structure_name structure_variable;
(to access structrure members)
structure_variable.structure_member=value;
}