Escolar Documentos
Profissional Documentos
Cultura Documentos
Initialisation
String variables
User-defined functions
Example C programs
CDS 1
Unit II – Arrays, Strings & Storage classes
FUNCTIONS
The advantages of using functions are
• Easy to write a small function and correctly
• Easy to read, write and debug a function
• Easier to maintain or modify such a function
• Small function tend to be a self documenting and
highly readable.
• A function can be called any number of times in
any place with different parameters
main ( ) {
printf ("programming in C is easy");
}
This program when executed would print the
message "programming in C is easy".
CDS 2
Unit II – Arrays, Strings & Storage classes
my_message ( ){
printf ("programming in C is easy");
}
main ( ){
my_message ( );
}
CDS 3
Unit II – Arrays, Strings & Storage classes
CDS 4
Unit II – Arrays, Strings & Storage classes
CDS 5
Unit II – Arrays, Strings & Storage classes
CDS 6
Unit II – Arrays, Strings & Storage classes
CDS 7
Unit II – Arrays, Strings & Storage classes
CDS 8
Unit II – Arrays, Strings & Storage classes
Example
add (x, y)
int x, y;
{
int t;
t = x + y;
return (t);
}
A function may even have more than one return
statement like
if (a > 100)
return (1);
else
return (2);
The function, which returns a value, can be called
from the main program as
main ( ){
int a;
a = add (10,20);
printf ("%d, a);
}
The function is called at a = add (10,20) and control
is transferred to the function with the actual values,
then it is executed, after execution the sum of two
numbers is returned using the return statement back
to the main program and the return value is assigned
to the integer variable a.
CDS 9
Unit II – Arrays, Strings & Storage classes
Types of functions
Functions are classified according to the presence of
arguments and return values:
(i) Function with no arguments and no return values:
That means there is no data transfer between the
calling function and the called function.
Example:
Function_a( ) function_b( )
{ {
S1; ---
S2; ---
... ---
function_b ( ) }
...
}
function_a is calling function with no arguments and
there is no return statement in function_b.
CDS
10
Unit II – Arrays, Strings & Storage classes
CDS
11
Unit II – Arrays, Strings & Storage classes
CDS
12
Unit II – Arrays, Strings & Storage classes
CDS
13
Unit II – Arrays, Strings & Storage classes
Function Prototypes
ANSI standard compiles the function definition and
arguments declaration in one line, with the function
header.
Example
float mul (float a, float b)
we cannot write it as
float mul (float a, b)
CDS
14
Unit II – Arrays, Strings & Storage classes
CDS
15
Unit II – Arrays, Strings & Storage classes
CDS
16
Unit II – Arrays, Strings & Storage classes
CDS
17
Unit II – Arrays, Strings & Storage classes
Nesting of Functions
Example:
main ( ){
---
---
f1( );
---
---
}
f1( ){
---
---
f2 ( );
---
---
}
f2 ( ){
---
---
}
Note that main ( ) has called f1( ), which calls f2( ).
CDS
18
Unit II – Arrays, Strings & Storage classes
Recursion
The function, which calls itself again and again
directly or indirectly, is called recursive function and
this technique is known as recursion.
Examples:
main ( ){
printf ("I am in an infinite loop");
main ( );
}
CDS
19
Unit II – Arrays, Strings & Storage classes
CDS
20
Unit II – Arrays, Strings & Storage classes
Exercises
(1) The following function returns the value of x/y.
ratio (x, y)
float x, y;
{
return (x/y);
}
What will be the value of the following function
calls?
(a) ratio(10,2) (b) ratio(9,2) (c) ratio(4.5, 1.5)
CDS
21
Unit II – Arrays, Strings & Storage classes
STORAGE CLASSES
Variables in ‘C’ language differ in behaviour from
those in most other languages, for instance in most
other languages, the variables retain their value
throughout the program.
CDS
22
Unit II – Arrays, Strings & Storage classes
Automatic variables
They are declared inside a function.
CDS
23
Unit II – Arrays, Strings & Storage classes
Examples
main ( ) {
auto int n;
---
---
}
main ( ) {
int n = 100;
func1( );
printf(“n in main is %d,” n);
}
func1( ){
int n = 10;
printf(“n in func1 is %d, n);
}
func2( ) {
int n = 1000;
func1( );
printf(“n in func2 is %d”, n);
}
The output of the above program is:
10
1000
100
CDS
24
Unit II – Arrays, Strings & Storage classes
CDS
25
Unit II – Arrays, Strings & Storage classes
External variables
Variables that are alive and active throughout the
entire program are known as external variables or
global variables.
Example
int n = 100;
float m = 10.5; global or external variables
main ( ) {
---
---
}
f1 ( ){
---
---
}
f2 ( ){
}
CDS
26
Unit II – Arrays, Strings & Storage classes
Example
int n;
main ( ) {
n = 10;
---
---
}
f1 ( ){
int n = 0;
---
---
n = n+1;
}
CDS
27
Unit II – Arrays, Strings & Storage classes
Example
int n;
main ( ) {
n = 10;
printf(“n = %d \n”, n);
printf(“n = %d \n”, f1( ));
printf(“n = %d \n”, f2());
}
f1( ){
n = n+10;
return (n);
}
f2 ( ){
int n;
n = 1;
n + +;
return (n);
}
The output of this program is
n = 10
n = 20
n=2
The global variables are visible only from the point
of declaration to the end of the program.
Consider the program fragment:
main ( ){
y = 100;
---
---
}
int y;
CDS
28
Unit II – Arrays, Strings & Storage classes
f1( ){
y = y+1;
}
External declaration
In the above program segment, we have seen that
main( ) cannot access the variable y since it was
declared after main( ).
CDS
29
Unit II – Arrays, Strings & Storage classes
CDS
30
Unit II – Arrays, Strings & Storage classes
Static variables
The value of static variables persists until the end of
the program.
CDS
31
Unit II – Arrays, Strings & Storage classes
CDS
32
Unit II – Arrays, Strings & Storage classes
static-fun ( ) {
static int x = 0;
x + = 3;
printf (“x = %d \n”, x);
}
REGISTER VARIABLES
The features of a variable to be of register storage
class are as under:
• stored in CPU registers
• default initial value is garbage value
• scope is local to the block in which the variable is
defined.
• the life time is till the control remains within the
block in which the variable is defined.
CDS
33
Unit II – Arrays, Strings & Storage classes
Example
main ( ){
register int i;
for (i = 1; i < = 10; i ++)
printf (“%d \n”,i)
}
Even though we have declared the storage class of ‘i’
as register, we cannot be sure that the value of i
would be stored in CPU register, because of the
limited number of CPU registers.
CDS
34
Unit II – Arrays, Strings & Storage classes
ARRAYS
The ‘C’ language provides the capability to define a
set of ordered data elements known as an array.
Definition
An array is a group of related data items that share a
common name.
CDS
35
Unit II – Arrays, Strings & Storage classes
Array Declaration
Like other variables, arrays must be declared before
they are to be used.
Example:
float height[100];
declares height as an array variable which can store
100 floating values.
int code[10];
creates 100 memory locations which can store integer
values each addressed by code [i].
CDS
36
Unit II – Arrays, Strings & Storage classes
name 0 W
1 E
2 L
3 C
4 O
5 M
6 E
7 M
8 A
9 N
10 ‘\0’
Array Initialisation
Array elements can also be initialised like other
scalar variables, in the declaration itself.
CDS
37
Unit II – Arrays, Strings & Storage classes
Examples
Static int marks [3] = {10,20,30};
initialises marks[1] to 10; marks[2] to 20 and
marks[3] to 30.
CDS
38
Unit II – Arrays, Strings & Storage classes
CDS
39
Unit II – Arrays, Strings & Storage classes
Example Programs
Program to read a set of n numbers in an integer
array and calculate their sum and average.
#include <stdio.h>
main ( ) {
int i, n;
float sum, average;
int number [100]; ← array definition
scanf (“%d”, &n);
for (i = 0; i < = n; ++i)
scanf (“%d”, & numbers [i]);
sum = 0;
for (i = 0; i <=n; ++i)
sum + = numbers [i];
average = sum/n;
printf(“sum = %f, Average = %f”, sum, average);
}
CDS
40
Unit II – Arrays, Strings & Storage classes
CDS
41
Unit II – Arrays, Strings & Storage classes
Exercises
(1) Give the output of the following:
(a) int i = 0;
main ( ) {
auto int i = 1;
printf (“%d”, i);
{
int i = 10;
printf (“%d”, i);
{
i + = 10;
printf (“%d”, i);
}
printf (“%d”, i);
}
printf (“%d”, i);
}
(b) main ( ) {
int i,r;
for (i=1; i<=3; ++i){
r = check (i);
printf (“%d”, r);
}
}
check (int i) {
static int k = 1;
k = k * i;
return (k);
}
CDS
42
Unit II – Arrays, Strings & Storage classes
CDS
43
Unit II – Arrays, Strings & Storage classes
Example:
Program to find the largest and smallest element in an
array of ‘n’ elements:
main ( ){
int max_min ( );
int values [100];
int n; i;
printf(“%d”, & n);
CDS
44
Unit II – Arrays, Strings & Storage classes
CDS
45
Unit II – Arrays, Strings & Storage classes
CDS
46
Unit II – Arrays, Strings & Storage classes
Multidimensional arrays
Two-dimensional arrays
So far, we have seen how to handle single
dimensional arrays, which can be referenced by a
single subscript.
10 20 30 40
50 60 70 80
90 15 25 35
45 55 65 75
CDS
47
Unit II – Arrays, Strings & Storage classes
CDS
48
Unit II – Arrays, Strings & Storage classes
CDS
49
Unit II – Arrays, Strings & Storage classes
Programming problems
(1) Write a program using functions to add, and
multiply two matrices.
CDS
50
Unit II – Arrays, Strings & Storage classes
Examples:
int abc [3] [5] [4];
int xyz [5] [4] [2];
CDS
51
Unit II – Arrays, Strings & Storage classes
Strings
A string is an array of characters.
CDS
52
Unit II – Arrays, Strings & Storage classes
Example:
#include <stdio.h>
main ( ){
char name [20];
gets (name);
}
Example:
printf((“%s”, first-name);
would print the contents of the string first-name of
the screen.
CDS
53
Unit II – Arrays, Strings & Storage classes
Example:
strcat(strcat(str1, str2), str3);
The result would be str3 would be concatenated with
the concatenation of str1 and str2.
Example:
The statement strcpy (State, “Delhi”);
will assign the character constant “Delhi” to the
character array state, assuming that we have declared
the variable state as:
char state [15];
CDS
54
Unit II – Arrays, Strings & Storage classes
Example:
strcmp(“their”, “there”);
will the return the difference between i & r in terms
of the ASCII value.
CDS
55
Unit II – Arrays, Strings & Storage classes
CDS
56
Unit II – Arrays, Strings & Storage classes
Example:
Program to read two strings str1 and str2, compare
them, if they are not equal then concatenate str1 and
str2 and then copy str1 to str3.
#include <string.h>
main ( ){
char str [10], str2 [10], str3 [10];
scanf (“%s %s”, str1, str2);
if (strcmp (str1, str2) !=0){
strcat (str1, str2);
}
strcpy (str3, str1);
printf(“str1=%s, str2=%s, str3=%s”, str1, str2, str3);
}
CDS
57
Unit II – Arrays, Strings & Storage classes
CDS
58