You are on page 1of 13

C - Language

Arrays (I)
Why, when, what….
 It Arrays
becomes more difficult to keep track of n-number of
variables in a program. Arrays offer a solution to this
problem.
 An array is a multi-element box, a bit like a filing cabinet,
and uses an indexing system to find each variable stored
within it. In C, indexing starts at zero.
 Arrays, like other variables in C, must be declared before
they can be used.
 Arrays may consist of any of the valid data types.
 Arrays are declared along with all other variables in the
declaration section of the program.
Arrays
 A group of related data items that share a common
name.
 Can be of any variable type.
 Is a data structure that holds multiple variables of the
same data type.
 Individual elements in a array are indicated by the index
number or subscript in brackets after the array name

 Usage

data type <array name> [<size>]


Cont’d
float salary[5];

This memory gets reserved

Allocates continuous stretch of memory


One-Dimensional Array
 List of items given one variable name using only one
subscript are called single-subscripted variable or one-
dimensional array.

 The subscript begins with 0.


 Usage :-
int value[5]={10, 20, 30, 40, 50};

Or
10 value[0]
value[3] = 25; 20 value[1]
30 value[2]
printf(“%d”, value[3]); 40 value[3]
50 value[4]
Initialization of Arrays
 General form :-
type array-name [size] = { list of values }

 The size can be omitted, in such case the compiler


allocates enough space for all initialized elements.

 Drawbacks:-
 There is no convenient way to initialize only selected

elements.

 There is no shortcut method for initializing a large


number of array elements.
Accessing array elements
 Use a loop
 Counter variable => index

 Access element by element


 Character Arrays
 C language treats character strings simply as arrays of

characters.
 Usage :-

char <arrayname> [<size>]

 The size in a character array represents the maximum


number of character that the string can hold.
 Each character of the string is treated as an element of the
array.
 Character strings are terminated with an additional null
character.
 When declaring character array, allow one extra space for
the null terminator.
Reading strings from

terminal
Reading Words
 scanf function can be used with %s format specification to
read in a string of characters.
 Usage :-
scanf( “%s”, <char array name> );

 Ampersand (&) is not required before the variable name.


 scanf function automatically terminates the string that is
read with a null character.
 Problem with scanf - it terminates its input on the first
white space.
Writing strings to screen
 printf function with %s format to print strings to the screen.

 Usage :-

printf (“%s” , <array name> );


String – handling
 functions
Cannot assign one string to another directly in C language.

 Cannot join two strings together by simple arithmetic


addition.

string 3 = string1 + string2 NOT POSSIBLE


string 3 = “Hello!!!,“ + “Welcome to C”;

Putting Strings together (concatenation)


 Characters from string1 and string2 should be copied into
the string3 one after the other.

 Size of the array string3 should be large enough to hold the


total characters
Cont’d
Comparison of two Strings

 C does not permit the comparison of two strings directly.

if(name1 == name2)
` NOT POSSBILE
if(name == “NSBM”)

 Strings are always compared character by character.


Cont’d
 Some of string handling functions supported by C
library:-
Function Action Usage
strcat() Concatenates two strings string strcat(string, string)
strcmp() Compares two strings int strcmp(string, string)
strcpy() Copies one string over string strcpy(string, string)
another
strlen() Finds the length of a string int strlen(string)

You might also like