Unit3 Notes
Unit3 Notes
Unit3 Notes
ARRAYS
C supports a derived data type known as array that can be used to
handle large amounts of data(multiple values) at a time.
Definition
Or
Advantages of arrays
1. It is capable of storing many elements at a time.
2. It allows random access of elements.
Disadvantages of arrays
1. Predetermining the size of array is must
2. Memory wastage will be there.
Types of arrays
Arrays can be mainly three types one dimensional, two dimensional and multi
dimensional arrays.
One‐Dimensional Arrays
Declaration of an Array
data-type variable-name[size/length of array];
For example:
int arr[10];
3 : Initialization without Size: The array size may be omitted. In such cases, the
compiler allocates enough space for all initialized elements.
Ex: int counter[ ]={1,1,1,1,1};
will declare the counter array to contain five elements with initial values 1.
You can access elements of an array by indices/index. You can use array
subscript (or index) to access any element stored in array. Subscript starts with 0,
which means array_name[0] would beused to access first element in an array.
In general array_name[n-1] can be used to access nth element of an array. where
n is any integer number.
Ex:
int a[ ]={10,20,30,40 }; → here array size is 4.
To read one dimensional array:
for(i=0;i< size;i++)
scanf(“ % d”, &a[i]);
To print one dimensional array
for(i=0;i< size;i++)
printf(“ % d”, a[i]);
Two‐Dimensional Arrays
data_type array_name[row_size][col_size];
Example
int a[3][4];
Initialization of 2D Array
int arr[4][3]={{1,2,3},{2,3,4},{3,4,5},{4,5,6}};
Accessing Two-Dimensional Array Elements
int test[2][3][4] = {
};
DYNAMIC ARRAYS
An array created at compile time by specifying size in the source code has
a fixed size and cannot be modified at run time. The process of allocating memory
at compile time is known as static memory allocation and the array created at
compile time are called static arrays.
In C it is possible to allocate memory to arrays at run time. This feature is
known as dynamic memory allocation and the array created at run time are called
as dynamic arrays. The dynamic arrays are created using pointer variables and
memory management allocation functions malloc(), calloc() and realloc().
FUNCTIONS
Types of Functions
1). System defined functions: These are also called as built in or
library functions. They are defined by C compiler and included in header
files.
Ex: pow() //power function
sqrt() //square root function.
printf(), scanf() and string functions etc.
2). User defined functions: these are defined by the user. User can include
any number of functions in the program, for which declaration and definition must
be provided.
Structure of a function
Return-type function-name(arguments list….)
{
local declarations
……..
…..code…
……..
}
example:
void sum (int,int); /*function declaration (prototype)*/
main()
{
sum(10,20); /*calling function*/
}
void sum(int x,int y) /*function definition*/ /* Called function */
{
printf(“sum=%d”,x+y);
}
Function prototype: Declaration of a function can be called as function prototype.
This gives the clear picture of the function i.e which type of arguments it
takes and how many arguments and what will be return value with the function
name.
Syntax: Return-type function-name(arguments list….);
Advantages :
• It is easy to use.
• It represents compact programming structures.
Disadvantages :
• It is slower than that of looping statements because each time function is
called.
void recurse()
{
……
recurse();//calling self function
……
}
int main()
{
……
recurse();//calling recursion function
……
}
void main()
{
int fact=0;
clrscr();
fact = factorial(5);
printf("\n factorial of 5 is %d", fact);
getch();
}
OUTPUT
factorial of 5 is 120
fact = 5 * factorial(4)
= 5 * 4 * factorial(3)
= 5 * 4 * 3 * factorial(2)
= 5 * 4 * 3 * 2 * factorial(1)
=5*4*3*2*1
= 120
STORAGE CLASSES
In C language, each variable has a storage class which is used to define scope and
life time of a variable. Storage classes in C are used to determine the lifetime,
visibility, memory location, and initial value of a variable.
There are four storage classes in C programming
1. auto
2. register
3. static
4. extern
Or
In “C‟ language the group of characters, digits, and symbols enclosed within
double quotation ( " " ) marks are called as string otherwise a string is an array of
characters and terminated by NULL character which is denoted by the escape
sequence “\0‟.
Declaration of String: C does not support string as a data type. However, it allows
us to represent strings as character arrays. In C, a string variable is any valid C
variable name and it is always declared as an array of characters.
The general form of declaration of a string variable is :
Syntax: char string_name[size];
The size determines the number of characters in the string name.
Note: In declaration of string size must be required to mention otherwise it gives
an error.
Ex: char str[]; // Invalid
char str[0]; // Invalid
char str[-1]; // Invalid
char str[10]; // Valid
char a[9]; //Valid
Using this declaration the compiler allocates 9 memory locations for the variable a
ranging from 0 to 8.
C O M P U T E R \0
0 1 2 3 4 5 6 7 8
R A M A \0 \0 \0 \0 \0 \0
0 1 2 3 4 5 6 7 8 9
I / O Functions
Inpu Outpu
t t Inpu Outpu
t t
scanf( print(
) ) getc() putc(
)
fscanf( fprintf(
) ) getchar( putchar(
) )
gets( puts(
) )
getch()
getche(