0% found this document useful (0 votes)
71 views7 pages

V Unit PDF

The document discusses arrays in C programming. It defines arrays as a collection of elements of the same type stored in contiguous memory locations. It describes one-dimensional and two-dimensional arrays, and how to declare, initialize, read from, and write to arrays. It also covers declaring and manipulating string variables as character arrays.

Uploaded by

Chithirai Veni
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
71 views7 pages

V Unit PDF

The document discusses arrays in C programming. It defines arrays as a collection of elements of the same type stored in contiguous memory locations. It describes one-dimensional and two-dimensional arrays, and how to declare, initialize, read from, and write to arrays. It also covers declaring and manipulating string variables as character arrays.

Uploaded by

Chithirai Veni
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Unit – V

1. Array
Introduction of array

 Arrays a kind of data structure that can store a fixed-size sequential


collection of elements of the same type.
 An array is used to store a collection of data, but it is often more useful to
think of an array as a collection of variables of the same type
 All arrays consist of contiguous memory locations. The lowest address
corresponds to the first element and the highest address to the last element .
 An array is a boundary collection of elements of the same data type
Array are categorized as

1. One dimensional array


2. Two dimensional array
3. Multi-dimensional array

Declaring Arrays

 To declare an array in C, a programmer specifies the type of the


elements and the number of elements required by an array as follows.
Syntax

type arrayName [ arraySize ];

This is called a single-dimensional array. The arraySize must be an


integer constant greater than zero and type can be any valid C data type.

Example

double balance[10];

1
Initializing Arrays

 Initialize an array in C either one by one or using a single statement as


follows
Syntax

double balance[5] = {1000.0, 2.0, 3.4, 7.0, 50.0};

 The number of values between braces { } cannot be larger than the number
of elements that we declare for the array between square brackets [ ].
 If you omit the size of the array, an array just big enough to hold the
initialization is created.
2. Declaring of one and two dimensional array

1.One dimensional arrays

 Array which have elements with single subscript are known as one
dimensional array.
 It is simply a grouping of like type data.
 It can be used to represent a list of number or a list of names.
Syntax:

datatype array_name[size];

regno [10]

 Data type: It denotes the type of the elements in the array.


 array_name: Name of the array. It must be a valid identifier.

Declaration of one dimensional array:-

 All the variables are declared before they are used in the program. Similarly,
an array must be declared before it is used.
 During declaration, the size of the array has to be specified. The size used
during declaration of the array informs the compiler to allocate and reserve
the specified memory locations.
2
Syntax:-

data_type array_name[n];

 Where, n is the number of data items (or) index(or) dimension. 0 to (n-1) is


the range of array.
Example:

int a[5]; float x[10];

2.Two dimensional array

 A two dimensional array consists of rows and cloumns.each lement is


accessed by two subscripts.
Syntax

datatype array_name[ROW][COL];

Declaration of two dimensional array

 An array consisting of two subscripts is known as two-dimensional array.


These are often known as array of the array.
 In two dimensional arrays the array is divided into rows and columns. These
are well suited to handle a table of data. In 2-D array we can declare an array
as
Syntax:

data_type array_name[row_size][column_size];

Example:-

int arr[3][3];

 Where first index value shows the number of the rows and second index
value shows the number of the columns in the array.

3
3. Initialization of one and two dimensional array

1. Initializing one-dimensional arrays:

 The elements of an array can be initialized in the declaration itself.

Syntax

Type array name [size] = {list of values}

Example:

Int reg no [5] = {10, 11,12,13,14,};

2. Initializing two-dimensional arrays:

 Like the one-dimensional arrays, two-dimensional arrays may be initialized


by following their declaration with a list of initial values enclosed in braces.

Example:

int a[2][3]={0,0,0,1,1,1};
initializes the elements of the first row to zero and the second row to one. The
initialization is done row by row.

int a[2][3] = {{ 0,0,0},{1,1,1}};

by surrounding the elements of each row by braces. We can also initialize a


two-dimensional array in the form of a matrix as shown below

int a[2][3]=

{0,0,0},

{1,1,1}

};

4
4. Declaring and initialling string variables
String

 A string is a sequence of characters that is treated as a single data item.

Declaration of string variables


 String _name is the name of the string variable and size is the number of
character in the string.
Syntax

Char variable_name[size];

Example

char name[10];
char city[20];

Initialization of string variables


 Character arrays can be initialized when they are declared. Character at the
end of the string.
Example

Char string [] = “SIVA”

Char city [10] = “BANGALORE”;

5. Reading string from terminal


Reading Strings

 They are three types of using reading strings scanf, gets, and fgets
 Scanf
 Gets
 fgets
1. scanf

 The standard format specifier for reading strings with scanf is %s


 scanf reads in a string of characters, only up to the first non-whitespace
character. I.e. it stops reading when it encounters a space, tab, or newline
character.
 scanf appends a null byte to the end of the character string stored.

5
 scanf does skip over any leading whitespace characters in order to find the
first non-whitespace character.
 The width field can be used to limit the maximum number of characters
read.
 The argument passed to scanf needs to be of type pointer-to-character, i.e.
char *, and is normally the name of a char array. ( In this case the size of the
array -1 should be given as the width field, to make sure scanf can append
the null byte without overflowing the array.
char * gets ( char * str );

 The gets function will read a string of characters from standard input ( the
keyboard ) until it encounters either a newline character or the end of file.
 Unlike scanf, gets does not care about spaces in the input, treating them the
same as any other character.
 Note that gets has no provision for limiting the number of characters read,
leading to possible overflow problems.
 When gets encounters a new line character, it stops reading, but does NOT
store it.
 A null byte is always appended to the end of the string of stored characters.
char * fgets ( char * str, int num, FILE * stream );

 fgets is equivalent to gets, except for two additional arguments and one key
difference:
 num provides for a limit on the maximum number of characters read
 The FILE * argument allows fgets to read from any file. stdin may be
entered to read from the keyboard.
 fgets stops reading when it encounters a new line character, and it DOES
store the newline character.
 A null byte is always appended to the end of the string of stored characters.

6. Writing string on the screen


Writing Strings

They are two types of using writing string printf and puts

printf

 The standard format specifier for printing strings with printf is %s


 The width parameter can be used to print a short string in a long space
 The .precision parameter limits the length of longer strings.

6
int puts ( const char * str );

 Writes the string out to standard output ( the screen ) and automatically
appends a newline character at the end.
 This is a lower-level routine that printf, without formatting, and possibly
faster

7. Arithmetic operator on characters

 C Programming Allows you to Manipulate on String


 Whenever the Character is variable is used in the expression then it
is automatically Converted into Integer Value called ASCII value
 All Characters can be Manipulated with that Integer Value.(Addition,
Subtraction)

Syntax

1. ASCII value of : ‘a’ is 97


2. ASCII value of : ‘z’ is 121

Example:

Char x = 'z' - 'a';

printf("%d",x);

/* Display Result = 25

(difference between ASCII of z and a ) */

You might also like