You are on page 1of 13

ARRAY

It Is A Collection Of Elements Of Similar Data Type


[]-Subscript
[size]-one dimensional array
1.Declaration Of Array
syntax: datatype arrayname[size];
example: int marks [6];
2.initialization of array
syntax: datatype arrayname[size]={element1,element2...element6};
example: int marks[6]={99,98,100,97,95,93};
index = 0 1 2 3 4 5
Example
To print a single value
#include<stdio.h>
int main()
{
int marks[6]={99,98,100,97,95,93};
printf("%d",marks[4]); //general form variablename[index]
}
To Print All 6 Values
printf("%d%d%d%d%d%d",marks[0],marks[1],marks[2],marks[3],marks[4],marks[5]);
// since it is very big we r going do it in a simple way using for loop(compile time)
#include<stdio.h>
int main()
{
int i;
int marks[6]={99,98,100,97,95,93};
for(i=0;i<6;i++)
{
printf("%d \t",marks[i]);
}
}

(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}};

(compile time)- refers to declaration and initialization in same line


#include<stdio.h>
int main()
{
int i,j;
int A[3][3] ={{1,2,3},{4,5,6},{7,8,9}};
for(i=0;i<3;i++) //row
{
for(j=0;j<3;j++) //column
{
printf("%d",A[i][j]);
}
printf("\n");
}
}
O/P:
123
456
789
(run time)
#include<stdio.h>
int main()
{
int A[3][3],i,j;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("elements of[%d][%d]:",i,j);
scanf("%d",&A[i][j]);
}
}
printf("the matrix is given below :");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d \t ",A[i][j]);
}
printf("\n");
}
}
O\P:
elements of[0][0]:1
elements of[0][1]:2
elements of[0][2]:3
elements of[1][0]:4
elements of[1][1]:5
elements of[1][2]:6
elements of[2][0]:7
elements of[2][1]:8
elements of[2][2]:9
the matrix is given below
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);

USER DEFINED FUNCTIONS


Syntax:
1.function declaration - above main()

function declaration is also called as function prototype


Syntax:
return type_function_name(parameters);
Parameter- input values given to function
Actual parameter- is in function call
Formal parameter – is in function definition

2.function call - writing inside main()


Syntax:
function_name(parameters); //reference to function definition

3.function definition -outside the main()


Syntax:
return type_function_name(parameters) - no semicolon
{
logic;
return;
}
TYPES OF USER DEFINED FUNCTIONS
1.WITHOUT PARAMETER WITHOUT RETURN
Void - Returns No Data Type
EG:Printing Hello World Using Functions
#include<stdio.h>
void display();
main()
{
display(); //reference to function definition
}
void display()
{
printf("hello world");
}
2.WITHOUT PARAMETER WITH RETURN
#include<stdio.h>
int add();
main()
{
int answer =add(); // in int datatype 9 is assigned as variable name(answer)
printf("the expected output is %d",answer);
}
int add()
{
int a,b,sum; //no parameters given so we have to get the input in function definition
a=5;
b=4;
sum=a+b;
return sum; //return to add();
}
3.WITH PARAMETER WITH RETURN
#include<stdio.h>
int add(int,int); // only dt
main()
{
int a,b,sum; // since we have given parameter, we have to get the input in function
call
a=5;
b=7;
sum = add(a,b); //no need datatype,only variable name
printf("the expected output is %d",sum);
}
int add(int a,int b) // we have to give dt with variable
{
int result;
result=a+b;
return result;
}
4.WITH PARAMETER WITHOUT RETURN
#include<stdio.h>
void add(int,int); //void becoz no return
main()
{
int a,b,sum; //in this type, definition itself print the value instead of function call
a=9;
b=7;
add(a,b);
printf(“bye”); //if suppose some printf is there after function call,then it first prints
the function definition and this
}
void add(int a,int b)
{
int result;
result=a+b;
printf("the expected output is %d",result);
}
O/P:
The expected output is 16
bye

PARAMETER PASSING IN C PROGRAM AND THE TYPES OF DOING IT


1.Call By Value(Pass by value)
Value Of Actual Parameters Is Copied In Formal Parameter
Example: swapping of two numbers a=10, b=20
#include <stdio.h>
void swap(int,int);
main()
{
int a,b;
a=10;
b=20;
swap(a,b); //actual
printf("before swaping of a & b is :%d %d",a,b);
}
void swap(int a,int b) //formal
{
int temp;
temp=a; // temp = 10
a=b; // a=20
b=temp;//b=10
printf("after swaping of a & b is :%d %d",a,b);
}

2.Call By Reference (Pass By Reference)


Passing Address Of Actual Parameter To Formal Parameter
&-address
*(copy) - used to get the value of a particular address
Example: swapping of two numbers a=10, b=20

#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

To Read And Print The String in Run Time Initialization


#include<stdio.h>
main()
{
char array[100];
printf("enter an array:");
scanf("%s",array); // in string ,& is optional
printf("the entered string is %s",array);
}
O/P:
enter an array: hello
the entered string is hello

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;
}

You might also like